首页 文章

如何使用Google Oauth2重新验证用户身份

提问于
浏览
0

这是我的问题 . 我在带有自定义图片的页面上有一个Google Plus链接 . 网址是

https://accounts.google.com/o/oauth2/auth?client_id=' . Zend_Registry :: get('config') - > googlePlus-> client_id . '&redirect_uri =' . urlencode($ strRedirectUrl) . '&access_type = offline&response_type = code&scope =' . urlencode('https://www.googleapis.com/auth/plus.login') . '&' . 进行urlencode( 'https://www.googleapis.com/auth/plus.me')

客户端ID和重定向是动态传递的 . (此链接由PHP函数生成 . )

用户点击链接并通过Google进行身份验证 . 现在我需要将它们记录到我的应用程序中 . 似乎从服务器返回的唯一事情是身份验证代码 . 我不知何故需要 Google_Client 我可以获取用户信息 . 事情是我 Build 客户端以满足所有谷歌试图重用代码 . 我想我已经找到了解决方法 .

但是,会发生什么事情才能获得 redirect_uri_mismatch . 广泛的谷歌搜索说这是因为URI不在我的开发者控制台中 . 但确实如此 . 我've quadruple checked it and it is exactly the same. There are no special ports or trailing slashes or anything. So I can'弄清楚为什么我收到这个错误 .

是因为我在上面的链接中传入了redirect_uri,然后在下面指定一个?我注意到如果我使两个redirect_uris相同,redirect_uri错误消失了,但是我得到一个错误,代码已经被兑换了 . 我猜是因为它骑自行车回到原来的位置 . 无论如何,我不能让两者相同,因为我需要不同的方法来通过我的代码路由浏览器 .

(以下所有Zend_Registry值都已确认 . 此函数返回一个字符串,即必要的API密钥 . )

$client = new Google_Client();
$client->setApplicationName('Web Application');
$client->setClientId(Zend_Registry::get('config')->googlePlus->client_id);
$client->setDeveloperKey(Zend_Registry::get('config')->googlePlus->serverKey);
$client->setClientSecret(Zend_Registry::get('config')->googlePlus->secret);
$client->setRedirectUri('http://test.XXX.com/login/social/network/google');
$client->setScopes(array('https://www.googleapis.com/auth/userinfo.email','https://www.googleapis.com/auth/userinfo.profile'));
$client->setAccessType('offline');
$client->setApprovalPrompt('force'); # this line is important when you revoke permission from your app, it will prompt google approval dialogue box forcefully to user to grand offline access
$client->getRefreshToken();

$plus = new Google_Service_Oauth2($client);

if (isset($_GET['code'])) {
    $client->authenticate($_GET['code']);
}

if ($client->getAccessToken())
 {
     $userinfo = $plus->userinfo;
     die(print_r($userinfo->get()));

 }

我不喜欢这种结构的方式,因为当用户在弹出窗口中填写他们的凭据时,我已经使用Google进行了身份验证 . 但我没有看到任何解决方法 . 我愿意接受任何建议 . 这是我第一次使用这个API,我不得不说文档非常糟糕 .

1 回答

  • 0

    范围需要用空格分隔 . 你添加了 . '&' .

    这意味着第二个范围是无效密钥,因为&是一个特殊字符 .

    如果您不知道,您可能会发现Oauthplayground在尝试各种请求时非常有用 . https://developers.google.com/oauthplayground/

相关问题