在 chrome 扩展中使用 gapi.client.drive 的正确方法

Right way to use gapi.client.drive in chrome extension

我正在尝试使用驱动器保存来自 chrome 扩展的数据。

首先,我将需要的选项设置为 manifest.json

"oauth2": {
    "client_id": "999999.apps.googleusercontent.com",
    "scopes": [
        "https://www.googleapis.com/auth/drive.appdata"
    ]
},

然后尝试获取文件列表:

$.getScript("https://apis.google.com/js/client.js", function () {

    gapi.load('client', function () {

        console.log("gapi.client is loaded")

        gapi.client.load('drive', 'v3', function () {

            console.log("gapi.client.drive is loaded");

            chrome.identity.getAuthToken({'interactive': true}, function (token) {

                gapi.client.setToken({access_token: token});

                console.log("token :", token);

                gapi.client.drive.files.list().then(function (list) {
                    console.log(list)
                })

            });

        });
    });
});

控制台说:

gapi.client is loaded
gapi.client.drive is loaded
token : [TOKEN]

错误是这样的:

"code": 403,
"message": "The granted scopes do not give access to all of the requested spaces."

该错误表明您正试图访问未经授权的 space。你的作用域只允许你使用 "appDataFolder" space。因此,你必须改变

gapi.client.drive.files.list()

为了

gapi.client.drive.files.list({spaces:"appDataFolder"}).