【问题标题】:Unable to get access Token linkedin Oauth无法在 Oauth 中获取访问令牌链接
【发布时间】:2019-03-01 06:04:02
【问题描述】:

我知道有些人会发表评论,比如这篇文章是很多问题的重复,但我已经尝试了很多方法来在linkedin Oauth 中实现访问令牌。解释我的尝试。

1) 我关注的是官方文档的Linkedin Oauth2

2) 我已成功从第 2 步获取授权代码,并将该代码传递到第 3 步,以交换身份验证代码以获取访问令牌。但我收到以下错误 {"error_description":"缺少必需参数,包含无效参数值,参数不止一次。:无法检索访问令牌:appId 或重定向 uri 与授权码不匹配或授权码已过期","error": “无效请求”}

3) 根据一些链接,我需要在标题中设置内容类型。Link which tells to set content-type is missing

4) 然后我尝试调用https://www.linkedin.com/uas/oauth2/accessToken 这个服务而不是POStGET。并将数据作为 queryParams 传递。

5) 一些 link 说 oauth 代码将在 20 秒内过期,所以我检查了一下,我正在不到 1 秒内调用访问令牌。

6) 如果我在 Body 参数中传递数据,如下所示并将 url 用作 https://www.linkedin.com/uas/oauth2/accessToken

var postData = {
                grant_type: "authorization_code",
                code: authCode,
                redirect_uri: 'https%3A%2F%2Foauthtest-mydeployed-app-url',
                client_id: 'my_client_id',
                client_secret: 'secret_key'
            };

7) 使用Get 拨打我的网址我试过 https://www.linkedin.com/uas/oauth2/accessToken?grant_type=authorization_code&code='+authCode+'&redirect_uri=https%3A%2F%2Foauthtest-mydeployed-app-url&client_id=my_client_id&client_secret=secret_key

即使状态码是 200,我仍然收到错误消息(使用 GET api) 如果POSt 通过在正文中传递postData 我收到错误请求400 状态码

不明白为什么我没有获得访问代码。我已经阅读了很多解决方案。 按要求共享代码。

sap.ui.define([
	"sap/ui/core/mvc/Controller",
	"sap/m/MessageToast"
], function (Controller, MessageToast) {
	"use strict";

	return Controller.extend("OauthTest.OauthTest.controller.View1", {
		onPress: function (evt) {
			var sPath =
				'https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=my_client_id&redirect_uri=https%3A%2F%2Foauthtest-mydeployed-app-url&state=DCEeFWf45A53sdfKef424&scope=r_basicprofile';
			window.location.href = sPath;
            var oRouter = new sap.ui.core.UIComponent.getRouterFor(this);
			oRouter.navTo("View2", {
				"username": "Test"
			});
			MessageToast.show(evt.getSource().getId() + " Pressed");
		},
		
		//after user allows access, user will be redirected to this app with code and state in URL
		//i'm fetching code from URL in below method(call is happening in max.569ms)
		
		onAfterRendering: function () {
			var currentUrl = window.location.href;
			var url = new URL(currentUrl);
			var authCode = url.searchParams.get("code");
			if (authCode !== undefined && authCode !== null) {
				var postData = {
					grant_type: "authorization_code",
					code: authCode,
					redirect_uri: 'https%3A%2F%2Foauthtest-mydeployed-app-url',
					client_id: 'my_client_id',
					client_secret: 'secret_key'
				};
				
			/*	var accessTokenUrl = 'https://www.linkedin.com/uas/oauth2/accessToken?grant_type=authorization_code&code=' + authCode +'&redirect_uri=https%3A%2F%2Foauthtest-mydeployed-app-url&client_id=my_client_id&client_secret=secret_key';*/

				var accessTokenUrl = 'https://www.linkedin.com/uas/oauth2/accessToken';

				$.ajax({
					url: accessTokenUrl,
					type: "POST",
					beforeSend: function (xhr) {
						xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
					},
					data: postData,
					success: function (data, textStatus, jqXHR) {
						console.log(data);
						alert('success');
					},
					error: function (jqXHR, textStatus, errorThrown) {
						console.log(errorThrown);
						alert('error');
					}
				});
			}
		}

	});
});

帮助将被appriciated..!!!

【问题讨论】:

  • 如错误提及,您必须检查以下事项 1) 确保访问代码未过期。 2) 生成访问令牌的 URL 将是:linkedin.com/oauth/v2/accessToken 3) 方法:POST 4) POSTFIELDS 如下 i) grant_type='authorization_code' ii) code="" iii) redirect_uri= iv) client_id= v) client_secret=
  • 以下是示例代码(PHP 中)供您参考。 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,"linkedin.com/oauth/v2/accessToken"); curl_setopt($ch, CURLOPT_POST, 0); curl_setopt($ch, CURLOPT_POSTFIELDS,"grant_type=authorization_code&code=".$code."&redirect_uri=".env('LINKEDIN_REDIRECT_URL' )."&client_id=".env('LINKEDIN_CLIENT_ID')."&client_secret=".env('LINKEDIN_CLIENT_SECRET')); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $server_output = curl_exec ($ch); curl_close ($ch );
  • @KetavChotaliya,我根据您的第一条评论尝试了发布,在这种情况下,我收到错误的请求和状态代码为 400。我们也需要在标题中传递 content-type。我检查了我的访问代码没有过期
  • 需要仔细调试...流程和我上面说的一样。如果可能的话,分享你的代码。
  • @KetavChotaliya 添加了代码以提高清晰度

标签: api sapui5 linkedin


【解决方案1】:

经过这么多搜索,我终于很高兴发布我的答案。 我所做的每一步都是正确的,但我在这里缺少一件事,Linkedin API 不支持 CORS。

我尝试实现 Javascript SDK,它就像魅力一样。但 API 不是。 然后我发现Link 很有帮助,它说我需要通过允许 CORS 从后端实现 Rest API,而不是从前端实现。

确保遵循我在上面的帖子中提到的所有要点。 对于Allow CORS,请点击此链接。根据LinkedIn条款data can be accessible

,您将获得数据,但只有用户的基本资料

希望这篇文章可以帮助人们有时间搜索更多

【讨论】:

  • 我要说的第一件事是,由于对 CORS yap 的浏览器保护,对 api 的 javascript 调用可能会失败。无论如何,我这边的问题只是时间问题。 stackoverflow.com/questions/28094926/…
猜你喜欢
  • 1970-01-01
  • 2019-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-10
  • 1970-01-01
相关资源
最近更新 更多