【问题标题】:403 creating a folder via Google-Drive-RESTAPI v3403 通过 Google-Drive-RESTAPI v3 创建文件夹
【发布时间】:2018-12-30 15:12:30
【问题描述】:

我正在尝试使用 Google-Drive REST-API 创建一个文件夹。我的问题是响应“权限不足”。

我直接使用 GDrive-API 文档中的示例代码。但它不起作用。

fs.readFile('credentials.json', (err, content) => {
    if (err) return console.log('Error loading client secret file:', err);
    // Authorize a client with credentials, then call the Google Drive API.
    authorize(JSON.parse(content), createDir);
});


/**
 * Create an OAuth2 client with the given credentials, and then execute the
 * given callback function.
 * @param {Object} credentials The authorization client credentials.
 * @param {function} callback The callback to call with the authorized client.
 */
function authorize(credentials, callback) {
    const {client_secret, client_id, redirect_uris} = credentials.installed;
    const oAuth2Client = new google.auth.OAuth2(
        client_id, client_secret, redirect_uris[0]);

    // Check if we have previously stored a token.
    fs.readFile(TOKEN_PATH, (err, token) => {
        if (err) return getAccessToken(oAuth2Client, callback);
        oAuth2Client.setCredentials(JSON.parse(token));
        callback(oAuth2Client);
    });
}

function createDir(auth) {
    const drive = google.drive({version: 'v3', auth});
    var fileMetadata = {
        'name': 'Invoice',
        'mimeType': 'application/vnd.google-apps.folder'
    };
    drive.files.create({
        resource: fileMetadata,
        fields: 'id'
    }, function (err, file) {
        if (err) {
            // Handle error
            console.error(err);
        } else {
            console.log('Folder Id: ', file.id);
        }
    });
}

注意:读取文件没有任何问题。

【问题讨论】:

    标签: javascript node.js google-drive-api


    【解决方案1】:

    我使用了错误的访问范围。使用“https://www.googleapis.com/auth/drive”获得完全访问权限。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-22
      • 2019-05-02
      • 2018-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多