AngularJS和Google OAuth2 redirect_uri

mor*_*are 10 javascript google-api oauth-2.0 angularjs

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

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

这使我可以选择在Google上完全重定向到OAuth2端点,并使用access_token将用户重定向回我的应用.

我认为这样可以正常工作.重定向URI将为" http://myapp.com/#/register ",并附加"access_token"查询参数.然后,我将使用access_token并将用户定向到我的应用程序中的其他位置.

这不起作用,因为Google API凭据(http://developers.google.com/console)不喜欢在重定向URI中使用"#".

然后,我试着通过使用来关闭URI中的'#'要求

$locationProvider.html5Mode(true);
Run Code Online (Sandbox Code Playgroud)

这也不起作用,因为我的Angular路线无法识别(在Chrome中)明确浏览到" http://myapp.com/register ".

有关如何实现这一点的任何想法?

Ada*_*eis 1

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

此代码进入重定向页面的控制器(确保注入 $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) {
    //...
}
Run Code Online (Sandbox Code Playgroud)

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