NodeJS linkedin voyager API:从代码调用API时响应无效

u5i3ibmn  于 2023-06-22  发布在  Node.js
关注(0)|答案(1)|浏览(78)

我正在使用这个linkedin-private-API库连接到linkedin voyager api。
当我运行这段代码时,我得到了一个无效的JSON响应,类似于这样:�/�>�2:���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������|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);
});

}
zbwhf8kr

zbwhf8kr1#

另一端点使用GZIP加密。但该端点没有。所以我只是将accept-encoding设置为空**_headers[“accept-encoding”] =“"**。令人困惑的是,即使编码错误, Postman 也显示了正确的响应。

相关问题