用户拒绝/取消同意屏幕时,Google OAuth 2.0重定向网址

时间:2015-03-13 16:27:38

标签: php google-oauth

我正在将我的PHP应用中的使用Google登录从旧的现已弃用的OpenID迁移到新的OAuth 2.0。

用户需要的只是他的电子邮件地址,所以我可以注册或登录他。这一切都按预期工作,但我注意到如果用户拒绝同意屏幕(取消< / em>按钮,白色)再次显示同意屏幕。 我可以指定用户拒绝同意屏幕时的重定向网址,这样我就可以向他显示自定义消息了吗?

我的代码是:

$client = new Google_Client();
$client->setAuthConfig(Config::$GOOGLE_API_AUTH_CONFIG_JSON);
$client->addScope(Google_Service_Oauth2::USERINFO_EMAIL);
$client->setRedirectUri(Config::$WEBSITE_URL . 'index.php?page=login&action=login');
if (!isset($_GET['code'])) {
    $auth_url = $client->createAuthUrl();
    header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else {
    $client->authenticate($_GET['code']);
    $oauth2_service = new Google_Service_Oauth2($client);
    UserLogin::get()->signupOrLogin($oauth2_service->userinfo->get()->getEmail());
}

1 个答案:

答案 0 :(得分:3)

您无法指定其他网址,但可以在代码中检查查询字符串上的“error”参数 - 如果用户取消,则会设置此参数。详细了解如何处理https://developers.google.com/accounts/docs/OAuth2WebServer#handlingtheresponse

上的回复