LinkedIn API OAUTH 返回"grant_type"错误



我对编码很陌生,但试图使用LinkedIn的API编写一个简单的脚本,该脚本将把组织的追随者数量拉入谷歌应用程序脚本。在我可以查询API之前,我必须使用LinkedIn API中解释的誓言进行身份验证。

这个函数返回一个错误响应

function callLinkedAPI () {
var headers = {
"grant_type": "client_credentials",
"client_id": "78ciob33iuqepo",
"client_secret": "deCgAOhZaCrvweLs"
}
var url = `https://www.linkedin.com/oauth/v2/accessToken/`
var requestOptions = {
'method': "POST",
"headers": headers,
'contentType': 'application/x-www-form-urlencoded',
'muteHttpExceptions': true
};
var response = UrlFetchApp.fetch(url, requestOptions);
var json = response.getContentText();
var data = JSON.parse(json);

console.log(json)
}

当我尝试发送报头通过我得到这个错误作为响应

{"error":"invalid_request","error_description":"A required parameter "grant_type" is missing"}

grant_type,client_id,client_secret不在请求头中。相反,尝试将它们放在POST请求的主体中,内容类型为x-www-form-urlencoded,就像您在发布的代码的标题中已经拥有的那样。

例如:

fetch('https://www.linkedin.com/oauth/v2/accessToken/', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
},
body: new URLSearchParams({
grant_type: 'client_credentials',
client_id: '78ciob33iuqepo',
client_secret: 'deCgAOhZaCrvweLs'
})
})
.then(response => response.json())
.then(responseData => {
console.log(JSON.stringify(responseData))
})

使用Apps Script,你应该像这样发送有效载荷:

例子:

function callLinkedAPI() {
var payload = {
"grant_type": "client_credentials",
"client_id": "78ciob33iuqepo",
"client_secret": "deCgAOhZaCrvweLs"
}

var url = `https://www.linkedin.com/oauth/v2/accessToken/`
var requestOptions = {
'method': "POST",
'contentType': 'application/x-www-form-urlencoded',
'muteHttpExceptions': true,
"payload":payload
};
var response = UrlFetchApp.fetch(url, requestOptions);
var json = response.getContentText();
var data = JSON.parse(json);
console.log(json)
}

相关内容

最新更新