douchen5971 2015-05-29 14:23
浏览 37
已采纳

如何浏览项目谷歌驱动器中的文件

i have created project in google console with service type credentials. I have downloaded p12 key file, and then wrote sample code for inserting file to 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 with many urls. Some of them, like 'downloadUrl', returns 401 Unauthorized error. When I tried 'alternateLink' it showed google drive page with information that i need access, and buttons to request access and to switch account.

It also said that Im currently logged on my account, that was beeing used to create project, and that is visible on Permissions page of the project on the google console page.

How can I access drive that belongs to the project I used in the script ?

  • 写回答

1条回答 默认 最新

  • dongqi6964 2015-06-02 08:31
    关注

    Finally found solution, it was missing permissions on file. Its worth to note that passing permission object to the $file object (before insertion) didn't work. I had to insert permission by passing already created file ID and permission object to the $service->permissions->insert method, just like that:

    $permission = new Google_Permission();
    $permission->setValue('PERSONAL.ACCOUNT@gmail.com');
    $permission->setType('user');
    $permission->setRole('writer');
    $service->permissions->insert($createdFile->getId(), $permission);
    

    Still I wasn't able to actually edit this file. When I opened file with URL

    $createdFile->getAlternateLink();
    

    Then I had to reopen that file with google docs. That copied file to my personal account and then I could only edit only that copy of a file, not a base file itself.

    I dont know if there is a way to make that files truly editable, but I moved to the dropbox, as google drive API and its documentation SUX.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能