【问题标题】:why I am not able to get access token?为什么我无法获得访问令牌?
【发布时间】:2013-02-09 01:33:15
【问题描述】:

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

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);

【问题讨论】:

  • 如果你没有得到一个令牌,一切都不好。您能否澄清“无法获得令牌”的含义?访问者永远不会被重定向到您的网站?重定向是在没有令牌的情况下发生的?你看到一个例外还是?响应是空的吗?

标签: php curl google-api access-token


【解决方案1】:
$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);

【讨论】:

    【解决方案2】:

    请记住,如果您在 URL 中使用 HTTPS,则必须在代码中添加:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-01
      相关资源
      最近更新 更多