无法生成要发送给用户的URL,以请求访问特定的非公开Goog​​le工作表

时间:2016-03-19 19:00:14

标签: node.js google-api google-sheets google-spreadsheet-api

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

我已阅读并重新阅读Google Sheet API here和Google REST API for Node 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(之前已注释掉)。当我尝试运行该应用程序时,出现以下错误:无权查看工作表。这是有道理的,因为工作表是私有的,我需要生成一个允许我访问它的链接。我已在haste bin here

上发布了代码

1 个答案:

答案 0 :(得分:0)

好的,我设法解决了。问题是我没有调用正确的Scopes,并将错误的回调函数传递给#define SIZE 31 char ** commonWords(char *str1, char *str2) { int i,j=0,count1,count2,k=0,a,b,m=0,n; char str3[100][100], str4[100][100]; char **output; output = (char **)malloc(SIZE*sizeof(char*)); if (str1 == NULL || str2 == NULL) { return NULL; } for (i = 0; str1[i] != '\0'; i++) { if (str1[i] != ' ') { str3[j][k++] = str1[i]; } else { str3[j][k++] = '\0'; j++; k = 0; } } str3[j][k++] = '\0'; count1 = j > 0 ? j + 1 : j; j = k = 0; for (i = 0; str2[i] != '\0'; i++) { if (str2[i] != ' ') { str4[j][k++] = str2[i]; } else { str4[j][k++] = '\0'; j++; k = 0; } } str4[j][k++] = '\0'; count2 = j > 0 ? j + 1 : j; for (i = 0; i < count1; i++) { for (j = 0; j < count2; j++) { if (str3[i][k] == str4[j][k]) { if (str3[i][k + 1] == str4[j][k + 1] && str3[i][k + 2] == str4[j][k + 2] == '\0') { a = i; b = k; while (str3[a][b] != '\0') { output = (char **)malloc(SIZE*sizeof(char)); output[m][n] = str3[a][b]; n++; b++; } output[m][n] = '\0'; } else if (str3[i][k + 1] == str4[j][k + 1] && str3[i][k + 2] == str4[j][k + 2]) { a = i; b = k; while (str3[a][b] != '\0') { output = (char **)malloc(SIZE*sizeof(char)); output[m][n] = str3[a][b]; n++; b++; } output[m][n] = '\0'; m++; } } } } return output; } 方法。这是正确的代码:

authorize