【问题标题】:AWS Lambda NodeJS - OAuth to Google APIAWS Lambda NodeJS - 到 Google API 的 OAuth
【发布时间】:2017-12-01 23:51:20
【问题描述】:

我正在使用自己的 gmail 用户来阅读公共日历。让程序在本地运行,并使用 console.log 显示凭据/令牌(更改值以保护我的令牌):

Got Token
OAuth2Client {
  transporter: DefaultTransporter {},
  _certificateCache: null,
  _certificateExpiry: null,
  _clientId: 'xxxxxxxxxxxxxx',
  _clientSecret: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  _redirectUri: 'urn:ietf:wg:oauth:2.0:oob',
  _opts: {},
  credentials:
   { access_token: 'xxxxxxx',
     refresh_token: 'xxxxxxxxxxxxxxxxxx',
     token_type: 'Bearer',
     expiry_date: 1512151860704 } }

我也做了 StackOverflow 所说的:How to oAuth Google API from Lambda AWS?,它给了我与上面显示的相同的 access_token。

所以,如果我理解的话,我需要获取访问令牌并将其放入我的程序或文件中,但我不知道该怎么做。我的代码来自此处的 Google 示例:https://developers.google.com/google-apps/calendar/quickstart/nodejs

我是把这个令牌放在我的 client_secret.json 文件中的某个地方还是什么?我尝试直接将它作为 TOKEN 的值传递给 listEvents 方法,但得到“400 Bad Request”。

更新 1: 我尝试将文件存储到磁盘,然后按如下方式读取:

exports.getCalendarJSONEventsNew = 
  function getCalendarJSONEvents(callback) {
    console.log("started getCalendarJSONEventsNew");
    fs.readFile('OAuth2Client.json', 'utf8',
      function processedFile(err, content) {
          if (err) {
            console.log('Error loading OAuth2Client.json: ' + err);
            return;
          }

          console.log("content=");
          console.log(content);
          var tokenFromFile = JSON.parse(content); 
          listEvents(tokenFromFile, function(jsonResult) {
                      console.log("Json Callback Events="); 
                      console.log(jsonResult); 
                      callback(jsonResult); 
          }); 
    }); 

}

错误:它似乎并不完全是 JSON,所以不是如何将其反序列化回对象:

OAuth2Client {
^

SyntaxError: Unexpected token O in JSON at position 0

更新2:然后我有另一个想法,我将以下内容保存为

credentials: {
    access_token: 'xxxxx',
    refresh_token: 'xxxxxx',
    token_type: 'Bearer',
    expiry_date: 1512151860704
}

作为 .credentials/calendar-nodejs-quickstart.json。

然后当我在服务器上运行时,我得到了这个响应:

Authorize this app by visiting this url: https://accounts.google.com/o/oauth2/auth?etc... 

【问题讨论】:

    标签: amazon-web-services lambda oauth google-calendar-api


    【解决方案1】:

    到目前为止,这是我如何让它工作的。

    1) 在根目录中创建了一个名为 calendar-nodejs-quickstart.json 的文件。 尝试读取 .credentials/calendar-nodejs-quickstart.json 时,我不断收到错误消息。我尝试设置环境变量,但最终更改示例代码如下:

    var TOKEN_DIR = '.credentials/';
    var TOKEN_PATH = 'calendar-nodejs-quickstart.json';
    

    2) 必须从文件开头删除“credentials :”,并添加双引号(并将单引号更改为双引号)。这是为了克服各种 JSON 解析错误。 { “access_token”:“xxxxx”, "refresh_token": "xxxx", "token_type": "承载者", “到期日期”:1512151860704 }

    3) 我还在下面添加了“utf8”,并添加了一些调试代码以查看发生了什么:

      // Check if we have previously stored a token.
      console.log("TOKEN_PATH=" + TOKEN_PATH); 
      fs.readFile(TOKEN_PATH, 'utf8', function(err, token) {
        if (err) {
          console.log("err=" + err); 
          getNewToken(oauth2Client, callback);
        } else {
          console.log("Use stored tokens from " + TOKEN_PATH); 
          console.log(token); 
          oauth2Client.credentials = JSON.parse(token);
          callback(oauth2Client);
        }
      });
    

    显示“err”变量的值对我来说似乎很重要。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-26
      • 1970-01-01
      • 2018-06-07
      • 2017-07-13
      • 2023-03-30
      • 2019-10-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多