无法访问令牌linkedin Oauth [英] Unable to get access Token linkedin Oauth
问题描述
我知道有些人会发表这样的评论,就像这篇文章是许多问题的重复一样,但是我已经尝试了许多方法来在linkedin Oauth中实现访问令牌.解释我的尝试.
I know some will put comment like this post is duplicate of so many questions, but I've tried many ways to achieve Access Token in linkedin Oauth. Explaining what i tried.
1)我关注的是官方文档的 Linkedin Oauth2
1) I'm following it's official doc's Linkedin Oauth2
2)我成功地从步骤2获取了授权代码,并将该代码传递给了步骤3,以交换用于获取访问令牌的Auth代码.但是我收到以下错误 {"error_description":缺少必需的参数,包括无效的参数值,参数多次..:无法检索访问令牌:appId或重定向uri与授权码不匹配或授权码已过期",错误": "invalid_request"}
2) I'm successfully getting Authorization code from step 2 and passing that code to step 3 for exchanging Auth code for getting Access Token. But i'm getting following error {"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) According to some links i need to set content-type in the header.Link which tells to set content-type is missing
4)然后我尝试调用https://www.linkedin.com/uas/oauth2/accessToken
而不是POSt
到GET
的此服务.并将数据作为queryParams传递.
4)Then i tried calling https://www.linkedin.com/uas/oauth2/accessToken
this service instead of POSt
to GET
. And passing data as queryParams.
5)一些链接说,oauth代码在20秒后过期,因此我已经检查过,我正在不到1秒钟的时间内发出访问令牌的呼叫.
5) Some link says oauth code expires in 20 sec, So i've checked, i'm making call for access token in less that 1 sec.
6)如果我在如下所示的Body参数中传递数据,并使用url作为https://www.linkedin.com/uas/oauth2/accessToken
6) And if i pass data in Body params like as below and used url as 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
7) With Get
call my url i tried
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
状态代码
Still i'm getting Error even though status code is 200, i'm getting that error(with GET
api)
and If POSt
by passing postData
in body i'm getting bad request 400
status code
不理解为什么我没有访问代码.我已经阅读了许多解决方案. 根据要求共享代码.
Not understanding why m I not getting access code. I've read many solutions. Sharing code as requested.
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');
}
});
}
}
});
});
将寻求帮助..!!!
Help will be appriciated..!!!
推荐答案
最后,经过如此多的搜索,我很高兴发表我的答案. 我所做的每一步都是正确的,但是我在这里缺少的一件事就是Linkedin API不支持CORS.
Finally I am happy to post my answer after so much search. Every step I did is correct only, but one thing I was missing here like, Linkedin API doesn't supports CORS.
我尝试实现Javascript SDK,该工具的工作原理类似于魅力.但是API并非如此. 然后,我发现链接很有帮助,该链接说我需要通过允许从后端实现Rest API CORS,而不是前端.
I tried implementing Javascript SDK, that works like charm. But API wasn't. Then I found very helpful Link which says I need to implement Rest API from backend by allowing CORS, not from front end.
确保遵循我在帖子中上面提到的所有要点. 对于允许CORS ,请点击此链接.您将获得数据,但根据LinkedIn条款,只有用户的基本个人资料数据可以访问
Make sure to follow all the points which I mentioned above in my post. And for Allow CORS follow this link. You will get data but only basic profile of user according to LinkedIn Terms data can be accessible
希望这篇文章可以帮助某人时间搜索更多
Hope this post may help someones time to search more
这篇关于无法访问令牌linkedin Oauth的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!