dongshou1856 2016-12-01 11:11
浏览 99

php中没有足够的权限google drive api oauth2

I am trying to upload a file on google drive through PHP. I've created a project in the developer console, enabled the Drive API and added an OAuth 2.0 client ID. here is my code.

<?php
 require_once __DIR__.'/vendor/autoload.php';

    session_start();

    $client = new Google_Client();
    $client->setAuthConfigFile('client_secret.json');
    $client->addScope(Google_Service_Drive::DRIVE_METADATA_READONLY);
    $client->setScopes(array('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())->getFiles();
        echo "<pre>";
        print_r($files_list);
    } else {
        $redirect_uri = 'http://localhost/oauth2callback.php';
        header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
    }   
    $filename = "abc.txt";
    $file = new Google_Service_Drive_DriveFile();
    $file->setName($filename);
    $result = $drive_service->files->create($file, array(
  'data' => file_get_contents("abc.txt"),
  'mimeType' => 'application/octet-stream',
  'uploadType' => 'media'
));

Output is:

Fatal error:  Uncaught exception 'Google_Service_Exception' with message '{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "insufficientPermissions",
    "message": "Insufficient Permission"
   }
  ],
  "code": 403,
  "message": "Insufficient Permission"
 }
}
' in D:\xampp\htdocs\vendor\google\apiclient\src\Google\Http\REST.php:118
  • 写回答

1条回答 默认 最新

  • duanpacan9388 2016-12-02 11:56
    关注

    The error means The user does not have sufficient permissions for the {fileId} The user does not have write access to a file, and the app is attempting to modify that file.

    Grant your service account permission to access the file. Use Permissions:insert to inserts a permission for a file.

    import com.google.api.services.drive.Drive;
    import com.google.api.services.drive.model.Permission;
    
    import java.io.IOException;
    // ...
    
    public class MyClass {
    
      // ...
    
      /**
       * Insert a new permission.
       *
       * @param service Drive API service instance.
       * @param fileId ID of the file to insert permission for.
       * @param value User or group e-mail address, domain name or {@code null}
                      "default" type.
       * @param type The value "user", "group", "domain" or "default".
       * @param role The value "owner", "writer" or "reader".
       * @return The inserted permission if successful, {@code null} otherwise.
       */
      private static Permission insertPermission(Drive service, String fileId,
          String value, String type, String role) {
        Permission newPermission = new Permission();
    
        newPermission.setValue(value);
        newPermission.setType(type);
        newPermission.setRole(role);
        try {
          return service.permissions().insert(fileId, newPermission).execute();
        } catch (IOException e) {
          System.out.println("An error occurred: " + e);
        }
        return null;
      }
    
      // ...
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?