使用 nodejs 进行 Google 身份验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10099341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:30:45  来源:igfitidea点击:

Google authentication with nodejs

node.jsoauthgoogle-apiredirect

提问by Simon

I'm trying to reach Google authentication API with my node.js server but I just can't find how. Basically, I want the user to arrive on my login page, click on the authentication button, then the server redirects him to the Google API, the user allows my app to access his data and then the Google API redirects the user to my site with the authorization token.

我正在尝试使用我的 node.js 服务器访问 Google 身份验证 API,但我找不到方法。基本上,我希望用户到达我的登录页面,点击身份验证按钮,然后服务器将他重定向到 Google API,用户允许我的应用访问他的数据,然后 Google API 将用户重定向到我的网站授权令牌。

I've set up the url required by oauth2 and it seems to work (if I paste it in my web browser, it redirects me on google authorization page). I saw this postwhich seemed to answer my question but I don't how to handle the express code part.

我已经设置了 oauth2 所需的 url,它似乎可以工作(如果我将它粘贴到我的网络浏览器中,它会将我重定向到 google 授权页面)。我看到这篇文章似乎回答了我的问题,但我不知道如何处理快速代码部分。

Here is the code I'm using :

这是我正在使用的代码:

https.get(options, function(res) {
    var retrieved_data = '';
    res.on('data', function(data) { retrieved_data += data; });
    res.on('end', function() { 
        // FIXME
        client_response.writeHead(302, {"Content-Type" : "text/plain", "Location" : res.headers.location});
        client_response.end(retrieved_data);    
    });
});

But this method returns an error on my client page (raised by the error field of my ajax request). If I change the HTTP code to 200 without the "Location" information, it displays on the client a "Moved Temporarily" page with a button which redirects me to Google API authorization page.

但是此方法在我的客户端页面上返回错误(由我的 ajax 请求的错误字段引发)。如果我在没有“位置”信息的情况下将 HTTP 代码更改为 200,它会在客户端上显示一个“临时移动”页面,并带有一个将我重定向到 Google API 授权页面的按钮。

Any suggestion ?

有什么建议吗?

采纳答案by Simon

As stated by Burcu Dogan, Google has released an official client library for accessing Google APIs. It can be found here : https://github.com/google/google-api-nodejs-client/

正如 Burcu Dogan 所说,谷歌已经发布了一个用于访问谷歌 API 的官方客户端库。可以在这里找到:https: //github.com/google/google-api-nodejs-client/

回答by arvidkahl

I would suggest to use existing modules for that task. everyauthis a module that allows not only oauth2 access to Google, but to most other platform that offer it as well. I am using this for twitter access on my projects. It has expressintegration as well.

我建议将现有模块用于该任务。Everyauth是一个模块,它不仅允许 oauth2 访问 Google,还允许大多数其他提供它的平台。我正在使用它来访问我的项目的 twitter。它也有express集成。

You can look for more alternatives at The Node Toolbox. Passport comes to mind.

您可以在The Node Toolbox 中寻找更多替代方案。想到了护照。

回答by UpTheCreek

I've had good results with Passport(not related to the MS authentication scheme!). I initially tried everyauth, but IMO passport fits better with expressjs conventions.

我使用Passport取得了不错的结果(与 MS 身份验证方案无关!)。我最初尝试过everyauth,但IMO 护照更适合expressjs 约定。

There's a Google auth provider ready to use: http://passportjs.org/guide/google/

有一个可供使用的 Google 身份验证提供程序:http: //passportjs.org/guide/google/

回答by Wanasit Tanakitrungruang

You may want to checkout my Node.JS + Google Calendar project here: https://github.com/berryboy/google-calendar

您可能想在此处查看我的 Node.JS + Google 日历项目:https: //github.com/berryboy/google-calendar

I also have a few examples of using google oauth.

我也有一些使用 google oauth 的例子。