dthp96899 2018-08-20 10:54
浏览 78
已采纳

Google Drive PHP API:删除显示权限不足

I've been working on deleting a file in my Google Drive but I can't make it work. I have changed my scope from $client->setScopes(Google_Service_Drive::DRIVE_METADATA_READONLY); to $client->setScopes(Google_Service_Drive::DRIVE);.

Here's my full code:

<?php
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_METADATA_READONLY);
  $client->setScopes(Google_Service_Drive::DRIVE);
  //$client->setScopes(Google_Service_Drive::DRIVE_APPDATA);
  //$client->setScopes(Google_Service_Drive::DRIVE_FILE);
  //$client->setAuthConfig('credentials.json');
  $client->setAccessType('offline');

  // Load previously authorized credentials from a file.
  $credentialsPath = 'token.json';
  if (file_exists($credentialsPath)) {
    $accessToken = json_decode(file_get_contents($credentialsPath), true);
  } else {
    // Request authorization from the user.
    $authUrl = $client->createAuthUrl();
    printf("Open the following link in your browser:
%s
", $authUrl);
    print 'Enter verification code: ';
    $authCode = trim(fgets(STDIN));

    // Exchange authorization code for an access token.
    $accessToken = $client->fetchAccessTokenWithAuthCode($authCode);

    // Check to see if there was an error.
    if (array_key_exists('error', $accessToken)) {
        throw new Exception(join(', ', $accessToken));
    }

    // Store the credentials to disk.
    if (!file_exists(dirname($credentialsPath))) {
        mkdir(dirname($credentialsPath), 0700, true);
    }
    file_put_contents($credentialsPath, json_encode($accessToken));
    printf("Credentials saved to %s
", $credentialsPath);
}
$client->setAccessToken($accessToken);

// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
    $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
    file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
}
    return $client;
}


// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Drive($client);

/*Get Files under hourly_backup*/
$hourly_backup_id = '1Y3cGEwXy9gLcw9WO0isqwtwtysU0g_bK';

$optParams = array(
  //'pageSize' => 20,
  'fields' => 'nextPageToken, files(id,name,size,parents,createdTime)',
  'q' => "'".$hourly_backup_id."' in parents"
);
$results = $service->files->listFiles($optParams);

if (count($results->getFiles()) == 0) {
    print "No files found.
";
} else {
    foreach ($results->getFiles() as $file) {
        if(strtotime(date('Y-m-d H:i:s', strtotime($file->getcreatedTime()))) <= strtotime('-48 hours')){
            try {
                return $service->files->delete($file->getId());
            } catch (Exception $e) {
                print "An error occurred: " . $e->getMessage();
            }
            return NULL;
        } else {
           print "weh 
";
        }
    }
}

If the condition is met and the delete code is ran, this is what I get:

An error occurred: {
 "error": {
   "errors": [
   {
    "domain": "global",
    "reason": "insufficientPermissions",
    "message": "Insufficient Permission"
   }
  ],
  "code": 403,
  "message": "Insufficient Permission"
 }
}

UPDATE:

I also tried in the API Explorer and it worked without any problem.

The google drive I'm working on is mine and I am able to delete a file directly without any problems. What could be missing in my code?

  • 写回答

1条回答 默认 最新

  • dongpin3794 2018-08-20 13:19
    关注

    "Insufficient Permission"

    Means just that the user who is currently authenticated does not have permission to do what you are trying to do.

    You said that you changed the scopes. But did you reauthcate the application? The application will pop up and request permission to the drive account. If you are still running with an old access token or refresh token that still has read only access you are not going to be able to delete the file.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题