2013-03-06 12 views
5

मैं google-api-php-client 0.6.1 का उपयोग कर रहा हूं और मैं जानना चाहता हूं कि सेवा खाते के साथ ठोस उपयोगकर्ता का प्रतिरूपण करने का कोई तरीका है? मेरे एप्लिकेशन को कुछ फ़ाइलों को अपने Google ड्राइव में स्टोर करने की आवश्यकता है। इसलिए, मैंने उपयोगकर्ता सेवा खाते और .p12 कुंजी - प्रमाणीकरण का निर्णय लिया है। यह बहुत अच्छा काम करता है, लेकिन सभी फाइलें सेवा खाते में संग्रहीत की जा रही हैं, इसलिए मैं उन्हें प्रबंधित नहीं कर सकता। मैं दस्तावेजों को कुछ निश्चित खाते में संग्रहीत करना चाहता हूं (जो एपीआई प्रोजेक्ट और सेवा खाता स्वयं बनाने के लिए उपयोग कर रहा था)। मैं इस कोड का उपयोग करने के लिए कोशिश कर रहा था:एक सेवा खाते के साथ 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

मैं एक ही समस्या का सामना और ऐसा लगता है कि मैं गूगल विन्यास अपने आप में कुछ परिवर्तन करना चाहिए। क्या आपको समाधान मिला है? क्या आप मुझे बता सकते हैं कि मुझे क्या बदलना चाहिए? – Sergei

उत्तर

1

मत उपयोगकर्ता $ auth- > prn, $ auth-> उप का उपयोग करें। यह मेरे लिए काम करता है:

// 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(); 
संबंधित मुद्दे