javascript - AngularJS 和 Google OAuth2 redirect_uri

标签 javascript angularjs oauth-2.0 google-api

我正在尝试使用 AngularJS 和 Google 的 OAuth2 进行身份验证来创建一个简单的应用程序。

由于弹出窗口阻止问题和移动友好性,我决定不使用 Google APIs Client Library for JavaScript .

这让我可以选择完全重定向到 OAuth2 endpoint在谷歌,并使用 access_token 将用户重定向回我的应用程序。

我认为这会很好用。重定向 URI 将是“http://myapp.com/#/register” ' 带有附加的 'access_token' 查询参数。然后我会使用 access_token 并将用户定向到我应用程序中的其他地方。

这没有用,因为 Google API 凭据 (http://developers.google.com/console) 不喜欢在重定向 URI 中使用“#”。

然后我尝试使用

关闭 URI 中的“#”要求
$locationProvider.html5Mode(true);

这也不起作用,因为(在 Chrome 中)显式浏览到“http://myapp.com/register” ' 无法被我的 Angular 路线识别。

关于我应该如何实现这一点有什么想法吗?

最佳答案

以下对我有用,在我的应用程序中将 html5 模式设置为 true。

此代码进入重定向页面的 Controller (确保注入(inject) $window 服务):

//Get parameters from hash fragment
var params = {};
if ($window.location.hash.indexOf('#') === 0) {
    var regex = /([^&=]+)=([^&]*)/g, m;
    while (m = regex.exec($window.location.hash.substr(1))) {
        params[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
    }
}

//Validate params, store access token etc.
if (!params.access_token) {
    //...
}

如果您的应用程序无法识别非# 路由,请确保您的服务器启用了重写引擎,并将所有相关请求重定向到您的 Angular 应用程序主 HTML 文件。

关于javascript - AngularJS 和 Google OAuth2 redirect_uri,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21079040/

相关文章:

javascript - Internet Explorer (IE) 中的脚本挂起并且对空缓存不负责任(在缓存中工作正常)

javascript - 如何检测未声明的变量与已声明但未初始化的变量

javascript - 使用JSP/Servlet/Ajax下载文件而不刷新页面

javascript - 单个元素上具有新/隔离范围的两个属性指令

asp.net-core - 我们如何将 identityserver4 和 web api 集成到同一个项目(端口)中,而不是为每个项目创建不同的项目?

javascript - 通过 dojo 加载自定义 Typescript 类不起作用

angularjs - angular2 中的 package.json 是否需要以任何方式启动应用程序?

javascript - div 上的基本 ng-show 不起作用

php - Google_Service_OAuth2 在 PHP 中是 'Undefined type'

asp.net-mvc - 什么最适合认证 ASP.NET MVC REST API? OAuth 2.0 还是 OAuth 1.0?