我正在尝试使用 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/