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

How to reauthenticate a user using Google Oauth2

这是我的问题。我在带有自定义图像的页面上有 Google Plus link。 URL 是

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和重定向是动态传入的。 (此 link 由 PHP 函数生成。)

用户单击 link 并使用 Google 进行身份验证。现在我需要将它们登录到我的应用程序中。唯一似乎从服务器返回的是身份验证代码。我不知何故需要一个 Google_Client 来获取用户信息。事情是当我构建客户端以满足 Google 的所有要求时,我遇到了一个问题,我正在尝试重用代码。我想我已经想出了解决办法。

不过,发生的事情是 redirect_uri_mismatch。广泛的谷歌搜索说这是因为 URI 不在我的开发者控制台中。但它是。我已经四次检查它,它完全一样。没有特殊端口或尾部斜线或任何东西。所以我不明白为什么会出现此错误。

难道是我在上面link中传入了一个redirect_uri然后在下面指定了一个?我确实注意到,如果我使两个 redirect_uri 相同,redirect_uri 错误就会消失,但随后我会收到一个错误,提示代码已被兑换。我猜是因为它循环回到之前的位置。无论如何我不能让两者相同,因为我需要不同的通过我的代码来路由浏览器。

(下面所有的Zend_Registry值都已经确认。这个函数returns一个字符串,必要的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()));

 }

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

范围需要用 space 分隔。您已添加 . '&' 。

这意味着第二个作用域是一个无效键,因为 & 是一个特殊字符。

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