无法获取领英Oauth访问令牌。

2 浏览
0 Comments

无法获取领英Oauth访问令牌。

我知道有些人会在评论中提到这篇文章与很多问题的重复,但我已经尝试了许多方法来实现领英OAuth的访问令牌。我将解释我尝试过的方法。

1)我正在遵循其官方文档的领英OAuth2

2)我成功地从第2步获取了授权代码,并将该代码传递到第3步以交换授权代码以获取访问令牌。但我收到以下错误

{\"error_description\":\"missing required parameters, includes an invalid parameter value, parameter more than once. : Unable to retrieve access token : appId or redirect uri does not match authorization code or authorization code expired\",\"error\":\"invalid_request\"}

3)根据一些链接,我需要在头部设置内容类型。告诉设置内容类型的链接已经失踪

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

5)一些链接表示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调用我的url我尝试

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时会遇到该错误。

如果通过传递postData来进行POSt,我会收到错误的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');
					}
				});
			}
		}
	});
});

将不胜感激的!

admin 更改状态以发布 2023年5月25日
0
0 Comments

经过长时间的搜索,我终于很高兴地发布了我的答案。
我所做的每一步都是正确的,但有一件事我在这里错过了,就是Linkedin API不支持CORS。

我尝试了实现Javascript SDK,它表现得很棒。但是API并不是。
然后我发现了一个非常有用的链接,它说我需要通过允许CORS从后端实现Rest API,而不是从前端。

确保按照我在帖子中提到的所有要点进行操作。
至于允许CORS,请按照此链接进行操作。根据LinkedIn条款,仅有基本个人资料可访问数据

希望这篇文章可以帮助某些人节省搜索时间

0