2016-09-05 2 views
1

처음 로그인 할 때 제대로 작동하는이 코드가 있지만 페이지가 열린 상태에서 세션을 로그 아웃하지 않으면 다음 메시지가 표시되고 브라우저를 다시 시작해야합니다 내 페이지를 다시 올리려고. 토큰을 자동으로 새로 고칠 수 있습니까?Google OAuth Token Expires

Fatal error: Uncaught exception 'Google_Auth_Exception' with message 'The OAuth 2.0 access token has expired, and a refresh token is not available. Refresh tokens are not returned for responses that were auto-approved.'...

이 코드

<?php 

require_once __DIR__.'/gplus-lib/vendor/autoload.php'; 

const CLIENT_ID = 'CLIENT_ID'; 
const CLIENT_SECRET = 'CLIENT_SECRET'; 
const REDIRECT_URI = 'REDIRECT_URI'; 

session_start(); 

$client = new Google_Client(); 
$client->setClientId(CLIENT_ID); 
$client->setClientSecret(CLIENT_SECRET); 
$client->setRedirectUri(REDIRECT_URI); 
$client->setScopes('email'); 

$plus = new Google_Service_Plus($client); 

if (isset($_REQUEST['logout'])) { 
    session_unset(); 
} 

if (isset($_GET['code'])) { 
    $client->authenticate($_GET['code']); 
    $_SESSION['access_token'] = $client->getAccessToken(); 
    $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; 
    header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL)); 
} 

if (isset($_SESSION['access_token']) && $_SESSION['access_token']) { 
    $client->setAccessToken($_SESSION['access_token']); 
    $me = $plus->people->get('me'); 

    // Get User data 
    $id = $me['id']; 
    $name = $me['displayName']; 
    $email = $me['emails'][0]['value']; 
    $profile_image_url = $me['image']['url']; 

} else { 
    // get the login url 
    $authUrl = $client->createAuthUrl(); 
} 

?> 
<div> 
if (isset($authUrl)) { 
    echo "<a class='login' href='" . $authUrl . "'><img src='gplus-lib/signin_button.png' height='50px'/></a>"; 
} else { ?> 

<!-- Some HTML --> 

<?php 
} 
?> 
</div> 

답변

0

당신은 'Google_Auth_Exception'오류를 캡처하고 새로운 토큰을 생성 할수 없어 무엇입니까?

로그인하기 전에 해당 토큰으로 서비스 및 Google에 전화를 걸면이 토큰이 만료되었는지 여부를 알 수 있고 호출에 실패하면 새 토큰을 생성 할 수 있습니다.

0

이것은 인증 코드 흐름입니다. 승인 코드를 요청하는 동안 Offline_Access 소유권 주장을 요청해야합니다.

해당 소유권 주장을 사용하면 액세스 토큰과 함께 새로 고침 토큰을 받게됩니다. 그런 다음 새로 고침 토큰을 사용하면 액세스 토큰이 만료 될 때마다 새 토큰을 가져올 수 있습니다.

그래서 새로 고침 토큰을 제공 할 다른 범위를 요청해야하는 auth_Code를 요청할 수 있습니다.

테스트를 위해 우편 배달부 앱을 사용할 수 있습니다. 그러면 다양한 범위를 요구하면서 어떤 정보가 수신되는지에 대한 아이디어를 얻을 수 있습니다.