如何从Google登录Javascript SDK获取访问令牌?

sti*_*ure 21 javascript google-plus google-oauth google-plus-signin

我有一个简单的单页javascript webapp,它使用"Google Signs In for Websites":https://developers.google.com/identity/sign-in/web/sign-in

如何为用户获取访问令牌?我需要在我的服务器上验证用户身份的可验证性.我不想离线访问; 我只想知道当Web客户端向我的服务器发送ajax请求时,我可以信任登录用户的身份.

Sca*_*ami 52

出于验证目的,最好使用作为id_tokenauth响应的一部分,并且可以在以下任何点检索:

gapi.auth2.getAuthInstance().currentUser.get().getAuthResponse().id_token
Run Code Online (Sandbox Code Playgroud)

Google API客户端库提供验证id_token的功能,并在服务器端为您提供相关的用户信息:https://developers.google.com/api-client-library/

  • @stickfigure如果你感兴趣的话,我今天也写了一篇关于使用新的Identity API进行服务器端访问的文章,因为它有几个问题,而且文档还没那么广泛:http:// codingwithgerwin.blogspot.co.at/2015/04/google-sign-in-20-server-side.html (2认同)

Sah*_*kar 6

首先需要初始化SDK然后调用以下函数获取access-token

gapi.auth2.getAuthInstance().currentUser.get().getAuthResponse().access_token; 
Run Code Online (Sandbox Code Playgroud)

这个解决方案帮助我在 javascript 中获取访问令牌。