php - How to browse files from projects google drive -
i have created project in google console service type credentials. have downloaded p12 key file, , wrote sample code inserting file test it:
<?php require_once 'google-api-php-client/src/google_client.php'; require_once 'google-api-php-client/src/contrib/google_driveservice.php'; require_once "google-api-php-client/src/contrib/google_oauth2service.php"; $scopes = array('https://www.googleapis.com/auth/drive'); $accountemail = 'secret'; $accountp12filepath = './key.p12'; $key = file_get_contents($accountp12filepath); $auth = new google_assertioncredentials($accountemail, $scopes, $key); $client = new google_client(); $client->setuseobjects(true); $client->setassertioncredentials($auth); $service = new google_driveservice($client); $file = new google_drivefile(); $file->settitle('test title'); $file->setdescription('test description'); $parent = new google_parentreference(); $file->setparents(array($parent)); $createdfile = $service->files->insert($file, array( 'data' => 'test data', 'mimetype' => 'text/plain' )); var_dump($createdfile); ?>
it dumps google_drivefile object many urls. of them, 'downloadurl', returns 401 unauthorized error. when tried 'alternatelink' showed google drive page information need access, , buttons request access , switch account.
it said im logged on account, beeing used create project, , visible on permissions page of project on google console page.
how can access drive belongs project used in script ?
finally found solution, missing permissions on file. worth note passing permission object $file object (before insertion) didn't work. had insert permission passing created file id , permission object $service->permissions->insert method, that:
$permission = new google_permission(); $permission->setvalue('personal.account@gmail.com'); $permission->settype('user'); $permission->setrole('writer'); $service->permissions->insert($createdfile->getid(), $permission);
still wasn't able edit file. when opened file url
$createdfile->getalternatelink();
then had reopen file google docs. copied file personal account , edit copy of file, not base file itself.
i dont know if there way make files editable, i moved dropbox, google drive api , documentation sux.
Comments
Post a Comment