<i id='SbPtT'><tr id='SbPtT'><dt id='SbPtT'><q id='SbPtT'><span id='SbPtT'><b id='SbPtT'><form id='SbPtT'><ins id='SbPtT'></ins><ul id='SbPtT'></ul><sub id='SbPtT'></sub></form><legend id='SbPtT'></legend><bdo id='SbPtT'><pre id='SbPtT'><center id='SbPtT'></center></pre></bdo></b><th id='SbPtT'></th></span></q></dt></tr></i><div id='SbPtT'><tfoot id='SbPtT'></tfoot><dl id='SbPtT'><fieldset id='SbPtT'></fieldset></dl></div>

      <small id='SbPtT'></small><noframes id='SbPtT'>

    1. <tfoot id='SbPtT'></tfoot>
        <bdo id='SbPtT'></bdo><ul id='SbPtT'></ul>

      1. <legend id='SbPtT'><style id='SbPtT'><dir id='SbPtT'><q id='SbPtT'></q></dir></style></legend>

        php 中使用 curl 的 OAuth 2.0

        时间:2023-10-02

        <small id='fveAh'></small><noframes id='fveAh'>

          <bdo id='fveAh'></bdo><ul id='fveAh'></ul>

            <tbody id='fveAh'></tbody>
          <i id='fveAh'><tr id='fveAh'><dt id='fveAh'><q id='fveAh'><span id='fveAh'><b id='fveAh'><form id='fveAh'><ins id='fveAh'></ins><ul id='fveAh'></ul><sub id='fveAh'></sub></form><legend id='fveAh'></legend><bdo id='fveAh'><pre id='fveAh'><center id='fveAh'></center></pre></bdo></b><th id='fveAh'></th></span></q></dt></tr></i><div id='fveAh'><tfoot id='fveAh'></tfoot><dl id='fveAh'><fieldset id='fveAh'></fieldset></dl></div>

            <legend id='fveAh'><style id='fveAh'><dir id='fveAh'><q id='fveAh'></q></dir></style></legend>
                <tfoot id='fveAh'></tfoot>

                  本文介绍了php 中使用 curl 的 OAuth 2.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我需要获取用于 OAuth 2.0 的 access_token 和 refresh_token 才能访问 Google API,下面的 php 脚本应该返回一个带有 access_token、refresh_token 的 json,如下所示:

                  I need to get my access_token and refresh_token for OAuth 2.0 to Access Google APIs, the php script below should return a json with access_token, refresh_token like this:

                  {
                    "access_token" : "####",
                    "token_type" : "Bearer",
                    "expires_in" : 3600,
                    "refresh_token" : "####"
                  }
                  

                  但是,php 脚本只返回这个错误信息:

                  but, the php script return me only this error message:

                  {
                  "error" : "invalid_request",
                  "error_description" : "Client must specify either client_id or client_assertion, not both"
                  }
                  

                  我尝试删除 client_secret/client_id 并仅使用 client_id/client_secret,但仍然出现相同的错误.

                  I tried to remove client_secret/client_id and use only client_id/client_secret, but still get the same error.

                  $client_id = '###.apps.googleusercontent.com';
                  $redirect_uri = 'http://localhost/phpConnectToDB/csv/refreshFusionTable.php';
                  $client_secret = '###';
                  
                  $ch = curl_init();
                  
                  curl_setopt($ch, CURLOPT_URL, "https://accounts.google.com/o/oauth2/token");
                  
                  $code = $_REQUEST['code'];
                  
                  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
                  
                  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                  
                  
                  curl_setopt($ch, CURLOPT_POSTFIELDS, array(
                  'code' => $code,
                  'client_id' => $clientID,
                  'client_secret' => $clientSecret,
                  'redirect_uri' => $redirect_uri,
                  'grant_type' => 'authorization_code'
                  ));
                  
                  $data = curl_exec($ch);
                  
                  var_dump($data);
                  

                  尽管 cmd 中的 curl 可以正常工作并返回我的访问权限和刷新令牌而没有任何错误.

                  Although curl in cmd works and returns me access and refresh token without any errors.

                  curl --data "code=###&client_id=###.apps.googleusercontent.com&client_secret=###&redirect_uri=http://localhost/phpConnectToDB/csv/refreshFusionTable.php&grant_type=authorization_code" https://accounts.google.com/o/oauth2/token
                  

                  我不明白为什么会出现缺少的方案错误,尽管 .php 脚本存在并且它位于给定的路径上.你能帮我吗?

                  I don't understand why I get the missing scheme error, although the .php script exists and it's located on the given path. Could you help me please ?

                  EDIT

                  解决了redirect_uri 的参数值无效:缺少方案"的问题,我只是用这个 'redirect_uri' => $redirect_uri 替换了 'redirect_uri' => urlencode($redirect_uri),在CURLOPT_POSTFIELDS.

                  EDIT

                  Problem with "Invalid parameter value for redirect_uri: Missing scheme" solved, I just replaced 'redirect_uri' => urlencode($redirect_uri), with this 'redirect_uri' => $redirect_uri, in CURLOPT_POSTFIELDS.

                  推荐答案

                  哇,愚蠢的错误,我应该休息一下.

                  Wow, stupid mistake, I should have a rest.

                  变量名称不匹配.我定义:

                  The variables names don't match. I defined:

                      $client_id = '###.apps.googleusercontent.com';
                      $client_secret = '###';
                  

                  但是这里我使用了一个不存在的 clientID 和 clientSecret :

                  But here I used an non-existing clientID and clientSecret :

                      curl_setopt($ch, CURLOPT_POSTFIELDS, array(
                      'code' => $code,
                      'client_id' => $clientID,
                      'client_secret' => $clientSecret,
                      'redirect_uri' => $redirect_uri,
                      'grant_type' => 'authorization_code'
                      ));
                  

                  固定和工作的 PHP 脚本

                      $client_id = '###.apps.googleusercontent.com';
                      $redirect_uri = 'http://localhost/phpConnectToDB/csv/refreshFusionTable.php';
                      $client_secret = '###';
                  
                      $ch = curl_init();
                      
                      curl_setopt($ch, CURLOPT_URL, "https://accounts.google.com/o/oauth2/token");
                      
                      curl_setopt($ch, CURLOPT_POST, TRUE);
                      
                      $code = $_REQUEST['code'];
                  
                      // This option is set to TRUE so that the response
                      // doesnot get printed and is stored directly in
                      // the variable
                      curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
                      
                      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                      
                      curl_setopt($ch, CURLOPT_POSTFIELDS, array(
                      'code' => $code,
                      'client_id' => $client_id,
                      'client_secret' => $client_secret,
                      'redirect_uri' => $redirect_uri,
                      'grant_type' => 'authorization_code'
                      ));
                  
                      $data = curl_exec($ch);
                      
                      var_dump($data);
                  

                  但是我不得不说google在这里提供了一个有点误导性的错误信息,因为我没有定义client_id和client_secret,错误信息是:

                  But I have to say that google provides a little misleading error message here, because I hadn't defined client_id nor client_secret and the error message was:

                      {
                      "error" : "invalid_request",
                      "error_description" : "Client must specify either client_id or client_assertion, not both"
                      }
                  

                  这篇关于php 中使用 curl 的 OAuth 2.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:使用 YoutubeApi v3 和 ouath2 在没有用户身份验证的情 下一篇:使用 Laravel 5.4 和 Passport 进行多重身份验证

                  相关文章

                  最新文章

                  <i id='SJWfR'><tr id='SJWfR'><dt id='SJWfR'><q id='SJWfR'><span id='SJWfR'><b id='SJWfR'><form id='SJWfR'><ins id='SJWfR'></ins><ul id='SJWfR'></ul><sub id='SJWfR'></sub></form><legend id='SJWfR'></legend><bdo id='SJWfR'><pre id='SJWfR'><center id='SJWfR'></center></pre></bdo></b><th id='SJWfR'></th></span></q></dt></tr></i><div id='SJWfR'><tfoot id='SJWfR'></tfoot><dl id='SJWfR'><fieldset id='SJWfR'></fieldset></dl></div>

                    1. <small id='SJWfR'></small><noframes id='SJWfR'>

                    2. <tfoot id='SJWfR'></tfoot>
                        <bdo id='SJWfR'></bdo><ul id='SJWfR'></ul>

                    3. <legend id='SJWfR'><style id='SJWfR'><dir id='SJWfR'><q id='SJWfR'></q></dir></style></legend>