无法获取领英Oauth访问令牌。
无法获取领英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
这个服务,而不是POSt
到GET
。并将数据作为查询参数传递。
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'); } }); } } }); });
将不胜感激的!