我需要得到我的access_token和refresh_token为OAuth 2.0访问谷歌api,下面的php脚本应该返回一个json与access_token, 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);
虽然curl在cmd工作,并返回我访问和刷新令牌没有任何错误。
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脚本存在,它位于给定的路径上。你能帮我一下吗?
EDIT
问题"redirect_uri的无效参数值:缺失方案"解决了,我只是替换了'redirect_uri' => urlencode($redirect_uri),与此'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"
}