22

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.

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 post which seemed to answer my question but I don't how to handle the express code part.

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.

Any suggestion ?

asked Apr 11, 2012 at 3:19
1

4 Answers 4

9

I would suggest to use existing modules for that task. everyauth is 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 express integration as well.

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

TJB
13.5k4 gold badges37 silver badges47 bronze badges
answered Apr 12, 2012 at 12:50
Sign up to request clarification or add additional context in comments.

Comments

9

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/

answered Jan 3, 2014 at 14:18

Comments

8

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.

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

answered Feb 1, 2013 at 10:37

1 Comment

Note that the passport-google module uses OpenID, which is no longer supported by Google. Instead use npmjs.com/package/passport-google-oauth2
3

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

I also have a few examples of using google oauth.

answered Nov 23, 2012 at 5:25

1 Comment

Please expand your answer with more content from the linked article.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.