問題描述
我需要獲取用于 OAuth 2.0 的 access_token 和 refresh_token 才能訪問 Google API,下面的 php 腳本應(yīng)該返回一個帶有 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,但仍然出現(xiàn)相同的錯誤.
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 可以正常工作并返回我的訪問權(quán)限和刷新令牌而沒有任何錯誤.
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
我不明白為什么會出現(xiàn)缺少的方案錯誤,盡管 .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 的參數(shù)值無效:缺少方案"的問題,我只是用這個 '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.推薦答案
哇,愚蠢的錯誤,我應(yīng)該休息一下.
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在這里提供了一個有點(diǎn)誤導(dǎo)性的錯誤信息,因?yàn)槲覜]有定義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"
}
這篇關(guān)于php 中使用 curl 的 OAuth 2.0的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!