简体   繁体   English

php 中使用 curl 的 OAuth 2.0

[英]OAuth 2.0 in php using curl

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

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

but, the php script return me only this error message:但是,php 脚本只返回此错误消息:

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

I tried to remove client_secret/client_id and use only client_id/client_secret, but still get the same error.我试图删除 client_secret/client_id 并仅使用 client_id/client_secret,但仍然出现相同的错误。

PHP script 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");

$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);

Although curl in cmd works and returns me access and refresh token without any errors.尽管 cmd 中的 curl 可以工作并返回我的访问权限和刷新令牌而没有任何错误。

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

I don't understand why I get the missing scheme error, although the .php script exists and it's located on the given path.我不明白为什么我会收到丢失的方案错误,尽管 .php 脚本存在并且它位于给定的路径上。 Could you help me please ?请问你能帮帮我吗 ?

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. 解决了“redirect_uri 的参数值无效:缺少方案”的问题,我只是在 CURLOPT_POSTFIELDS 中用这个 'redirect_uri' => $redirect_uri 替换了 'redirect_uri' => urlencode($redirect_uri)。

Wow, stupid mistake, I should have a rest.哇,愚蠢的错误,我应该休息一下。

The variables names don't match.变量名称不匹配。 I defined:我定义:

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

But here I used an non-existing clientID and clientSecret :但在这里我使用了一个不存在的 clientID 和 clientSecret :

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

Fixed and working PHP script固定和工作的 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);

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:但我不得不说,谷歌在这里提供了一些误导性的错误信息,因为我没有定义 client_id 和 client_secret,错误信息是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM