Php 使用驱动器API时出现权限不足错误
我试图上传文件到谷歌驱动器使用下面的代码Php 使用驱动器API时出现权限不足错误,php,google-drive-api,Php,Google Drive Api,我试图上传文件到谷歌驱动器使用下面的代码 require_once 'google-api-php-client-master/src/google/autoload.php'; session_start(); $client = new Google_Client(); $client->setAuthConfigFile('client_secrets.json'); $client->addScope('https://www.googleapis.com/auth/dr
require_once 'google-api-php-client-master/src/google/autoload.php';
session_start();
$client = new Google_Client();
$client->setAuthConfigFile('client_secrets.json');
$client->addScope('https://www.googleapis.com/auth/drive');
if (isset($_SESSION['access_token']) && $_SESSION['access_token'])
{
$client->setAccessToken($_SESSION['access_token']);
/*$drive_service = new Google_Service_Drive($client);
$files_list = $drive_service->files->listFiles(array())->getItems();
echo json_encode($files_list);*/
$service = new Google_Service_Drive($client);
$file = new Google_Service_Drive_DriveFile();
$file->setTitle("test");
$file->setMimeType('application/vnd.google-apps.folder');
$createdFile = $service->files->insert($file, array(
'mimeType' => 'application/vnd.google-apps.folder',
'uploadType' => 'multipart'
));
echo "===".$fileId = $createdFile->id;
}
else {
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/google/oauth2callback.php';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
这是给我的权限不足的错误。
我错过什么了吗
我尝试使用服务帐户,效果很好,使用Google Drive API上传文件需要服务帐户吗?您从哪里获得您的令牌(
$\u会话['access\u token']
)?从具有相同作用域和重定向的OAuth2
机制?第一次$\u会话['access\u token']未设置时,我使用oauth2callback.php中的$client->authenticate($\u GET['code'])来获取访问令牌。我正在使用listFiles方法从gdrive获取列表,但无法插入,那么它可能与不同的作用域有关。您是否在接收令牌时定义了其他作用域,然后在尝试插入文件时定义了其他作用域?