6

ここにあるクライアントライブラリを使用して、次のPHPコードを実行しています:https://code.google.com/p/google-api-php-client/。私はこのコードのいずれかのエラーを取得しませんが、getAccessToken()を呼び出すと、nullを返します。サービスアカウントを使用して、getAccessToken()がnullを返す

私は自分のカレンダーでこのサービスアカウントにアクセスでき、APIコンソール経由でプロジェクトに完全にアクセスできました。

アイデア?何らかの理由で

require_once 'google-api-php-client/src/Google_Client.php'; 

const CLIENT_ID = 'blahblahblah'; 
const SERVICE_ACCOUNT_NAME = '[email protected]'; 
const KEY_FILE = 'path/to/privatekey.p12'; 

$google_client = new Google_Client(); // created only to initialized static dependencies 
$client = new Google_OAuth2(); // you really just need Google_OAuth2 

$key = file_get_contents(KEY_FILE); 

$client->setAssertionCredentials(
    new Google_AssertionCredentials(
     SERVICE_ACCOUNT_NAME, 
     array('https://www.googleapis.com/auth/calendar'), 
     $key 
    ) 
); 

var_dump($client->getAccessToken()); 

答えて

3

、これが動作するように見えた:はこの仕事を、なぜ誰かがを説明することができた場合は

require_once 'google-api-php-client/src/Google_Client.php'; 

const CLIENT_ID = 'blahblahblah'; 
const SERVICE_ACCOUNT_NAME = '[email protected]'; 
const KEY_FILE = 'path/to/privatekey.p12'; 
const CALENDAR_SCOPE = "https://www.googleapis.com/auth/calendar"; 

$key = file_get_contents(KEY_FILE); 
$auth = new Google_AssertionCredentials(
    SERVICE_ACCOUNT_NAME, 
    array(CALENDAR_SCOPE), 
    $key 
); 

$client = new Google_Client(); 
$client->setScopes(array(CALENDAR_SCOPE)); 
$client->setAssertionCredentials($auth); 
$client->getAuth()->refreshTokenWithAssertion(); 
$accessToken = $client->getAccessToken(); 

$client->setClientId(CLIENT_ID); 

、この回答を編集したり、コメントしてください!

+0

どちらかわかりません。私はあなたのコードをhttps://gist.github.com/fulldecent/6728257で信用していますが、参考までにsetSCopesの行は不要です –

+1

'' getAccessToken() ''はあなたが呼び出すまで存在しない現在のアクセストークンを返します'' refreshTokenWithAssertion() ''(初期化する)または '' setAccessToken() ''(あなたが使いたい以前のアクセストークンを回復した場合) – tiho

関連する問題