我知道有些人会评论说这篇文章是许多问题的重复,但我已经尝试了许多方法来实现领英Oauth中的访问令牌。解释一下我尝试过的。
1)我正在遵循官方文档Linkedin 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)根据一些链接,我需要在标题中设置内容类型。 Link which tells to set content-type is missing
4)然后我尝试调用https://www.linkedin.com/uas/oauth2/accessToken
这个服务,而不是POSt
到GET
。并将数据作为查询参数传递。
5) 有些链接说oauth代码在20秒后过期,所以我检查了一下,我在不到1秒的时间内调用了访问令牌。
6) 如果我像下面这样传递数据并使用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');
}
});
}
}
});
});
需要帮助,感激不尽..!!!
content-type
。我检查了我的访问代码并没有过期。 - Prasanna