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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 00:06:09  来源:igfitidea点击:

Google OAuth2 Sign-in with AngularJS callback

javascriptangularjsgoogle-apioauth-2.0google-oauth

提问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

  1. User clicks the sign-in button. The authorization request is sent to Google's OAuth servers
  2. OAuth dialog is triggered for the user
  3. access_token, id_token, and a one-time code are returned
  4. Client sends id_token and code to server
  5. Server exchanges one-time code for access_token
  6. Google returns access_token
  7. Server should confirm "fully logged in" to Client
  1. 用户单击登录按钮。授权请求被发送到 Google 的 OAuth 服务器
  2. 为用户触发 OAuth 对话框
  3. 返回 access_token、id_token 和一次性代码
  4. 客户端将 id_token 和代码发送到服务器
  5. 服务器为 access_token 交换一次性代码
  6. Google 返回 access_token
  7. 服务器应向客户端确认“完全登录”

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:

登录按钮在这里:

https://developers.google.com/+/web/signin/server-side-flow#step_4_add_the_sign-in_button_to_your_page

https://developers.google.com/+/web/signin/server-side-flow#step_4_add_the_sign-in_button_to_your_page

<!-- 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 手动呈现按钮。