php - php 中的 OAuth 2.0 使用 curl

标签 php curl oauth-2.0 google-api http-post

我需要获取 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" : "####"
}

但是,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 工作并返回我访问和刷新 token 而没有任何错误。

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 脚本存在并且它位于给定的路径上。你能帮帮我吗?

编辑

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

最佳答案

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

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

    $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"
    }

关于php - php 中的 OAuth 2.0 使用 curl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25423924/

相关文章:

java - 将 Curl 转换为 Java 等价物

c# - 使用 Auth0 进行 Azure AD 身份验证

oauth - "Invalid JWT: Failed audience check."尝试从 Google 获取 OAuth token 时出错

php - MYSQL 搜索脚本 php 的错误结果

php - XmlHttpRequest 与 cURL

php - 如何禁用php中的mysql功能

powershell - 从powershell调用cmd无法正确解析?

oauth - OAuth 1 的 future 是什么?

php - 如何从数据库中获取 php session 的用户类型

javascript - 如何在具有文件 jquery.dataTables.js 的 jquery 插件中对同一列中的日期和字符串进行排序?