我一直在嘗試使用Google Oauth2獲取訪問令牌和刷新令牌的最後幾天,但沒有獲取任何地方。使用CURL獲取Google Client API訪問令牌
該文檔是垃圾,我找不到任何可用的示例。
我剛剛開始使用CURL試圖獲取訪問代碼和刷新令牌,但我不斷收到錯誤的響應。
我一直在使用這個
// init the resource
$url = 'https://accounts.google.com/o/oauth2/token';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded',
'Content-length: 0'
));
curl_setopt($ch, CURLOPT_POSTFIELDS,
'code=' . urlencode('AUTHORISATION_CODE') . '&' .
'client_id=' . urlencode('MY_CLINET_ID.apps.googleusercontent.com') . '&' .
'client_secret=' . urlencode('MY_CLIENT_SECRET') . '&' .
'redirect_uri=http://www.example.com/' . '&' .
'grant_type=authorization_code'
);
// execute
$output = curl_exec($ch);
echo $output;
// free
curl_close($ch);
嘗試,但我剛剛得到這個響應
{
"error" : "invalid_request",
"error_description" : "Required parameter is missing: grant_type"
}
,我已經試過在命令行中運行卷曲
curl --data "code=AUTHORISATION_CODE&client_id=MY_CLIENT_ID.apps.googleusercontent.com&client_secret=MY_CLIENT_SECRET&redirect_uri=http://www.example.com/&grant_type=authorization_code" https://accounts.google.com/o/oauth2/token
,但我只是得到不同的迴應
{
"error" : "invalid_client",
"error_description" : "The OAuth client was not found."
}
爲什麼僅僅獲取訪問令牌非常困難?
UPDATE
我不知道發生了什麼,但我沒有收到錯誤了,我得到一個訪問令牌,但沒有刷新令牌
我看了這個帖子Not receiving Google OAuth refresh token我已經取消了對我的應用程序的訪問來重新生成刷新令牌,但使用此代碼顯示關於無效代碼的錯誤
只是一個想法,但省略了最後'/'從'redirect_uri'。或者以某種方式逃避。 – loveNoHate 2014-09-12 12:20:34
沒有任何區別 – AdRock 2014-09-12 12:37:34
你做了什麼? – loveNoHate 2014-09-12 12:41:42