source

서버 측 앱 인증에 대한 Google+ 로그인 기능이 작동하지 않습니다.

nicesource 2023. 2. 10. 22:04
반응형

서버 측 앱 인증에 대한 Google+ 로그인 기능이 작동하지 않습니다.

워드프레스 사이트에 구글+ auth를 추가하려고 합니다.원하는 것: 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']) {
    }
}

여기 내 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' 메시지와 함께 /var/www/html/v4/wp-content/plugins/social/google-plus/google-api/auth/Google_mismatch 메시지: 'redirect_uri_match'가 표시됩니다.OAuth2.php:113Stack 트레이스:#0 /var/ww/html/v4/wp-content/plugins/social/google-plus/google-api/Google_Client.php(131):구글_OAuth2 -> 인증(어레이, '4/ScmpTq)EIWT0SJ...'#1 /var/www/html/v4/wp-contents/plugins/social/google-plus/functions.php(35): Google_Client->authenticate('4/ScmpTq)EIWT0SJ...'#2 [내부 함수]: login_gplus("")#3 /var/www/html/v4/wp-includes/plugin.php(406) : call_user_func_array('login_gplus', Array) #4 /var/ww/html/v4/wp-admin/admin-ajax.php(74) : do_action('wp_ajax_nopriv_'...') #5 {main}이 /var/wwww/ww/ww/wp-google4/plugoogle4/plugoogle4/plus/plugoogle4/plus}에 삽입되어 있습니다.phOAuth2.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

네 코드는 기본적으로 맞지만, 내가 보기에 별로 문서화되어 있지 않은 약간의 이상한 점이 있어!리다이렉트를 설정해야 합니다.사용 중인 URL이 아닌 포스트 메시지 URI.

$client->setRedirectUri('postmessage');

버튼에서 Javascript를 교환할 때 토큰에 설정된 URI와 일치합니다.샘플 코드는, https://github.com/googleplus/gplus-quickstart-php/blob/master/signin.php 에서 참조해 주세요.서류에 메모를 꼭 추가하도록 하겠습니다.

언급URL : https://stackoverflow.com/questions/15664950/google-sign-in-for-server-side-apps-auth-not-working

반응형