linkedin voyager api:从代码调用 API 时响应无效
linkedin voyager api : Invalid response when calling API from code
我正在使用这个 linkedin-private-api
连接到 linkedin voyager 的库 api.
当我 运行 此代码时,我得到一个无效的 json 响应,类似于这样的内容:�/J�>�2:��������i ��f{��|tV4����>X��+��0
但是当我使用 Postman 时,我得到了有效的 json 响应
"data": {
"metadata": {
"type": "TypeaheadFindByType",
"id": "ff39d918-5e52-4a14-9aa3-6c37c72cb81b",
"$type": "com.linkedin.voyager.typeahead.TypeaheadMetadata"
},
"entityUrn": "urn:li:collectionResponse:IFrGi77656cS3A4SqIFwTMfpqxVjguPOPn6DHgNnpiQ=",
"elements": [
{
"targetUrn": "urn:li:fs_geo:103688978",
"text": {
"text": "Sousse Governorate, Tunisia",
"$type": "com.linkedin.pemberly.text.AttributedText"
},
"dashTargetUrn": "urn:li:fsd_geo:103688978",
"type": "GEO",
"trackingId": "JLoPDNEmRo2BEEQr26vinw==",
"$type": "com.linkedin.voyager.typeahead.TypeaheadHitV2"
}
}
这是完整的 nodejs 测试代码:
const {Client} = require('linkedin-private-api');
const https = require('https')
test();
async function test() {
const client = new Client();
await client.login.userPass({
username: "username",
password: "password"
});
var _headers = client.request.request.defaults.headers
const options = {
hostname: 'www.linkedin.com',
path: '/voyager/api/typeahead/hitsV2?keywords=USA&origin=OTHER&q=type&queryContext=List(geoVersion-%3E3,bingGeoSubTypeFilters-%3EMARKET_AREA%7CCOUNTRY_REGION%7CADMIN_DIVISION_1%7CCITY)&type=GEO',
method: 'GET',
headers: _headers
}
https.get(options, function (res) {
var json = '';
res.on('data', function (chunk) {
json += chunk;
});
res.on('end', function () {
if (res.statusCode === 200) {
try {
// data is available here:
console.log(json);
} catch (e) {
console.log('Error parsing JSON!');
}
} else {
console.log('Status:', res.statusCode);
}
});
}).on('error', function (err) {
console.log('Error:', err);
});
}
另一个端点使用了 gzip 加密。但是这个端点没有。所以我只是将 accept-encoding 设置为空 _headers["accept-encoding"] = ""。令人困惑的是,即使编码错误,邮递员也会显示正确的响应。
我正在使用这个 linkedin-private-api 连接到 linkedin voyager 的库 api.
当我 运行 此代码时,我得到一个无效的 json 响应,类似于这样的内容:�/J�>�2:��������i ��f{��|tV4����>X��+��0
但是当我使用 Postman 时,我得到了有效的 json 响应
"data": {
"metadata": {
"type": "TypeaheadFindByType",
"id": "ff39d918-5e52-4a14-9aa3-6c37c72cb81b",
"$type": "com.linkedin.voyager.typeahead.TypeaheadMetadata"
},
"entityUrn": "urn:li:collectionResponse:IFrGi77656cS3A4SqIFwTMfpqxVjguPOPn6DHgNnpiQ=",
"elements": [
{
"targetUrn": "urn:li:fs_geo:103688978",
"text": {
"text": "Sousse Governorate, Tunisia",
"$type": "com.linkedin.pemberly.text.AttributedText"
},
"dashTargetUrn": "urn:li:fsd_geo:103688978",
"type": "GEO",
"trackingId": "JLoPDNEmRo2BEEQr26vinw==",
"$type": "com.linkedin.voyager.typeahead.TypeaheadHitV2"
}
}
这是完整的 nodejs 测试代码:
const {Client} = require('linkedin-private-api');
const https = require('https')
test();
async function test() {
const client = new Client();
await client.login.userPass({
username: "username",
password: "password"
});
var _headers = client.request.request.defaults.headers
const options = {
hostname: 'www.linkedin.com',
path: '/voyager/api/typeahead/hitsV2?keywords=USA&origin=OTHER&q=type&queryContext=List(geoVersion-%3E3,bingGeoSubTypeFilters-%3EMARKET_AREA%7CCOUNTRY_REGION%7CADMIN_DIVISION_1%7CCITY)&type=GEO',
method: 'GET',
headers: _headers
}
https.get(options, function (res) {
var json = '';
res.on('data', function (chunk) {
json += chunk;
});
res.on('end', function () {
if (res.statusCode === 200) {
try {
// data is available here:
console.log(json);
} catch (e) {
console.log('Error parsing JSON!');
}
} else {
console.log('Status:', res.statusCode);
}
});
}).on('error', function (err) {
console.log('Error:', err);
});
}
另一个端点使用了 gzip 加密。但是这个端点没有。所以我只是将 accept-encoding 设置为空 _headers["accept-encoding"] = ""。令人困惑的是,即使编码错误,邮递员也会显示正确的响应。