我正在尝试在我的应用程序中添加 github 登录。因此,在本地计算机上进行测试时,我将回调网址设置为 http://localhost:8000/login/callback/
.
之后,我向我的页面添加一个登录链接,如下所示:<a href="https://github.com/login/oauth/authorize?client_id=CLIENT_ID&scope=user&redirect_uri=http://localhost:8000/login/callback">Login</a>
,还有
$routeProvider.when('/login/callback', {
controller: 'loginCtrl'
});
它成功返回给我一个带有如下链接的代码:http://localhost:8000/login/callback?code=cd07ff3b70f5d1d1b8a2
。但是angularjs无法正确响应。
Error response
Error code 404.
Message: File not found.
Error code explanation: 404 = Nothing matches the given URI.
我还尝试将我的redirect_url设置为http://localhost:8000/#/login/callback
,但是返回链接是这样的 http://localhost:8000/?code=61e9c8b73c073a0bccc0/#/login/callback
我需要代码出现在网址末尾而不是中间,以便我可以使用 $location.search().code
获取我的代码。如何正确设置?
想更改我的redirect_uri 或更改我的路由器吗?
最佳答案
@lowerkey 我仍然不知道如何处理这个问题,但我认为这是因为页面被重新加载。
当您未将 html5Mode
设置为 true
时,网址处将为 #
,如果您重新加载页面,则#
会将信息跟踪到 $scope
以与 htmlHistory
api 配合使用,以便应用程序即使在页面刷新时也能正常工作。
但是一旦将 html5Mode
设置为 true
,就没有 #
来存储任何 $scope
信息,当页面重新加载时,Angular无法找到相应的范围,因此返回404
。
您可以查看$location section on angularjs guide ,它有一个部分解释了为什么会发生这种情况。
Page reload navigation
The $location service allows you to change only the URL; it does not allow you to reload the page. When you need to change the URL and reload the page or navigate to a different page, please use a lower level API, $window.location.href.
关于angularjs - github 重定向_uri 与 angularjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17509413/