php – Google登录服务器端应用程序auth不工作

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – Google登录服务器端应用程序auth不工作脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图添加GOOGLE auth for WordPress网站.
我想要的:如果用户没有在网站上注册,在谷歌身份验证后 – 我将他重定向到他输入用户名页面;如果用户已经注册 – 它将被登录.
这里我的js代码
function doGooglePlusLogin(authresult) {
    if (authResult['code']) {
        jquery('#signinButton').attr('style','display: none');
        jQuery.ajax({
            url: '<?PHP echo sITe_url(); ?>/wp-admin/admin-ajax.PHP',tyPE: 'get',dataType: 'json',data: {
                action: 'LOGin_gplus',code: authResult['code']
            },success: function(result) {
            },});
    } else if (authResult['error']) {
    }
}

这里我的PHP代码

function login_gplus() {
$response = array();

if (isset($_GET['code']) && !empty($_GET['code'])) {
    @session_start();
    $client = new Google_Client();
    $client->setApplicationName('test');
    $client->setAccessType('offline');
    $client->setClientId(get_option(SOCIAL_GPLUS_CLIENT_ID));
    $client->setClientSecret(get_option(SOCIAL_GPLUS_CLIENT_SECRET));
    $client->setDeveloperKey(get_option(SOCIAL_GPLUS_API_KEY));
    $client->setredirectUri(get_option(SOCIAL_GPLUS_REDIRECT_URIS));
    $client->setApPRovalPrompt('auto');

    $code = $_GET['code'];
    $client->authenticate($code);

    $token = json_decode($client->getAccessToken());
    $reqUrl = 'https://www.googleapis.COM/oauth2/v1/tokeninfo?access_token=' . $token->access_token;
    $req = new Google_HttpRequest($reqUrl);

    $tokenInfo = json_decode(
            $client->getIo()
                    ->authenticatedRequest($req)
                    ->getResponseBody());

    if ($tokenInfo->error) {
        $response['test'] = $tokenInfo->error;
        send_json_response($response);
        die();
    }
    if ($tokenInfo->audience != get_option(SOCIAL_GPLUS_CLIENT_ID)) {
        $response['test'] = "Token's client ID does not match app's.";
        send_json_response($response);
        die();
    }
    $response['test'] = 'Succesfully connected with token: ' . print_r($token,true);
}
send_json_response($response);
die();
}

用户在Google中成功授权,但在PHP中我得到:

致命的错误:未知的异常“Google_AuthException”与消息’提取OAuth2访问令牌错误,消息:’redirect_uri_mismatch”in / VAR / www / htML / v4 / wp-content / plugins / social / google-plus / google-api / auth /Google_OAuth2.PHP:113堆栈跟踪:#0 /var/www/html/v4/wp-content/plugins/social/google-plus/google-api/Google_Client.PHP(131):Google_OAuth2-> authenticate(Array,‘4 / ScmpTqeiWt0SJ …’)#1 /var/www/html/v4/wp-content/plugins/social/google-plus/functions.PHP(35):Google_Client-> authenticate(‘4 / ScmpTqEIWt0SJ. #2 [internal function]:login_gplus(”)#3 /var/www/html/v4/wp-includes/plugin.PHP(406):call_user_func_array(‘login_gplus’,Array)#4 / var /www/html/v4/wp-admin/admin-ajax.PHP(74):do_action(‘wp_ajax_nopriv _…’)#5 {main}抛入/ var / www / html / v4 / wp-content / plugins /social/google-plus/google-api/auth/Google_OAuth2.PHP第113行

在应用程序设置中重定向指定为http://example.com/wp-admin/admin-ajax.php的URI.
我做错了什么

编辑:

Google登录按钮定义:

<span id="signinButton">
  <span class="g-signin"
   data-callback="doGooglePlusLogin"
   data-clientid="<?PHP echo $this->gplus_client_id; ?>"
   data-cookiepolicy="single_host_origin" data-accesstype="offline"
   data-requestvisibleactions="http://schemas.google.com/AddActivity"
   data-scope="https://www.googleapis.com/auth/plus.login">
  </span>
</span>

SOCIAL_GPLUS_REDIRECT_URIS是example.com/wp-admin/admin-ajax.PHP?action=login_gplus

你的代码基本上是对的,但有一点点奇怪,我看不到文件记录非常好!您必须将redirectURI设置为postmessage而不是您使用的URL.
$client->setRedirectUri('postmessage');

这是因为它与在按钮的Javascript交换期间的令牌URI设置相匹配.看看示例代码https://github.com/googleplus/gplus-quickstart-php/blob/master/signin.php,看看它在行动.我将确保我们在文档中添加一个注释.

脚本宝典总结

以上是脚本宝典为你收集整理的php – Google登录服务器端应用程序auth不工作全部内容,希望文章能够帮你解决php – Google登录服务器端应用程序auth不工作所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。