我已经在服务类型凭证在谷歌控制台中创建项目。我已经下载P12密钥文件,然后写示例代码插入文件来测试它:如何从项目中浏览文件谷歌驱动器
<?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);
?>
它转储Google_DriveFile对象有许多的URL。其中一些,如'downloadUrl',返回401未授权错误。当我尝试'alternateLink'时,它显示带有我需要访问的信息的Google驱动器页面,以及请求访问和切换帐户的按钮。
它还表示我当前登录了我的帐户,这是用来创建项目的工具,并且可以在Google控制台页面的项目的权限页面上看到。
如何访问属于我在脚本中使用的项目的驱动器?