【问题标题】:Google drive Upload returns - (403) Insufficient PermissionGoogle 云端硬盘上传返回 - (403) 权限不足
【发布时间】:2016-01-09 14:32:22
【问题描述】:

我正在尝试使用服务帐户将文件上传到谷歌驱动器,但我收到了类似的错误

(403) 权限不足错误

虽然从驱动器获取文件列表工作正常。

require_once 'google-api-php-client-master/src/Google/autoload.php';
require_once 'google-api-php-client-master/src/Google/Service.php';
session_start();

$client = new Google_Client();
$client->setAuthConfigFile('client_secrets.json');
$service_account_name = '251181790499-1en2vs6jjovga1egu15tphm1m7g44m60@developer.gserviceaccount.com';
$key_file_location = 'key.p12'; //key.p12

$key = file_get_contents($key_file_location);;

if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
    $client->setAccessToken($_SESSION['access_token']);
    //$client->addScope(Google_Service_Drive::DRIVE_METADATA_READONLY);
    //$client->setScopes(array('https://www.googleapis.com/auth/drive'));
    $cred = new Google_Auth_AssertionCredentials(
    $service_account_name,
    array('https://www.googleapis.com/auth/drive'),
    $key);

    $client->setAssertionCredentials($cred);
    $client->setAccessType("offline");

    $file = new Google_Service_Drive_DriveFile();
    $file->setTitle(uniqid().'.jpg');
    $file->setDescription('A test document');
    $file->setMimeType('image/jpeg');

    $data = file_get_contents('a.jpg');

    $service = new Google_Service_Drive($client);
    $createdFile = $service->files->insert($file, array(
          'data' => $data,
          'mimeType' => 'image/jpeg',
          'uploadType' => 'multipart'
        ));

    print_r($createdFile);

} 
else
{
  $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/googledrive/oauth2callback.php';
  header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}

我在论坛中查看了类似的问题并尝试了建议,但似乎没有任何效果。

代码有什么问题吗?

【问题讨论】:

  • 您是否授予服务帐户访问文件的权限?
  • 在权限选项卡下,服务帐户默认具有“可以编辑”权限。我有什么需要做的吗?
  • $key_file_location
  • 我可以试试,但是两个文件(密钥文件和代码所在的文件)是否在同一路径上是否重要?
  • developers.google.com/drive/v2/reference/permissions/insert 然后您可以授予自己对服务帐户驱动器帐户上文件的访问权限。

标签: google-drive-api


【解决方案1】:

将您的 Google 云端硬盘范围更改为

Google_Service_Drive::DRIVE

在 PHP 中

$client->addScope(Google_Service_Drive::DRIVE);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-22
    • 2015-09-09
    • 2015-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多