简体   繁体   English

AWS Lambda NodeJS-OAuth到Google API

[英]AWS Lambda NodeJS - OAuth to Google API

I'm using my own gmail user to read a public calendar. 我正在使用自己的Gmail用户阅读公共日历。 Got program working locally, and displayed the credentials/token with console.log (value altered to protect my token): 使程序在本地工作,并使用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 } }

I also did what StackOverflow said: How to oAuth Google API from Lambda AWS? 我还做了StackOverflow所说的话: 如何从Lambda AWS上oAuth Google API? and it gave me the same access_token as displayed above. 它给了我与上面显示的相同的access_token。

So, if I understand, I need to take the access token and put it in my program or a file, and I'm not sure how to do that. 因此,如果我理解的话,我需要获取访问令牌并将其放入程序或文件中,但我不确定该怎么做。 My code came from the Google example here: https://developers.google.com/google-apps/calendar/quickstart/nodejs 我的代码来自以下Google示例: https//developers.google.com/google-apps/calendar/quickstart/nodejs

Do I put this token somewhere in my client_secret.json file or what? 我是否将此令牌放在client_secret.json文件中的某个位置或什么位置? I tried just passing it straight to the listEvents method as the value of TOKEN but got "400 Bad Request". 我尝试将其直接传递给listEvents方法作为TOKEN的值,但是得到了“ 400 Bad Request”。

Update 1: I tried storing the file to disk and then reading it as follows: 更新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); 
          }); 
    }); 

}

Error: It doesn't seem to be exactly be JSON, so not how to deserialize it back into object: 错误:它似乎不完全是JSON,所以没有如何将其反序列化为对象:

OAuth2Client {
^

SyntaxError: Unexpected token O in JSON at position 0

Update 2: Then I had another idea, I saved the following as 更新2:然后我有了另一个想法,我将以下内容保存为

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

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

Then when I ran on the server, I got this response back: 然后,当我在服务器上运行时,我得到了以下响应:

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

Here's how I got it to work so far. 到目前为止,这是我使它工作的方式。

1) Created a file called calendar-nodejs-quickstart.json in the root directory. 1)在根目录中创建一个名为calendar-nodejs-quickstart.json的文件。 I kept getting errors when trying to read .credentials/calendar-nodejs-quickstart.json. 尝试读取.credentials / calendar-nodejs-quickstart.json时,我一直收到错误消息。 I tried setting the environment variables, but ended up changing sample code as follows: 我尝试设置环境变量,但最终更改了示例代码,如下所示:

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

2) Had to remove "credentials :" from the beginning of the file, and add the double quotes (and also changed single quotes to double quotes). 2)必须从文件的开头删除“ credentials:”,并添加双引号(并且还将单引号更改为双引号)。 This was to get past various JSON parsing errors. 这是为了克服各种JSON解析错误。 { "access_token": "xxxxx", "refresh_token": "xxxx", "token_type": "Bearer", "expiry_date": 1512151860704 } {“ access_token”:“ xxxxx”,“ refresh_token”:“ xxxx”,“ token_type”:“ Bearer”,“ expiry_date”:1512151860704}

3) I also added the 'utf8' below, and added some debug code to see what was going on: 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);
    }
  });

Seems critical to me to show the value of the "err" variable. 对我来说,显示“ err”变量的值似乎很关键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM