PHP。如何获取访问令牌。使用Google API的OAuth 2.0进行身份验证

问题描述:

I found this function据推测,它会获取accessToken,但我什么也没得到。 我确实有$_REQUEST['code']以及此功能所需的其他信息。PHP。如何获取访问令牌。使用Google API的OAuth 2.0进行身份验证

任何想法这里有什么错? 谢谢。

//Oauth 2.0: exchange token for session token so multiple calls can be made to api 
if(isset($_REQUEST['code'])){ 
    $_SESSION['accessToken'] = get_oauth2_token($_REQUEST['code']); 
} 

//returns session token for calls to API using oauth 2.0 
function get_oauth2_token($code) { 
    global $client_id; 
    global $client_secret; 
    global $redirect_uri; 

    $oauth2token_url = "https://accounts.google.com/o/oauth2/token"; 
    $clienttoken_post = array(
    "code" => $code, 
    "client_id" => $client_id, 
    "client_secret" => $client_secret, 
    "redirect_uri" => $redirect_uri, 
    "grant_type" => "authorization_code" 
    ); 

    $curl = curl_init($oauth2token_url); 

    curl_setopt($curl, CURLOPT_POST, true); 
    curl_setopt($curl, CURLOPT_POSTFIELDS, $clienttoken_post); 
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY); 
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 

    $json_response = curl_exec($curl); 
    curl_close($curl); 

    $authObj = json_decode($json_response); 

    if (isset($authObj->refresh_token)){ 
     //refresh token only granted on first authorization for offline access 
     //save to db for future use (db saving not included in example) 
     global $refreshToken; 
     $refreshToken = $authObj->refresh_token; 
    } 

    $accessToken = $authObj->access_token; 
    return $accessToken; 
} 

我没有看到你的代码有什么问题,但你可能想尝试刷新客户端的秘密,看看是否有帮助。此外,我建议你确切地看到curl命令返回的响应,我怀疑它是“invalid_grant”。

一个更好的方法来做到这一点是使用谷歌的PHP API客户端:

https://code.google.com/p/google-api-php-client/

,可处理大部分通信的为您服务。这里的例子非常易于使用。它主要取决于您尝试访问哪个Google服务。

该代码看起来很熟悉 - 我使用的代码大致相同 - 有两件事你可以尝试。

  1. 使用echo回显到浏览器

    回声$ json_response响应;提琴手

  2. 弄个并使用下面的线路的呼叫前curl_exec

    curl_setopt($卷曲,CURLOPT_PROXY, '127.0.0.1:8888');

“冒牌货我还没有得到它的工作要么 - 我得到的回应是

{ 
    "error" : "invalid_request" 
} 

现在如果有谁知道怎么样是不对的;)

从4.4节.2“OAuth 2.0授权协议draft-ietf-oauth-v2-20”

客户端向令牌端点发出请求,方法是添加 个使用“应用程序/ x WWW的形式进行了urlencoded”以下参数 格式在HTTP请求实体主体:

所以所张贴的参数应在一个字符串,而不是一个阵列的形式被提交。这也在curl_setopt的PHP手册中提到过。

因此,不要发布$ clienttoken_post,您可能需要发布http_build_query($ clienttoken_post,'','&')。

这可能无法解决您的所有问题,但这可能是迈向正确方向的一步。

我有同样的错误,我发现,添加

curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); 

允许请求遵循重定向迅速解决它。我希望能帮助别人!

这就是我做我的访问令牌和刷新令牌。

创建一个包含下面的代码文件:

<?php 

if (isset($_GET['code'])) { 
    // try to get an access token 
    $code = $_GET['code']; 
    $url = 'https://accounts.google.com/o/oauth2/token'; 
    $params = array(
     "code" => $code, 
     "client_id" => YOUR_CLIENT_ID, 
     "client_secret" => YOUR_CLIENT_SECRET, 
     "redirect_uri" => 'http://' . $_SERVER["HTTP_HOST"] . $_SERVER["PHP_SELF"], 
     "grant_type" => "authorization_code" 
    ); 

    $ch = curl_init(); 
    curl_setopt($ch, constant("CURLOPT_" . 'URL'), $url); 
    curl_setopt($ch, constant("CURLOPT_" . 'POST'), true); 
    curl_setopt($ch, constant("CURLOPT_" . 'POSTFIELDS'), $params); 
    $output = curl_exec($ch); 
    $info = curl_getinfo($ch); 
    curl_close($ch); 
    if ($info['http_code'] === 200) { 
     header('Content-Type: ' . $info['content_type']); 
     return $output; 
    } else { 
     return 'An error happened'; 
    } 
} else { 

    $url = "https://accounts.google.com/o/oauth2/auth"; 

    $params = array(
     "response_type" => "code", 
     "client_id" => YOUR_CLIENT_ID, 
     "redirect_uri" => 'http://' . $_SERVER["HTTP_HOST"] . $_SERVER["PHP_SELF"], 
     "scope" => "https://www.googleapis.com/auth/plus.me" 
    ); 

    $request_to = $url . '?' . http_build_query($params); 

    header("Location: " . $request_to); 
} 

现在,您的客户端ID和客户端密钥替换YOUR_CLIENT_IDYOUR_CLIENT_SECRET

确保您的范围是正确的。例如,如果您想访问Google Analytics,那么它应该是https://www.googleapis.com/auth/analytics

如果您运行该文件,则应该获得OAuth2批准屏幕。

如果你现在按Accept,你应该得到的结果看起来像这样:

{ 
    "access_token" : YOUR_ACCESS_TOKEN, 
    "token_type" : "Bearer", 
    "expires_in" : 3600, 
    "refresh_token" : YOUR_REFRESH_TOKEN 
} 

结果可能包含附加字段,这取决于你申请的是哪个范围。

+1

Thanx alot bro ....你的代码对我来说是完美的 – deemi 2016-10-13 11:49:58

+0

你是如何运行该文件的?我把它放到filezilla的一个php文件中,并用浏览器执行。但它不适用于我 – Simone 2017-08-13 11:45:03

+0

@Simone:你有任何错误? – 2017-08-13 15:40:33