2013-03-06 18 views
5

私はgoogle-api-php-client 0.6.1を使用しています。具体的なユーザーをサービスアカウントで偽装する方法はありますか?私のアプリケーションは、Googleのドライブにいくつかのファイルを保存する必要があります。だから、私はユーザーサービスアカウントと.p12鍵認証に決めました。それは素晴らしいですが、すべてのファイルはサービスアカウントに格納されているので、私はそれらを管理することはできません。ドキュメントを特定のアカウント(apiプロジェクトとサービスアカウント自体を作成するために使用していた)に保存したいと思います。私はこのコードを使用しようとしていた。サービスアカウントを持つGoogleのユーザーを偽装

$KEY_FILE = <p12 key file path>; 
$key = file_get_contents($KEY_FILE); 
$auth = new Google_AssertionCredentials(
     $SERVICE_ACCOUNT_NAME, 
     array('https://www.googleapis.com/auth/drive'), 
     $key); 
$auth->prn = '<[email protected]>'; 
$client = new Google_Client(); 
$client->setUseObjects(true); 
$client->setAssertionCredentials($auth); 
return new Google_DriveService($client); 

を私は得た「のOAuth2トークンをリフレッシュエラーメッセージ: 『{『エラー』:『ACCESS_DENIED』}』」

+0

同じ問題が発生し、Google設定自体にいくつか変更を加える必要があるようです。あなたは解決策を見つけましたか?私は何を変えなければならないと私に指摘できますか? – Sergei

答えて

1

ないでユーザーの$ auth- > prn、$ auth-> subを使います。これは私のために働きます:

// Create a new google client. We need this for all API access. 
$client = new Google_Client(); 
$client->setApplicationName("Google Group Test"); 

$client_id = '...'; 
$service_account_name = '...'; 
$key_file_location = '...'; 

if (isset($_SESSION['service_token'])) { 
    $client->setAccessToken($_SESSION['service_token']); 
} 
$key = file_get_contents($key_file_location); 

// https://www.googleapis.com/auth/admin.directory.group, 
// https://www.googleapis.com/auth/admin.directory.group.readonly, 
// https://www.googleapis.com/auth/admin.directory.group.member, 
// https://www.googleapis.com/auth/admin.directory.group.member.readonly, 
// https://www.googleapis.com/auth/apps.groups.settings, 
// https://www.googleapis.com/auth/books 
$cred = new Google_Auth_AssertionCredentials(
    $service_account_name, 
     array(
      Google_Service_Groupssettings::APPS_GROUPS_SETTINGS, 
      Google_Service_Directory::ADMIN_DIRECTORY_GROUP, 
      Google_Service_Directory::ADMIN_DIRECTORY_GROUP_READONLY, 

      Google_Service_Directory::ADMIN_DIRECTORY_GROUP_MEMBER, 
      Google_Service_Directory::ADMIN_DIRECTORY_GROUP_MEMBER_READONLY, 

      Google_Service_Books::BOOKS, 
     ), 
     $key, 
     'notasecret' 
    ); 
// 
// Very important step: the service account must also declare the 
// identity (via email address) of a user with admin priviledges that 
// it would like to masquerade as. 
// 
// See: http://stackoverflow.com/questions/22772725/trouble-making-authenticated-calls-to-google-api-via-oauth 
// 
$cred->sub = '...'; 
$client->setAssertionCredentials($cred); 
if ($client->getAuth()->isAccessTokenExpired()) { 
    $client->getAuth()->refreshTokenWithAssertion($cred); 
} 
$_SESSION['service_token'] = $client->getAccessToken(); 
関連する問題