Google PHP Client API: Insufficient Permission -
i have php code use google php client api use drive service.
<?php require_once 'google/client.php'; require_once 'google/service/drive.php'; const client_id = '[myclientid]'; const service_account_name = '[myserviceaccounid]'; // make sure keep key.p12 file in secure location, , isn't // readable others. const key_file = './[myprivatekey].p12'; // load key in pkcs 12 format (you need download // google api console when service account created. $client = new google_client(); $client->setapplicationname("google drive sample"); $key = file_get_contents(key_file); $client->setclientid(client_id); $client->setassertioncredentials(new google_auth_assertioncredentials( service_account_name, array('https://www.googleapis.com/auth/drive'), $key) ); $client->setclientid(client_id); if ($client->getauth()->isaccesstokenexpired()) { $client->getauth()->refreshtokenwithassertion(); } // json encoded access token. $token = $client->getaccesstoken(); echo "<pre>"; $service = new google_service_drive($client); $service->apps->listapps(); echo "</pre>"; ?>
i had client_id, service_account_name , key_file correctly installed. when run code following error messages:
fatal error: uncaught exception 'google_service_exception' message 'error calling https://www.googleapis.com/drive/v2/apps: (403) insufficient permission' in c:\xampp\htdocs\oauth\google\http\rest.php:79 stack trace: #0 c:\xampp\htdocs\oauth\google\http\rest.php(44): google_http_rest::decodehttpresponse(object(google_http_request)) #1 c:\xampp\htdocs\oauth\google\client.php(499): google_http_rest::execute(object(google_client), object(google_http_request)) #2 c:\xampp\htdocs\oauth\google\service\resource.php(195): google_client->execute(object(google_http_request)) #3 c:\xampp\htdocs\oauth\google\service\drive.php(1281): google_service_resource->call('list', array, 'google_service_...') #4 c:\xampp\htdocs\oauth\drive_client.php(41): google_service_drive_apps_resource->listapps() #5 {main} thrown in c:\xampp\htdocs\oauth\google\http\rest.php on line 79
what may wrong code?
the apps#list method requires authorization scope https://www.googleapis.com/auth/drive.apps.readonly
. add array of scopes:
$client->setassertioncredentials(new google_auth_assertioncredentials( service_account_name, array('https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/drive.apps.readonly'), $key) );
Comments
Post a Comment