Reputation: 368
i am trying to upload file to drive using service account. i am getting the following error saying Insufficient permissions.
Error calling POST https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart: (403) Insufficient Permission'
here is my total code.
$client_id = '69649809374-ib5c5qmr7n34adsfsdfsxxxifss95ue7qouplh7v3r.apps.googleusercontent.com'; //Client ID
$service_account_name = '269649809374-ib5c5qmr7n34aifss95ue7qouplh7v3r@developer.gserviceaccount.com'; //Email Address
$key_file_location = 'secretkey.p12'; //key.p12
echo pageHeader("Service Account Access");
if ($client_id == '269649809374-b5c5qmr7n34aifss95ue7qouplh7v3r.apps.googleusercontent.com'
|| !strlen($service_account_name)
|| !strlen($key_file_location)) {
echo missingServiceAccountDetailsWarning();
}
$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
if (isset($_SESSION['service_token']))
{
$client->setAccessToken($_SESSION['service_token']);
}
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
$service_account_name,
array('https://www.googleapis.com/auth/drive'),
$key);
$client->setAssertionCredentials($cred);
$client->addScope("https://www.googleapis.com/auth/drive");
if ($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
$_SESSION['service_token'] = $client->getAccessToken();
$service = new Google_Service_Drive($client);
$file = new Google_Service_Drive_DriveFile();
$parentId='0B7yersFxlKMTR2RsMThQSG1RUVk';
$parent = new Google_Service_Drive_ParentReference();
// create a file
DEFINE("TESTFILE", 'testfile-small.txt');
if (!file_exists(TESTFILE))
{
$fh = fopen(TESTFILE, 'w');
fseek($fh, 1024 * 1024);
fwrite($fh, "this is sekhar", 1);
fclose($fh);
}
$parent->setId($parentId);
$file->setParents(array($parent));
$file->setTitle("I am service Account");
$result = $service->files->insert($file,array('data' => file_get_contents(TESTFILE),
'mimeType' => 'application/octet-stream',
'uploadType' => 'media'));
is there any wrong with my code.
Upvotes: 0
Views: 1860
Reputation: 116868
403) Insufficient Permission'
Means that your service account doesn't have access to do what you are asking it to do.
The first thing I would check would be that you have correctly given the service account access to the directory in question. Is it one on your drive or is it one that the service account owns? (you give a service account access to a drive folder like you would a normal user take the service account email address and add it, it will need write access.)
Since you aren't doing a list of directories you might also want to double check that you have the correct directory id. Or just use root to test your upload code.
'0B7yersFxlKMTR2RsMThQSG1RUVk'
Upvotes: 2