重定向uri中我的Google+ oauth2州参数在哪里?

时间:2013-06-02 03:12:01

标签: php oauth-2.0

我正在为我正在构建的网络应用程序进行OpenID / oAuth2登录过程。

我可以向Google+发出授权请求。我的redirect_uri中返回了几个参数:

access_token = averylongstring
token_type = Bearer
expires_in = 3600
id_token = anextremelylongstring
refresh_token = 1/Jo_tXhJtL3sQTBZURWyKWwebQSjxY1Rb-7sflDC74Pw
created = 1370140758

然后我执行一个类似于:

的cURL GET
$url = 'https://www.googleapis.com/oauth2/v1/userinfo';
$curl_data = "?access_token=".$tokens['access_token']."&code=".urlencode('xxxxxxxxx.apps.googleusercontent.com')."&client_id=".urlencode('xxxxxxxxx.apps.googleusercontent.com')."&client_secret=".urlencode('xxxxxxxxx')."&redirect_uri=".urlencode('http://www.mydomain.com/oauth2callback.php')."&grant_type=authorization_code";

这给了我一个id,我认为这是我认证流程的成功结论。

那么,我在哪里丢失了

之间的状态变量
https://accounts.google.com/o/oauth2/auth?response_type=code&redirect_uri=http%3A%2F%2Fwww.mydomain.com%2Foauth2callback.php&client_id=xxxxxxxxxxxxxx.apps.googleusercontent.com&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fplus.login&access_type=offline&approval_prompt=force&state={%22cid%22%3A%22%22%2C%22tid%22%3A%221370142477%22%2C%22app%22%3A%22anappname%22%2C%22Provider%22%3A%22%22}

...和我的redirect_uri?

1 个答案:

答案 0 :(得分:0)

看起来有两个重定向到我的回调网址。在第一次重定向中,当用户允许访问其Google信息时,会立即跟随,将网址变量“状态”和“代码”发送到我的回调页面。但是,然后另一个调用,即最终的身份验证请求,来自我的回调页面,结果只是一个没有查询字符串的干净网址。

因此,在第一次重定向到我的回调页面时,我读取了GET变量,并将它们转换为SESSION变量。接下来,执行cURL(我仍然没有这个部分,因为我正在发布cURL帖子:

https://accounts.google.com/o/oauth2/token

...我的cURL代码如下:

    $ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"$url");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('access_token' => $access_token,
                                                            'code' => $_REQUEST['code'],
                                                            'client_id' => 'myclientid.apps.googleusercontent.com',
                                                            'client_secret' => 'mylittlesecret',
                                                            'redirect_uri' => 'http://www.mydomain.com/oauth2callback.php',
                                                            'grant_type' => 'authorization_code',
                                                            'approval_prompt' => 'force')));

获得:

{ "error" : "invalid_request" }

在编码项目的这一部分,我真的不高兴。