Google+登录服务器端应用无法正常使用

Pet*_*ter 9 javascript php wordpress google-plus

我尝试为wordpress网站添加Google+身份验证.我想要的是:如果用户未在网站上注册,则在Google+中验证后 - 我将其重定向到他输入用户名的页面; 如果用户已经注册 - 它将登录.这里我的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']) {
    }
}
Run Code Online (Sandbox Code Playgroud)

这里是我的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();
}
Run Code Online (Sandbox Code Playgroud)

用户在Google+中成功授权,但在php中我得到了这个:

致命错误:未捕获的异常"Google_AuthException",消息"在获取OAuth2访问令牌时出错,消息:'redirect_uri_mismatch''在/ 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 [内部函数]: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第113行/google-plus/google-api/auth/Google_OAuth2.php

在应用程序设置中指定为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>
Run Code Online (Sandbox Code Playgroud)

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

Ian*_*ber 22

你的代码基本上是正确的,但是我可以看到的一个轻微的怪癖没有记录得很好!您必须将redirectURI设置为postmessage而不是您正在使用的URL.

$client->setRedirectUri('postmessage');
Run Code Online (Sandbox Code Playgroud)

这是因为它与从按钮交换Javascript期间为令牌设置的URI匹配.请查看以下示例代码:https://github.com/googleplus/gplus-quickstart-php/blob/master/signin.php以查看其实际操作.我会确保在文档中添加注释.

  • 我的天啊!!!!我花了8个小时查看代码并试图摆脱这个愚蠢的`redirect_uri_mismatch`错误!尝试编码,没有,http/https,注册新的应用程序,不同的子域.....哦,我的上帝,我刚刚把'postmessage`.谷歌的家伙,请记录下来!!!!! (2认同)