2017-01-24 5 views
0

PHPクライアントライブラリからGMail APIを使用するために自分のサーバー上にアクセストークンを取得しようとしていますが、変数var_drumpの後にNULLを取得するとアクセストークンin getAccessToken();メソッドを使用します。Google OAuthアクセストークンがリフレッシュトークンでないNULL

私はアクセストークンをどのようにすることができるように間違っていますか?

私はcodeパラメータでURLに有効な認証コードを持っていますが、なぜアクセストークンを取得しようとするとnullになるのかわかりません。何か案は?ここで

が私のコードです:

require_once 'vendor/autoload.php'; 
$redirect_uri = 'https://website.com/m/?mail=tokened'; 
$client = new Google_Client(); 
$client->setAuthConfig('client_secrets.json'); 
$client->setRedirectUri($redirect_uri); 
$client->setAccessType('offline'); 
$client->setApprovalPrompt('force'); 
$client->authenticate($_GET['code']); 
$access_token = $client->getAccessToken(); 
var_dump($access_token); 

私はさらにGoogleは、この発見サーチし:Google API - request for token from Oauth2 returns null token

をし、これがエラーなしで実行しますものですので、私はそのコードに基づいて、以下を試してみましたが、正確に何をしていません私はまだNULLを取得しています。

今回は、認証コードを実行してサーバー側でアクセストークンをすべて取得しようとしましたが、唯一の違いは今回はアクセス許可を求めるccess Gmailデータ。

require_once 'vendor/autoload.php'; 
$client = new Google_Client(); 
$client->setAuthConfig('client_secrets.json'); 
$client->setScopes('https://mail.google.com'); 
if($_GET['mail']=='approved'){ 
    $client->setRedirectUri('https://website.com/m/php/googleTokens.php?mail=tokened'); 
    return header('Location: ' . $client->createAuthUrl()); 
} 
else{ 
    $client->authenticate($_GET['code']); 
    $tokens = $client->getAccessToken(); 
    var_dump($tokens); 
} 

答えて

1

認証フローを正しく実行していることを確認してください。まず、クライアントはGoogleのOAuthシステムに認証リクエストを送信します。その後、Googleがアクセスコードを返します。後でアクセストークンを交換することができます。プロセスのロジックは次のようにする必要があります:

require_once 'vendor/autoload.php'; //Include PHP Client Library 

//Create client object and set its configuration 
$client = new Google_Client(); 
$client->setAuthConfig('client_secrets.json'); 
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/index.php'); 
$client->setAccessType('offline'); 
$client->setApprovalPrompt('force'); 
$client->addScope(array("email", "profile")); 

//Check if the access token is already set and if it is, var dump access token 
if(isset($_SESSION["access_token"]) && $_SESSION["access_token"]) { 

    $client->setAccessToken($_SESSION['access_token']); 

    var_dump($_SESSION['access_token']); 

} else { // if access token is not set, authenticate client 

    if(!isset($_GET["code"])) { // if there is no access code 

    $auth_url = $client->createAuthUrl(); 
    header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL)); 

    } else { //if there is an access code 

    $client->authenticate($_GET['code']); //authenticate client 
    $_SESSION['access_token'] = $client->getAccessToken(); //save access token to session 
    $redirect_uri = "http://".$_SERVER['HTTP_HOST']."/index.php"; 
    header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL)); 

    } 
} 

ロジックを実行する前に、myaccount.google.com/permissionsに移動し、アプリケーションを削除し、その後、上記のコードを実行してください。最後に、より詳細な説明についてはofficial documentationを確認してください。また、ここではstackoverflowのいくつかの例がありますので、私もそれらをチェックすることをお勧めします。私はこれが助けて欲しい!

関連する問題