Authenticate to Google API with Node JS

后端 未结 2 1069
刺人心
刺人心 2020-12-31 10:12

What I have so far is the app redirects to the consent page. The user accepts, then I\'m redirected back to localhost with a valid authorization code. From what I understa

2条回答
  •  被撕碎了的回忆
    2020-12-31 10:42

    The Dummy's Guide to 3-legged Google OAuth.

    Literally everything you need to know is on this single page https://developers.google.com/identity/protocols/OAuth2WebServer . Read it twice and you'll be an OAuth ninja. In summary, it says ...

    1. Construct an accounts.google.com URL with 4 query params :-
      1. client_id to identify your app
      2. scope to say what permissions you're asking for
      3. redirect_uri to tell Google where to redirect the user's browser with the result
      4. response_type=code to say you want an Auth Code
    2. redirect the user's browser to that URL
    3. Have a sip of coffee while the user logs in, chooses his Google account, and grants permission, until eventually ...
    4. The user's browser gets redirected back to your app's redirect_uri, with a query param of code which is the one-time Auth Code
    5. Post the Auth Code to Google's token endpoint
    6. Parse the JSON response to get the Access Token
    7. Use the Access Token in a "authorization: bearer access_token" http header for your subsequent Google API requests

    If you go to https://developers.google.com/oauthplayground/ you can run through the steps online to see what the various URLs and responses look like.

提交回复
热议问题