javascript - 使用 OAuth2.0 使用 Javascript 的 Google Spreadsheets API

标签 javascript oauth-2.0 google-spreadsheet-api

我正在尝试使用 Javascript 访问私有(private) Google 电子表格。我已成功获得 OAuth2.0 授权,并且可以看到我所有 Google 云端硬盘文档的列表。我似乎无法做的是进入特定的电子表格。代码如下,函数“retrieveAllFiles”中的相关电子表格代码。其中很多都是从谷歌教程中挑选出来的。

var clientId = 'working';
var apiKey = 'working';
var scopes = 'https://www.googleapis.com/auth/drive.file https://www.googleapis.com/auth/drive https://spreadsheets.google.com/feeds';

function handleClientLoad() {
    console.log('inside handleClientLoad function');
    gapi.client.setApiKey(apiKey);
    window.setTimeout(checkAuth,1);
}

function checkAuth() {
    console.log('inside checkAuth function');
    gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: true}, handleAuthResult);
    console.log('finished checkAuth function');
}

function handleAuthResult(authResult) {
    console.log('inside handleAuthResult function');
    var authButton = document.getElementById('authButton');
    authButton.style.display = 'none';
    if (authResult && !authResult.error) {
        //Access token has been succesfully retrieved, requests can be sent to the API.
        apiCalls();
    } else {
        //No access token could be retrieved, show the button to start the authorization flow.
        authButton.style.display = 'block';
        authButton.onclick = function() {
            gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: false}, handleAuthResult);
        };
    }
}

function apiCalls() {
    console.log('inside apiCalls function');
    gapi.client.load('drive', 'v2', function() {
         retrieveAllFiles(callback);
    });
}

function retrieveAllFiles(callback) {
     $.get('http://spreadsheets.google.com/feeds/spreadsheets/private/full', function(data) {
        console.log('get request processed');
        console.log(data);
    });
    console.log('inside retrieveAllFiles function');
    var retrievePageOfFiles = function(request, result) {
        request.execute(function(resp) {
        result = result.concat(resp.items);
        var nextPageToken = resp.nextPageToken;
        if (nextPageToken) {
            request = gapi.client.drive.files.list({
                'pageToken': nextPageToken
            });
            retrievePageOfFiles(request, result);
        } else {
            callback(result);
        }
        });
    }
    var initialRequest = gapi.client.drive.files.list();
    retrievePageOfFiles(initialRequest, []);
}

function callback(result) {
    console.log('all should be loaded at this point');
    console.log(result);
    $('#drive-list').append('<ul class="items"></ul>');
    $.map(result, function(v,i){
        $('.items').append('<li>' + v.title + ':' + v.id + '</li>');
    });
}

明确地说,目前的最终结果是我所有 Google Drive 文档的列表,但没有用于“获取数据处理”的 console.log。我在控制台中没有收到任何错误消息,所以我不知道发生了什么。

谢谢。

最佳答案

从一堆不同来源编译的最终解决方案,希望这会对某人有所帮助。

var scopes = 'https://spreadsheets.google.com/feeds';
var clientId = 'working';
var apiKey = 'working';

function handleClientLoad() {
    console.log('inside handleClientLoad function');
    gapi.client.setApiKey(apiKey);
    window.setTimeout(checkAuth,1);
}

function checkAuth() {
    console.log('inside checkAuth function');
    gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: true}, handleAuthResult);
    console.log('finished checkAuth function');
}

function handleAuthResult(authResult) {
    console.log('inside handleAuthResult function');
    console.log(gapi.auth.getToken());
    var authButton = document.getElementById('authButton');
    authButton.style.display = 'none';
    if (authResult && !authResult.error) {
        //Access token has been successfully retrieved, requests can be sent to the API.
        loadClient();
    } else {
        //No access token could be retrieved, show the button to start the authorization flow.
        authButton.style.display = 'block';
        authButton.onclick = function() {
            gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: false}, handleAuthResult);
        };
    }
}

function loadClient() {
    console.log('inside loadClient function');
    var token = gapi.auth.getToken().access_token;
    var urlLocation = ''; //Get this from the URL of your Spreadsheet in the normal interface for Google Drive.

    //This gives a spitout of ALL spreadsheets that the user has access to.
    var url = 'https://spreadsheets.google.com/feeds/spreadsheets/private/full?access_token=' + token;

    //This gives a list of all worksheets inside the Spreadsheet, does not give actual data
    var url = 'https://spreadsheets.google.com/feeds/worksheets/' + urlLocation + '/private/full?access_token=' + token;

    //This gives the data in a list view - change the word list to cells to work from a cell view and see https://developers.google.com/google-apps/spreadsheets/#working_with_cell-based_feeds for details
    var url = 'https://spreadsheets.google.com/feeds/list/' + urlLocation + '/od6/private/full?access_token=' + token;

    console.log(url);
    $.get(url, function(data) {
        console.log(data);
    });
}

关于javascript - 使用 OAuth2.0 使用 Javascript 的 Google Spreadsheets API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14238935/

相关文章:

android - SampleSyncAdapter 存储密码明文?

c# - 在 .net 中针对启用 Oauth2 的 API 进行自动化集成测试

authentication - 跨浏览器选项卡的 JHipster JWT 身份验证

javascript - 在 Android 上使用 Realm 和远程调试失败

google-apps-script - Google Spreadsheets:仅复制已过滤/过滤 View 行

google-apps-script - (Gmail) 从电子表格发送电子邮件。如何添加带有图像的签名?

c# - 使用 C# 访问私有(private) Google 电子表格

javascript - 如何使用 div url 的 jQuery 加载不同的内容

Razor View 中 &lt;script&gt; 标记内的 C# 'if' 关键字

javascript - Javascript中有内置的算术函数吗?