Google驱动器上传返回 - (403)权限不足 [英] Google drive Upload returns - (403) Insufficient Permission

查看:215
本文介绍了Google驱动器上传返回 - (403)权限不足的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用服务帐户将文件上传到谷歌驱动器,但是我收到了错误,比如


(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('测试文档');
$ 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));
}

我已经在论坛上回顾了类似的问题并尝试了一些建议,但似乎没有任何结果。



代码有什么不对吗? 更改你的Google将范围扩展到

  Google_Service_Drive :: DRIVE 


I am trying to upload files to google drive using service account, but i am getting error like

(403) Insufficient Permission error

Although fetching list of files from drive is working fine.

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));
}

I have reviewed similar questions in forum and tried suggestions but nothing seems to work.

Is there anything wrong with code?

解决方案

Change your Google Drive Scope to

Google_Service_Drive::DRIVE

这篇关于Google驱动器上传返回 - (403)权限不足的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆