【发布时间】: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 这个服务而不是POSt 到GET。并将数据作为 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 添加了代码以提高清晰度