2017-10-07 3 views
1

사용자 시스템에 google api를 사용하는 웹 응용 프로그램을 만들려고합니다. 여기 google 현재 사용자가 경로를 따라 계속 로그인

$app->get('/home', function() use($client,$app){ 
    if(isset($_GET['code'])){ 
     $client->authenticate($_GET['code']); 
     $_SESSION['token'] = $client->getAccessToken(); 
    } 
    if(!isset($_SESSION['token'])){ 
     $url = $client->createAuthUrl(); 
     $output = '<a href="'.$url.'">Se connecter </a>'; 
    } else { 
     $client->setAccessToken($_SESSION['token']); 
     $token = json_decode($_SESSION['token']['access_token']); 
     $output = ""; 
     require "../view/planning.php"; 
    } 

    return $output; 
}); 

내 $ 클라이언트입니다 :

사실, 난 단 하나 개의 경로로 내 구글 계정을 연결할 수 있습니다, 여기있다

$client = new Google_Client(); 
$client->setApplicationName("Application de test"); 
$client->setClientId(MY_CLIENT_ID); 
$client->setClientSecret(MY_CLIENT_SECRET); 
$client->setScopes('https://www.googleapis.com/auth/calendar.readonly'); 
$client->addScope('https://www.googleapis.com/auth/userinfo.email'); 
$client->setRedirectUri(MY_REDIRECT_URI); 
$client->setAccessType('online'); 

그리고 내 질문은 : 나는 유지할 수있는 방법 전체 응용 프로그램에 현재 클라이언트? 이 경로가 작동하지

$app->get('/accueil', function() use($client){ 
    if(isset($_GET['code'])){ 
     $client->authenticate($_GET['code']); 
     $_SESSION['token'] = $client->getAccessToken(); 
    } 
    if(!isset($_SESSION['token'])){ 
     $url = $client->createAuthUrl(); 
     $output = '<a href="'.$url.'">Se connecter </a>'; 
    } else { 
     $client->setAccessToken($_SESSION['token']); 
     $token = json_decode($_SESSION['token']['access_token']); 
     require ('../view/accueil.php'); 
     $output = ""; 
    } 
    return $output; 
}); 

, 그것은 나에게 로그인 할 수있는 링크를 보여줍니다 :이 경로가있는 경우 exemple 들어 . 애플리케이션이 없기 때문에 애플리케이션이 URL에서 'code'을 얻을 수 없다는 것을 알고 있습니다.

if(isset($_GET['code']) || isset($_SESSION['code'])){ 
    $client->authenticate($_GET['code']); 
    $_SESSION['code'] = $_GET['code']; 
    $_SESSION['token'] = $client->getAccessToken(); 
} 

은 어떻게 로그인 한 사용자를 유지할 수 있습니다 :

나는이 같은 'code'을 유지하려고?

+0

세션에''code ''를 넣으십시오. – mega6382

+0

다른 경로에 액세스 할 때 $ _SESSION 변수가 존재하지 않는 것처럼 보입니다. – AdrienG

답변

0

글쎄, 긴 밤 후에, 나는 해결책을 찾는다.

127.0.0.1의 리디렉션이 localhost로 리디렉션하는 것과 다른 것처럼 보이므로 세션 변수가 재설정됩니다.

관련 문제