【问题标题】:Ionic LinkedIn cordova authentication bad redirectIonic LinkedIn cordova 身份验证错误重定向
【发布时间】:2017-01-12 11:11:36
【问题描述】:
我正在尝试使用 LinkedIn 登录。我设法获得了访问令牌,但是在重定向到我告诉它的页面之前的一瞬间,我看到了这个错误
HTTP 错误 404.0 - 未找到
您要查找的资源已被删除、名称已更改或暂时不可用。
这是链接
http://localhost:80/callback?code=****&state=WQ7IIHGw
虽然 Ionic 在我的 localhost:8100 上工作,但我已在开发人员页面链接中将授权重定向 URL 设置为 http://localhost/callback
另外,
当我将链接中的重定向 URL 设置为除本地主机/回调之外的任何内容时,我收到此错误
redirect_uri 无效。此值必须与使用 API 密钥注册的 URL 匹配。
【问题讨论】:
标签:
angularjs
cordova
authentication
ionic-framework
【解决方案1】:
按照/验证以下步骤:
设置 LinkedIn 帐户开发者选项: https://www.linkedin.com/developer/apps/
添加应用程序后转到其设置:
- 在身份验证密钥中,您将获得
Client ID 和Client Secret
- 在授权重定向 URL 中,您必须输入:http://localhost/callback,因为
$cordovaOauth 将使用它
- 保存/更新帐户设置
设置 Ionic 应用程序:
安装cordovaOauthbower install ng-cordova-oauth -S
将<script src="lib/ng-cordova-oauth/dist/ng-cordova-oauth.js"></script>添加到index.html和'ngCordovaOauth'到apps.js
将$cordovaOauth 添加到控制器依赖项
-
向控制器添加功能(仅Client ID和Client Secret需要替换为LinkedIn开发帐户的数据):
$scope.linkedInLoginIonic = function () {
var linkedinUriApi = "https://api.linkedin.com/v1/people/~:(email-address,first-name)?format=json&oauth2_access_token=";
$cordovaOauth.linkedin("Client ID", "Client Secret", ['r_basicprofile', 'r_emailaddress'])
.then(function (success) {
// Here you will get the access_token
console.log('Granted access to linkedin');
console.log(JSON.stringify(success));
// In request below my default header is disabled - otherwise Linkedin API will reject request
$http(
{method: 'GET',
url: linkedinUriApi + success.access_token,
headers: {Authorization: undefined}
}).then(function (response) {
console.log(response);
}, function (error) {
console.log(error);
console.log('Ionic LinkedIn API request after successful login failed');
});
},
function (error) {
console.log(error);
});
};
提示:
- 请求只能通过设备或模拟器完成 - 在 Web 浏览器中,您将在控制台中获得:
Cannot authenticate via a web browser
- 如果您有默认标头,则在请求 LinkedIn API 时禁用它(身份验证顺利后)
- 如果认证成功后的请求失败,检查范围是否匹配所有:
- API 请求:这里我们在
https://api.linkedin.com/v1/people/~:(email-address,first-name)? 中请求address 和first-name
- 身份验证时设置的范围,参见示例
$cordovaOauth 部分['r_basicprofile', 'r_emailaddress']
- LinkedIn 开发者帐户中设置的范围
- 以最小范围(即仅电子邮件)执行最终 API 请求
- 记得调试模拟器/设备请求,这样您就可以看到问题出在请求/标头中,并将其与https://developer.linkedin.com/docs/oauth2 中指定的值进行比较