javascript 使用 AngularJS 回调的 Google OAuth2 登录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15263114/
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
Google OAuth2 Sign-in with AngularJS callback
提问by Matthew Lucas
For reference, here is the Google+ flow that I am working with:
作为参考,这是我正在使用的 Google+ 流程:
https://developers.google.com/+/web/signin/server-side-flow
https://developers.google.com/+/web/signin/server-side-flow
- User clicks the sign-in button. The authorization request is sent to Google's OAuth servers
- OAuth dialog is triggered for the user
- access_token, id_token, and a one-time code are returned
- Client sends id_token and code to server
- Server exchanges one-time code for access_token
- Google returns access_token
- Server should confirm "fully logged in" to Client
- 用户单击登录按钮。授权请求被发送到 Google 的 OAuth 服务器
- 为用户触发 OAuth 对话框
- 返回 access_token、id_token 和一次性代码
- 客户端将 id_token 和代码发送到服务器
- 服务器为 access_token 交换一次性代码
- Google 返回 access_token
- 服务器应向客户端确认“完全登录”
I already have this mostly working, but I would like AngularJS to know when the client is "fully logged in" at step 7.
我已经完成了大部分工作,但我希望 AngularJS 知道客户端何时在第 7 步“完全登录”。
Ideally, I would like step 3 to be handled by an AngularJS Controller, but I'm not sure if that's possible.
理想情况下,我希望第 3 步由 AngularJS 控制器处理,但我不确定这是否可行。
The sign-in button is here:
登录按钮在这里:
<!-- Add where you want your sign-in button to render -->
<div id="signinButton">
<span class="g-signin"
data-scope="https://www.googleapis.com/auth/plus.login"
data-clientid="YOUR_CLIENT_ID"
data-redirecturi="postmessage"
data-accesstype="offline"
data-cookiepolicy="single_host_origin"
data-callback="signInCallback">
</span>
</div>
<div id="result"></div>
The data-callback parameter allows me to specify a function, but I can only get it to work on global functions.
data-callback 参数允许我指定一个函数,但我只能让它在全局函数上工作。
The only work-around I can think of is for AngularJS to constantly poll the server for updates just in case a user logs in, but I was wondering if there was a way to make this work within the event structure so that it's all instant.
我能想到的唯一解决方法是让 AngularJS 不断轮询服务器以获取更新,以防万一用户登录,但我想知道是否有办法在事件结构中进行这项工作,以便一切都是即时的。
Whether this is possible or not, I would greatly appreciate any advice on how I should go about this. Please let me know if you have any questions. Thank You!
无论这是否可能,我将不胜感激关于我应该如何处理的任何建议。请让我知道,如果你有任何问题。谢谢!
采纳答案by Matthew Lucas
After a bunch of searching, I finally found what I needed here:
经过一番搜索,我终于在这里找到了我需要的东西:
https://developers.google.com/+/web/signin/#javascript_api
https://developers.google.com/+/web/signin/#javascript_api
I couldn't use the data-attributes to accomplish what I needed. I needed to render the button manually using the javascript API.
我无法使用数据属性来完成我所需要的。我需要使用 javascript API 手动呈现按钮。