栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

尝试通过API(v3)在Google云端硬盘上创建文件夹时权限不足

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

尝试通过API(v3)在Google云端硬盘上创建文件夹时权限不足

我认为您的脚本有效。那么您可以再次确认以下几点吗?

  1. 关于范围。
    • 在快速入门中,默认范围为
      var SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly'];
      。但是为了使用
      drive.files.create
      https://www.googleapis.com/auth/drive
      必须将其添加到范围。
    • 如果您修改了范围,请删除的文件
      drive-nodejs-quickstart.json
      。并运行脚本。这样,将运行授权,并将添加的作用域反映到刷新令牌和访问令牌。
  2. 关于googleapis的版本
    • 最近,据报道v27.0.0,v26.0.1和v25.0.0的googleapi存在一些错误。因此,请确认您的版本,以防万一。我使用v24.0.0。
  3. 是否在API控制台上启用了Drive API。
    • 关于这一点,我认为从您的问题来看,您可能已经启用了它。

如果这些对您没有用,对不起。

编辑:

var fs = require('fs');var readline = require('readline');var google = require('googleapis');var googleAuth = require('google-auth-library');// If modifying these scopes, delete your previously saved credentials// at ~/.credentials/drive-nodejs-quickstart.jsonvar SCOPES = ['https://www.googleapis.com/auth/drive'];var TOKEN_DIR = './';var TOKEN_PATH = 'drive-nodejs-quickstart.json';// Load client secrets from a local file.fs.readFile('client_secret.json', function processClientSecrets(err, content) {  if (err) {    console.log('Error loading client secret file: ' + err);    return;  }  // Authorize a client with the loaded credentials, then call the  // Drive API.  authorize(JSON.parse(content), createFolder);});function authorize(credentials, callback) {  var clientSecret = credentials.installed.client_secret;  var clientId = credentials.installed.client_id;  var redirectUrl = credentials.installed.redirect_uris[0];  var auth = new googleAuth();  var oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl);  // Check if we have previously stored a token.  fs.readFile(TOKEN_PATH, function(err, token) {    if (err) {      getNewToken(oauth2Client, callback);    } else {      oauth2Client.credentials = JSON.parse(token);      callback(oauth2Client);    }  });}function getNewToken(oauth2Client, callback) {  var authUrl = oauth2Client.generateAuthUrl({    access_type: 'offline',    scope: SCOPES  });  console.log('Authorize this app by visiting this url: ', authUrl);  var rl = readline.createInterface({    input: process.stdin,    output: process.stdout  });  rl.question('Enter the pre from that page here: ', function(pre) {    rl.close();    oauth2Client.getToken(pre, function(err, token) {      if (err) {        console.log('Error while trying to retrieve access token', err);        return;      }      oauth2Client.credentials = token;      storeToken(token);      callback(oauth2Client);    });  });}function storeToken(token) {  fs.writeFile(TOKEN_PATH, JSON.stringify(token));  console.log('Token stored to ' + TOKEN_PATH);}function createFolder(auth) {      const drive = google.drive('v3');      console.log('auth:', auth);      const filemetadata = {        'name': 'daily-report',        'mimeType': 'application/vnd.google-apps.folder',        'parents': ['1P924MEzU_1VoL6OOvWPHSo6vb1u9u0a9'],      };      drive.files.create({        auth: auth,        resource: filemetadata,        fields: 'id',      }, function(err, file) {        if (err) {          // Handle error          console.error(err.message);        } else {          console.log('Folder Id: ', file.id);        }      });    }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/484768.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号