2013-03-27 7 views
9

나는 WordPress 용 Google+ 인증을 추가하려고합니다. 내가 원하는 것 : 사용자가 사이트에 등록되지 않은 경우 Google+에서 인증 한 후 사용자 이름을 입력 한 페이지로 리디렉션합니다. 사용자가 이미 등록 된 경우 - 그것은에 기록됩니다 여기 내 JS 코드 :서버 측 앱용 Google+ 로그인이 작동하지 않습니다.

여기
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에서 나는이있어 :

.

치명적 오류 : 'GoogleAutExceptionException'오류 메시지 : 'OAuth2 액세스 토큰을 가져 오는 중에 오류가 발생했습니다.'메시지 :/var/www/html/v4/wp-content/plugins/social/google-plus/google-api의 'redirect_uri_mismatch' /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 (배열, '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/google-plus/google-api/auth/Google_OAuth2.php on line 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

+0

[Google+ 로그인 버튼] (https://developers.google.com/+/web/signin/)을 사용 중이거나 직접 흐름을 시작하고 있습니까? 이 흐름을 트리거하는 방법을 보여주는 프런트 엔드 코드를 게시하십시오. Google에 전달되는 구성 매개 변수를 확인해야합니다. (클라이언트 ID를 숨기고 또한 귀하의 SOCIAL_GPLUS_REDIRECT_URIS에 어떤 값이 있는지 PHP 사이드에서 확인해야합니다. – BrettJ

+0

Google + 로그인 버튼을 사용하고 있습니다. ' – Peter

+0

SOCIAL_GPLUS_REDIRECT_URIS는 http://example.com/wp-admin/admin-ajax.php?action=login_gplus – Peter

답변

22

하지만, 내가 볼 수있는 약간의 특질 거기는 아니다 아주 잘 문서화되었습니다! redirectURI를 사용중인 URL이 아닌 postmessage로 설정해야합니다.

$client->setRedirectUri('postmessage'); 

이것은 자바 스크립트 교환 중에 버튼에서 설정 한 URI와 일치합니다. 샘플 코드를 보시려면 https://github.com/googleplus/gplus-quickstart-php/blob/master/signin.php을보십시오. 문서에 메모를 추가하도록하겠습니다.

+1

는 당신의 도움을 주셔서 감사합니다. 당신은 저를 저장합니다. – Peter

+2

OMG !!!! I 8 시간 동안 코드를 조사하고이 바보 같은'redirect_uri_mismatch' 오류를 없애기 위해 노력하십시오! http/https없이 새로운 앱을 등록하고 다른 하위 도메인을 등록하십시오. 오, 세상에, 나는'postmessage'를 넣었습니다. Google 문서, 문서화하십시오 !!!!! –

+0

감사합니다. Google 문서는 없습니다. ( – Stocki

관련 문제