php - 使用 PHP 创建文件夹 Google Drive API
问题描述
最近我在 google 驱动器上建立了一个自动文件创建系统,我已经将 API 集成到我的 symfony 应用程序中,我设法对自己进行身份验证等,但我没有阻止创建文件的步骤。
我有这个错误发生:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "insufficientPermissions",
"message": "Insufficient Permission: Request had insufficient authentication scopes."
}
],
"code": 403,
"message": "Insufficient Permission: Request had insufficient authentication scopes."
}
}
我真的不明白这是从哪里来的,因为我知道我已经设置了文档中指示的范围: $client->addScope(Google_Service_Drive::DRIVE);
我的功能:
public function createFolder($name)
{
$client = $this->getClient();
// $client->setAuthConfig('code_secret_client.json');
$client->addScope(Google_Service_Drive::DRIVE);
$client->setAccessToken('ya29.a0AfH6SMDO-L_7Lp6YWbGtSqWdPHJKCNezQr8-RRgS8xslHInLApC9uxBJVVljPTKEBgR-iidqIorjNaBThU4-aPjuAth1aD7mzjJXn5n2xP1xPw40p_OLssC7Ttj8CpBJHuqsUm89CYWAGL8cCHGhQ2hBY0YjKQ');
$service = new Google_Service_Drive($client);
$folder = new Google_Service_Drive_DriveFile();
$folder->setName($name);
$folder->setMimeType('application/vnd.google-apps.folder');
$result = $service->files->create($folder);
dump($result);
return $result;
}
你有什么想法 ?
解决方案
我能够通过谷歌示例重现
<?php
/**
* Copyright 2018 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// [START drive_quickstart]
require __DIR__ . '/vendor/autoload.php';
if (php_sapi_name() != 'cli') {
throw new Exception('This application must be run on the command line.');
}
/**
* Returns an authorized API client.
* @return Google_Client the authorized client object
*/
function getClient()
{
$client = new Google_Client();
$client->setApplicationName('Google Drive API PHP Quickstart');
$client->setScopes(Google_Service_Drive::DRIVE);
$client->setAuthConfig('credentials.json');
$client->setAccessType('offline');
$client->setPrompt('select_account consent');
// Load previously authorized token from a file, if it exists.
// The file token.json stores the user's access and refresh tokens, and is
// created automatically when the authorization flow completes for the first
// time.
$tokenPath = 'token.json';
if (file_exists($tokenPath)) {
$accessToken = json_decode(file_get_contents($tokenPath), true);
$client->setAccessToken($accessToken);
}
// If there is no previous token or it's expired.
if ($client->isAccessTokenExpired()) {
// Refresh the token if possible, else fetch a new one.
if ($client->getRefreshToken()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
} else {
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
printf("Open the following link in your browser:\n%s\n", $authUrl);
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
$client->setAccessToken($accessToken);
// Check to see if there was an error.
if (array_key_exists('error', $accessToken)) {
throw new Exception(join(', ', $accessToken));
}
}
// Save the token to a file.
if (!file_exists(dirname($tokenPath))) {
mkdir(dirname($tokenPath), 0700, true);
}
file_put_contents($tokenPath, json_encode($client->getAccessToken()));
}
return $client;
}
$name = "Stackoverflow";
// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Drive($client);
$folder = new Google_Service_Drive_DriveFile();
$folder->setName($name);
$folder->setMimeType('application/vnd.google-apps.folder');
$result = $service->files->create($folder);
// [END drive_quickstart]
同样的错误。然后我检查了 token.json,在“范围”它是只读的。我已删除该令牌并再次请求确保它具有“查看、编辑、创建和删除您的所有 Google Drive 文件”选项,以便在授予权限时进行选择,它会起作用。
这条线是 createFolder 的问题。
$client->setAccessToken('ya29.a0AfH6SMDO-L_7Lp6YWbGtSqWdPHJKCNezQr8-RRgS8xslHInLApC9uxBJVVljPTKEBgR-iidqIorjNaBThU4-aPjuAth1aD7mzjJXn5n2xP1xPw40p_OLssC7Ttj8CpBJHuqsUm89CYWAGL8cCHGhQ2hBY0YjKQ');
推荐阅读
- wxpython - wxPython 在 wx.ScrolledWindow 中添加大量图片
- .htaccess - 用 htaccess 重写多个 url
- branch.io - Sendgrid 集成:如何在与 CNAME 重定向相同的域上托管 AASA 文件
- c# - 如何使用 AutoMapper 将配方与成分映射
- dart - Flutter http错误类型'_InternalLinkedHashMap
' 不是类型 'Map 的子类型 ' - mysql - mysql服务器不见了
- java - 尽管没有显示错误,Java Array 元素仍未插入 MySQL 表
- python - 检查类是否在类中 - Python
- python - 使用 Python 打印和连接 Json 的元素
- python - Python子进程:进程终止后获取所有标准输出数据