Search code examples
javascriptgoogle-chromegoogle-chrome-extensiongoogle-api-js-client

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


I'm trying to use drive to save data from chrome extension.

First, I set needed options to manifest.json

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

Then try to get list of files:

$.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)
                })

            });

        });
    });
});

Console said:

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

And the error is like that:

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

Solution

  • The error indicates that you are trying to access unauthorized spaces. Your scope only allows you to use the "appDataFolder" space. Therefore, you have to change

    gapi.client.drive.files.list()

    for

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