gpt4 book ai didi

node.js - 无法生成发送给用户以请求访问特定非公开 Google 工作表的 URL

转载 作者:太空宇宙 更新时间:2023-11-04 02:15:06 25 4
gpt4 key购买 nike

过去几天我一直在尝试解决这个问题,但未能成功。我需要生成一个网址,要求用户允许阅读他们的特定 Google 表格,如果他们同意,我将有权阅读该表格。

我已经阅读并重新阅读了 Google Sheet API here ,以及用于 Node 的 Google REST API here ,但我无法将其应用到我的特定案例中。

我能够成功使用 REST API 指南来读取有关用户 Google 云端硬盘中所有文件的元详细信息,但我实际需要的是读取(并且仅读取,没有其他权限)一张 Google 工作表。根据 Sheet API,正确的范围是 https://spreadsheets.google.com/feeds,但这就是我可以从 API 中使用的所有信息,因为代码仅用 Java/.NET 编写。

我在论坛上搜索了答案,但其中大多数都非常过时且不适用,我发现this帖子建议使用 node-google-spreadsheets,但文档没有提供足够的信息来说明如何实际使用它。我在下面附上了我的代码。以及关于我遇到问题的地方的注释。我将不胜感激任何帮助,因为我已经筋疲力尽地试图让它发挥作用。谢谢(:

var fs = require('fs');
var readline = require('readline');
var google = require('googleapis');
var googleAuth = require('google-auth-library');
var GoogleSpreadsheets = require('google-spreadsheets');


var SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly']; //This scope works just fine when reading metadata, but to readspreedsheets, I think the correct one is: https://spreadsheets.google.com/feeds
var TOKEN_DIR = (process.env.HOME || process.env.HOMEPATH ||
process.env.USERPROFILE) + '/.credentials/';
var TOKEN_PATH = TOKEN_DIR + 'drive-nodejs-quickstart.json';

fs.readFile('client_secret.json', function processClientSecrets(err, content) {
if (err) {
console.log('Error loading client secret file: ' + err);
return;
}

authorize(JSON.parse(content), listFiles);
});


/*************************************No Idea where to put this code:

var oauth2Client = new google.auth.OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URL);
// Assuming you already obtained an OAuth2 token that has access to the correct scopes somehow...
oauth2Client.setCredentials({
access_token: ACCESS_TOKEN,
refresh_token: REFRESH_TOKEN
});

GoogleSpreadsheets({
key: '<spreadsheet key>',
auth: oauth2Client
}, function(err, spreadsheet) {
spreadsheet.worksheets[0].cells({
range: 'R1C1:R5C5'
}, function(err, cells) {
// Cells will contain a 2 dimensional array with all cell data in the
// range requested.
});
});



************************************************************/


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);

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 code from that page here: ', function(code) {
rl.close();
oauth2Client.getToken(code, 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) {
try {
fs.mkdirSync(TOKEN_DIR);
} catch (err) {
if (err.code != 'EEXIST') {
throw err;
}
}
fs.writeFile(TOKEN_PATH, JSON.stringify(token));
console.log('Token stored to ' + TOKEN_PATH);
}


function listFiles(auth) {
var service = google.drive('v3');
service.files.list({
auth: auth,
pageSize: 10,
fields: "nextPageToken, files(id, name)"
}, function(err, response) {
if (err) {
console.log('The API returned an error: ' + err);
return;
}
var files = response.files;
if (files.length == 0) {
console.log('No files found.');
} else {
console.log('Files:');
for (var i = 0; i < files.length; i++) {
var file = files[i];
console.log('%s (%s)', file.name, file.id);
}
}
});
}

Edit1:在 Sumnu2 的帮助下,我合并了 google-spreadsheets(之前已注释掉)。当我尝试运行该应用程序时,出现以下错误:无权查看工作表。这是有道理的,因为该工作表是私有(private)的,我需要生成一个允许我访问它的链接。我已将代码发布在Haste bin here

最佳答案

好的,我已经解决了。问题是我没有调用正确的作用域,并将错误的回调函数传递给 authorize 方法。这是正确的代码:

var fs = require('fs');
var readline = require('readline');
var google = require('googleapis');
var googleAuth = require('google-auth-library');
var GoogleSpreadsheets = require('google-spreadsheets');
var secrets = require('./client_secret.json');

var SCOPES = ['https://spreadsheets.google.com/feeds', 'https://spreadsheets.google.com/feeds/worksheets/key/private/basic',
];
var TOKEN_DIR = (process.env.HOME || process.env.HOMEPATH ||
process.env.USERPROFILE) + '/.credentials/';
var TOKEN_PATH = TOKEN_DIR + 'drive-nodejs-quickstart.json';

fs.readFile('client_secret.json', function processClientSecrets(err, content) {
if (err) {
console.log('Error loading client secret file: ' + err);
return;
}

authorize(JSON.parse(content), getSheet);
});



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();
oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl);

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 code from that page here: ', function(code) {
rl.close();
oauth2Client.getToken(code, 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) {
try {
fs.mkdirSync(TOKEN_DIR);
} catch (err) {
if (err.code != 'EEXIST') {
throw err;
}
}
fs.writeFile(TOKEN_PATH, JSON.stringify(token));
console.log('Token stored to ' + TOKEN_PATH);
}

function getSheet(auth) {
GoogleSpreadsheets({
key: '<SpreadSheetKey>',
auth: oauth2Client
}, function(err, spreadsheet) {
if (err) {
console.log('--------------err: ');
console.log(err);
return;
} else {
console.log('--------------reading sheet: ');

spreadsheet.worksheets[0].cells({

}, function(err, cells) {
console.log('================MADE IT HERE!!!!!!!!!!!!!!');

if (err) {
console.log(err);
return;
}

for(var prop in cells) {
console.log('for loop');
var value = cells[prop];
console.log(prop, value);
}

});

}
})
}

关于node.js - 无法生成发送给用户以请求访问特定非公开 Google 工作表的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36105853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com