gpt4 book ai didi

php - 我看不到使用 php 在我的 Google 云端硬盘中通过 api 创建的文件和文件夹

转载 作者:可可西里 更新时间:2023-10-31 22:14:46 27 4
gpt4 key购买 nike

我正在尝试使用 google drive api 创建文件夹。我能够在最后获得文件 ID。但我无法在谷歌驱动器中找到我的文件夹。好像是权限问题?

$scopes = array(
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/drive.appdata','https://www.googleapis.com/auth/drive.apps.readonly',
'https://www.googleapis.com/auth/drive.metadata','https://www.googleapis.com/auth/drive.file',
'https://www.googleapis.com/auth/drive.readonly','https://www.googleapis.com/auth/drive.photos.readonly'
);
$creds = new Google_Auth_AssertionCredentials(
$serviceAccountName,
$scopes,
file_get_contents($keyFile)
);
$client = new Google_Client();
$client->setApplicationName($appName);
$client->setClientId($clientId);
$client->setAssertionCredentials($creds);
$service = new Google_Service_Drive($client);
$fileMetadata = new Google_Service_Drive_DriveFile(array(
'name' => 'Invoices',
'permissions' => array('type'=>'anyone','role'=>'reader','allowFileDiscovery'=>1),

'mimeType' => 'application/vnd.google-apps.folder'));
$file = $service->files->create($fileMetadata, array());

printf("File ID: %s\n", $file->id);

最佳答案

正在创建的文件属于 API 帐户电子邮件(类似于 api-service-account@yourproject.iam.gserviceaccount.com。如果您转到驱动器 URL:https://docs.google.com/document/d/{ID},你会得到一个“Permission Denied, request access”页面。

服务帐户电子邮件与您的用户电子邮件无关。

为了与您的用户一起创建文件,您需要创建一个 OAauth token 授权。

  1. 按照 this page 上的“第 1 步:打开 Drive API”步骤创建 OAuth 凭证

  2. 按照 this page 上的示例修改您的脚本,你可以有:


use Google_Client;
use Google_Service_Drive;
use Google_Service_Drive_DriveFile;

...

$file = 'root/to/your/credentials.json';

$client = new Google_Client();
$client->setScopes([
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/drive.appdata',
'https://www.googleapis.com/auth/drive.apps.readonly',
'https://www.googleapis.com/auth/drive.metadata',
'https://www.googleapis.com/auth/drive.file',
'https://www.googleapis.com/auth/drive.readonly',
'https://www.googleapis.com/auth/drive.photos.readonly'
]);

$client->setAuthConfig($file);
$authUrl = $client->createAuthUrl();

printf("Open the following link in your browser:\n%s\n", $authUrl);

// You will need to open this url
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));

$accessToken = $client->authenticate($authCode);

file_put_contents($credentialsPath, $accessToken);
printf("Credentials saved to %s\n", $credentialsPath);

$client->setAccessToken(json_decode($accessToken, true));

$service = new Google_Service_Drive($client);

$metadata = new Google_Service_Drive_DriveFile([
'name' => 'testing drive',
'mimeType' => "application/vnd.google-apps.document"
]);
$file = $service->files->create($metadata, []);
print_r($file);

该文件应位于您的云端硬盘主目录中。

对了,我建议你试试新版本google/apiclient:2.0.2 (仍处于测试阶段,但工作正常)。

关于php - 我看不到使用 php 在我的 Google 云端硬盘中通过 api 创建的文件和文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38614941/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com