为什么我无法获得访问令牌?

时间:2013-02-23 13:25:00

标签: php curl google-api access-token

我正在尝试使用此代码获取access token但没有取得任何成功。我检查了每个卷曲操作,每件事都很好。但仍然无法获得令牌。

this is OAuth2 Guidlines from Google

$data_string = "code=" . $_REQUEST['code'] . "&client_id=" . $client_id . "&client_secret=" . $client_secret . "&redirect_uri=" . $redirect . "&grant_type=authorization_code";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,'https://accounts.google.com/o/oauth2/token');
curl_setopt($ch,CURLOPT_POST,TRUE);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-type: application/x-www-form-urlencoded'));
curl_setopt($ch,CURLOPT_POSTFIELDS,$data_string);
$response = curl_exec($ch);
curl_close($ch);
//$data = json_decode($response);
print_r($response);

2 个答案:

答案 0 :(得分:0)

$data_string = "code=" . $_REQUEST['code'] . "&client_id=" . $client_id . "&client_secret=" . $client_secret . "&redirect_uri=" . $redirect . "&grant_type=authorization_code";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://accounts.google.com/o/oauth2/token');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_POST,TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string);
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-type: application/x-www-form-urlencoded'));
$response = curl_exec($ch);
curl_close($ch);

答案 1 :(得分:0)

请注意,如果您在网址中使用HTTPS,则必须添加代码:

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);