Javascript 当多次调用google sheets时,API身份验证不会';第二次调用无效:请求的身份验证作用域不足
因此,基本上我在这里所做的就是为了我自己的目的劫持了google sheets api node.js快速启动指南。这里的一切都很好,直到它进入我的电子表格。batchUpdate,然后一切都变得古怪 如果我在spreadsheets.batchUpdate中注释掉“auth”初始化,我会收到错误消息:Javascript 当多次调用google sheets时,API身份验证不会';第二次调用无效:请求的身份验证作用域不足,javascript,node.js,authentication,google-sheets-api,Javascript,Node.js,Authentication,Google Sheets Api,因此,基本上我在这里所做的就是为了我自己的目的劫持了google sheets api node.js快速启动指南。这里的一切都很好,直到它进入我的电子表格。batchUpdate,然后一切都变得古怪 如果我在spreadsheets.batchUpdate中注释掉“auth”初始化,我会收到错误消息:API返回错误:错误:请求没有有效的身份验证凭据。 如果我在spreadsheets.batchUpdate中注释掉“auth”初始化,我会收到错误消息:API返回错误:错误:请求的身份验证范围不
API返回错误:错误:请求没有有效的身份验证凭据。
如果我在spreadsheets.batchUpdate中注释掉“auth”初始化,我会收到错误消息:API返回错误:错误:请求的身份验证范围不足。
我所要做的只是从工作表中获取一些数据,然后删除后面的行,但我无法解决这个身份验证问题
var fs = require('fs');
var updateDB = require('./updateDB.js');
var readline = require('readline');
var google = require('googleapis');
var googleAuth = require('google-auth-library');
var splitData;
// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/sheets.googleapis.com-nodejs-quickstart.json
var SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly'];
var TOKEN_DIR = (process.env.HOME || process.env.HOMEPATH ||
process.env.USERPROFILE) + '/.credentials/';
var TOKEN_PATH = TOKEN_DIR + 'sheets.googleapis.com-nodejs-quickstart.json';
var array = [];
var run = {
runQuickstart : function() {
// Load client secrets from a local file.
fs.readFile('client_secret.json', function processClientSecrets(err, content) {
if (err) {
console.log('Error loading client secret file: ' + err);
return;
}
// Authorize a client with the loaded credentials, then call the
// Google Sheets API.
authorize(JSON.parse(content), listMajors);
});
}
}
/**
* 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) {
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);
// Check if we have previously stored a token.
fs.readFile(TOKEN_PATH, function(err, token) {
if (err) {
getNewToken(oauth2Client, callback);
} else {
oauth2Client.credentials = JSON.parse(token);
callback(oauth2Client);
}
});
}
/**
* Get and store new token after prompting for user authorization, and then
* execute the given callback with the authorized OAuth2 client.
*
* @param {google.auth.OAuth2} oauth2Client The OAuth2 client to get token for.
* @param {getEventsCallback} callback The callback to call with the authorized
* client.
*/
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);
});
});
}
/**
* Store token to disk be used in later program executions.
*
* @param {Object} token The token to store to disk.
*/
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);
}
/**
* Print the names and majors of students in a sample spreadsheet:
* https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit
*/
function listMajors(auth) {
var sheets = google.sheets('v4');
sheets.spreadsheets.values.get({
auth: auth,
spreadsheetId: '1EV8S8AaAmxF3vP0F6RWxKIUlvF6uFEmsrOFWA1oNBYI',
range: 'Form Responses 1!A3:X3',
}, function(err, response) {
if (err) {
console.log('The API returned an error: ' + err);
return;
}
var rows = response.values;
//splitData = rows.split(',');
updateDB.inputFormToDB.apply(this, rows);
if (rows.length == 0) {
console.log('No data found.');
} else {
console.log('Form Responses');
for (var i = 0; i < rows.length; i++) {
var row = rows[i];
// Print columns A and E, which correspond to indices 0 and 4.
console.log('%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s', row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9],row[10], row[11], row[12], row[13], row[14], row[15], row[16], row[17], row[18], row[19], row[20], row[21], row[22], row[23]);
}
var spreadsheetId = '1EV8S8AaAmxF3vP0F6RWxKIUlvF6uFEmsrOFWA1oNBYI';
var requests = [];
requests.push({
"deleteDimension": {
"range": {
"sheetId": spreadsheetId,
"dimension": "ROWS",
"startIndex": 0,
"endIndex": 3
}
}
});
var batchUpdateRequest = {requests: requests}
var test = auth;
sheets.spreadsheets.batchUpdate({
// auth: test,
spreadsheetId: spreadsheetId,
resource: batchUpdateRequest
}, function(err, response) {
if (err) {
console.log('The API returned an error: ' + err);
return;
}
});
}
});
}
module.exports = run;
var fs=require('fs');
var updateDB=require('./updateDB.js');
var readline=require('readline');
var google=require('googleapis');
var googleAuth=require('google-auth-library');
var数据;
//如果修改这些作用域,请删除以前保存的凭据
//位于~/.credentials/sheets.googleapis.com-nodejs-quickstart.json
变量作用域=['https://www.googleapis.com/auth/spreadsheets.readonly'];
var TOKEN_DIR=(process.env.HOME | | process.env.HOMEPATH||
process.env.USERPROFILE)+'/.credentials/';
var TOKEN_PATH=TOKEN_DIR+'sheets.googleapis.com nodejs quickstart.json';
var数组=[];
变量运行={
runQuickstart:function(){
//从本地文件加载客户端机密。
fs.readFile('client_secret.json',函数processClientSecrets(err,content){
如果(错误){
log('加载客户端机密文件时出错:'+err);
返回;
}
//使用加载的凭据授权客户端,然后调用
//GoogleSheetsAPI。
授权(JSON.parse(content),listMajors);
});
}
}
/**
*使用给定的凭据创建OAuth2客户端,然后执行
*给定回调函数。
*
*@param{Object}凭据授权客户端凭据。
*@param{function}回调使用授权客户端调用的回调。
*/
函数授权(凭据、回调){
var clientSecret=credentials.installed.client\u secret;
var clientId=credentials.installed.client\u id;
var redirectUrl=credentials.installed.redirect_uris[0];
var auth=new googleAuth();
var oauth2Client=new auth.OAuth2(clientId、clientSecret、redirectUrl);
//检查我们以前是否存储过令牌。
fs.readFile(令牌\路径,函数(err,令牌){
如果(错误){
getNewToken(oauth2Client,回调);
}否则{
oauth2Client.credentials=JSON.parse(令牌);
回调(oauth2Client);
}
});
}
/**
*提示用户授权后获取并存储新令牌,然后
*使用授权的OAuth2客户端执行给定的回调。
*
*@param{google.auth.OAuth2}OAuth2客户端为OAuth2客户端获取令牌。
*@param{getEventsCallback}回调使用授权的
*客户。
*/
函数getNewToken(oauth2Client,回调){
var authUrl=oauth2Client.generateAuthUrl({
访问类型:“脱机”,
范围:范围
});
log('通过访问此url授权此应用:',authUrl);
var rl=readline.createInterface({
输入:process.stdin,
输出:process.stdout
});
rl.question('在此处输入该页面的代码:',函数(代码){
rl.close();
oauth2Client.getToken(代码、函数(错误、令牌){
如果(错误){
log('尝试检索访问令牌时出错',err);
返回;
}
oauth2Client.credentials=令牌;
storeToken(token);
回调(oauth2Client);
});
});
}
/**
*将令牌存储到磁盘,以便在以后的程序执行中使用。
*
*@param{Object}标记要存储到磁盘的标记。
*/
函数storeToken(token){
试一试{
fs.mkdirSync(TOKEN_DIR);
}捕捉(错误){
if(err.code!=“EEXIST”){
犯错误;
}
}
writeFile(TOKEN_PATH,JSON.stringify(TOKEN));
console.log('存储到'+Token_PATH'的令牌);
}
/**
*在样本电子表格中打印学生姓名和专业:
* https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit
*/
函数列表(auth){
var sheets=google.sheets('v4');
表。电子表格。值。获取({
auth:auth,
电子表格ID:“1EV8S8AAAMXF3VP0F6RWXKIULVF6UFEMSROFWA10ONBYI”,
范围:“表格回复1!A3:X3”,
},函数(错误,响应){
如果(错误){
log('API返回错误:'+err);
返回;
}
var行=response.values;
//splitData=rows.split(',');
updateDB.inputFormToDB.apply(此,行);
如果(rows.length==0){
console.log('未找到数据');
}否则{
log('formresponses');
对于(变量i=0;iAllows read-only access to the user's sheets and their properties.
Allows read/write access to the user's sheets and their properties.