【问题标题】:Google drive multipart Upload returns - (403) Insufficient PermissionGoogle 驱动器分段上传返回 - (403) 权限不足
【发布时间】:2015-03-04 04:16:53
【问题描述】:

我正在尝试使用服务帐户将文件上传到驱动器。我收到以下错误提示权限不足。

调用 POST https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart 时出错:(403) 权限不足'

这是我的总代码。

$client_id = '69649809374-ib5c5qmr7n34adsfsdfsxxxifss95ue7qouplh7v3r.apps.googleusercontent.com'; //Client ID
$service_account_name = '269649809374-ib5c5qmr7n34aifss95ue7qouplh7v3r@developer.gserviceaccount.com'; //Email Address
$key_file_location = 'secretkey.p12'; //key.p12

echo pageHeader("Service Account Access");
if ($client_id == '269649809374-b5c5qmr7n34aifss95ue7qouplh7v3r.apps.googleusercontent.com'
    || !strlen($service_account_name)
    || !strlen($key_file_location)) {
      echo missingServiceAccountDetailsWarning();
         }

$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");

if (isset($_SESSION['service_token']))
    {
    $client->setAccessToken($_SESSION['service_token']);
    }
$key = file_get_contents($key_file_location);

$cred = new Google_Auth_AssertionCredentials(
    $service_account_name,
    array('https://www.googleapis.com/auth/drive'),
    $key);

$client->setAssertionCredentials($cred);
$client->addScope("https://www.googleapis.com/auth/drive");

if ($client->getAuth()->isAccessTokenExpired()) {
   $client->getAuth()->refreshTokenWithAssertion($cred);
   }
$_SESSION['service_token'] = $client->getAccessToken();


$service = new Google_Service_Drive($client);

$file = new Google_Service_Drive_DriveFile();
$parentId='0B7yersFxlKMTR2RsMThQSG1RUVk';
$parent = new Google_Service_Drive_ParentReference();

// create a file

DEFINE("TESTFILE", 'testfile-small.txt');
if (!file_exists(TESTFILE)) 
                      {
                     $fh = fopen(TESTFILE, 'w');
                      fseek($fh, 1024 * 1024);
                     fwrite($fh, "this is sekhar", 1);
                       fclose($fh);
                        }

                     $parent->setId($parentId);
                     $file->setParents(array($parent));
                      $file->setTitle("I am service Account");

                   $result = $service->files->insert($file,array('data' => file_get_contents(TESTFILE),
        'mimeType' => 'application/octet-stream',
        'uploadType' => 'media'));

我的代码有什么问题吗?

【问题讨论】:

    标签: php google-drive-api google-api-php-client


    【解决方案1】:
    403) Insufficient Permission' 
    

    意味着您的服务帐户无权执行您要求它执行的操作。

    我要检查的第一件事是您是否已正确授予服务帐户访问相关目录的权限。它是您的驱动器上的一个还是服务帐户拥有的一个? (您授予服务帐户对驱动器文件夹的访问权限,就像普通用户获取服务帐户电子邮件地址并添加它一样,它需要写入权限。)

    由于您没有创建目录列表,因此您可能还需要仔细检查您的目录 ID 是否正确。或者只是使用 root 来测试您的上传代码。

    '0B7yersFxlKMTR2RsMThQSG1RUVk'
    

    【讨论】:

    • 感谢 DalmTo,文件夹有问题,现在我可以上传文件了,但是我如何访问它们,当我尝试访问它们时,说它没有权限,我在哪里可以更改权限公开查看,以便我可以将它们嵌入到我的应用程序中
    • 我认为这是一个不同的问题。
    猜你喜欢
    • 2016-01-09
    • 1970-01-01
    • 2018-01-24
    • 1970-01-01
    • 1970-01-01
    • 2014-06-01
    • 2021-12-02
    • 2013-09-10
    • 2016-06-23
    相关资源
    最近更新 更多