【问题标题】:Google calendar authentication on AWS Elastic beanstalkAWS Elastic beanstalk 上的 Google 日历身份验证
【发布时间】:2019-01-06 04:24:12
【问题描述】:

我刚刚将我的第一个卑鄙的应用程序移到了 aws 并考虑到所有事情都进展顺利。但我的问题是现在我已经启动它并且运行我的谷歌日历 api 不起作用。我检查了日志并注意到,因为我更改了 URL,api 想要通过访问来重新验证

通过访问此网址来授权此应用程序:https://accounts.google.com/o/oauth2/v2/auth?access_type=offline&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcalendar&response_type=code&client_id=628160639508-njce0d3437e19gs8t4gn5at3surr3seu.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob 在此处输入该页面的代码:

现在,我可以从日志中轻松获取 url 并进行验证,但随后它会给出一个代码并引导应用程序返回代码以进行验证。当它在 aws elastic beanstalk 上运行时,我该怎么做?

非常感谢任何帮助。 谢谢

【问题讨论】:

    标签: node.js amazon-web-services amazon-elastic-beanstalk mean-stack


    【解决方案1】:

    如本文档中所述:https://developers.google.com/calendar/quickstart/nodejs,您需要使用此新代码获取新令牌。

    这是一个可能对您有所帮助的示例:

    /**
     * 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 for the authorized client.
     */
    function getAccessToken(oAuth2Client, callback) {
      const authUrl = oAuth2Client.generateAuthUrl({
        access_type: 'offline',
        scope: SCOPES,
      });
      console.log('Authorize this app by visiting this url:', authUrl);
      const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout,
      });
      rl.question('Enter the code from that page here: ', (code) => {
        rl.close();
        oAuth2Client.getToken(code, (err, token) => {
          if (err) return callback(err);
          oAuth2Client.setCredentials(token);
          // Store the token to disk for later program executions
          fs.writeFile(TOKEN_PATH, JSON.stringify(token), (err) => {
            if (err) console.error(err);
            console.log('Token stored to', TOKEN_PATH);
          });
          callback(oAuth2Client);
        });
      });
    }
    

    【讨论】:

    • 是的,这就是我最初启动该过程的方式。但是当我将它移动到 beanstalk 时,我没有启动节点服务器,所以在我从访问谷歌网站获得授权代码后,我应该将它放在运行节点服务器的 beanstalk 文件中
    【解决方案2】:

    据我了解,您已经在日历 API 中注册了一个重定向 URL,一旦您在登录页面后开始验证过程,控制流就会使用所有凭据(令牌)重定向到该 URL。

    在这里,您可以在节点应用程序中创建一个端点,例如 /token 并将其注册到日历 API。一旦发生重定向,您可以将数据保存在节点应用程序中的地图中,以后可以将其用于所有其他日历 API 调用。

    这是一个令牌端点的小例子:

    var google        = require("googleapis");
    
    var OAuth2        = google.auth.OAuth2;
    
    var sessMap       = require('./sessionMap');
    
    var oauth2Client  = new OAuth2(<client_id>, <access_token>, <domain>+"/token");
    
    var app           = require('express')();
    
    app.all('/token', function(req, res){
       var code = req.query.code;
       oauth2Client.getToken(code, function(err, tokens) {
        if (err) {
          res.send(err);
          return;
        }
        oauth2Client.setCredentials(tokens);
    
        //updated after sessionMap
        sessMap.set('googleAuth', tokens);        
    
        res.send("This page will now redirect you");
    
       });         
    });
    

    要保存凭据,您可以创建一个会话映射,如下所示:

    sessionMap.js

    var hashmapSession = {};
    exports.sess = {
      set : function(key, value){
        hashmapSession[key] = value;
      },
      get : function(key){
        return hashmapSession[key];
      },
      all : function(){
        return hashmapSession;
      },
      delete : function(key){
        delete hashmapSession[key];
      }
    };
    

    以上代码必须合并到您的节点应用程序中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-06
      • 1970-01-01
      • 2015-05-10
      • 2012-05-29
      • 2014-07-30
      • 1970-01-01
      • 2021-09-29
      • 1970-01-01
      相关资源
      最近更新 更多