首页 文章

使用curl在php中的OAuth 2.0

提问于
浏览
1

我需要让我的access_token和refresh_token for OAuth 2.0来访问Google API,下面的php脚本应该返回一个带有access_token的json,refresh_token如下:

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

但是,php脚本只返回此错误消息:

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

我试图删除client_secret / client_id并仅使用client_id / client_secret,但仍然得到相同的错误 .

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

虽然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

我不明白为什么我得到丢失的方案错误,虽然.php脚本存在并且它位于给定的路径上 . 请问你能帮帮我吗 ?

编辑

问题"Invalid parameter value for redirect_uri: Missing scheme"已解决,我只是用CURLOPT_POSTFIELDS中的'redirect_uri' => $ redirect_uri替换了'redirect_uri' => urlencode($ redirect_uri) .

2 回答

  • 3

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

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

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

    但在这里我使用了一个不存在的clientID,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);
    

    但我不得不说,谷歌在这里提供了一些误导性的错误信息,因为我既没有定义client_id也没有定义client_secret,错误信息是:

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

    对不起,我无法发表评论 .

    但是你在 CMD-Line 电话和 php 电话中使用了两个不同的 redirect_uri .

    我认为应该是

    $redirect_uri = http://localhost/local/csv/refreshFusionTable.php;
    

    $redirect_uri = 'http://localhost/phpConnectToDB/csv/refreshFusionTable.php';
    

    .

相关问题