Axios请求google api v3返回加密(?)数据



我正试图从使用google api v3的频道获得一些youtube视频数据。

当我在浏览器中运行url时,我得到的信息看起来很好。https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=UCBYyJBCtCvgqA4NwtoPMwpQ&maxResults=10&order=date&type=video&key={MYAPIKEY}

{
"kind": "youtube#searchListResponse",
"etag": "lP1l3Vk-JQNUN9moIFDXVlQt9uY",
"nextPageToken": "CAoQAA",
"regionCode": "NL",
"pageInfo": {
"totalResults": 4109,
"resultsPerPage": 10
},
"items": [
{
"kind": "youtube#searchResult",
"etag": "DIARyKavv5X4EEGZzoIYKd2hzGY",
"id": {
"kind": "youtube#video",
"videoId": "N54TzfCbJOU"
},
"snippet": {
"publishedAt": "2022-11-22T16:00:07Z",
"channelId": "UCBYyJBCtCvgqA4NwtoPMwpQ",
"title": "The Wild Project #171 | ¿Engañaron a Jordi?, Qatar ridículo inaugural, Desastre con Ley del Sí es Sí",
"description": "Como cada semana, nueva tertulia en The Wild Project, comentando las noticias más destacadas de los últimos días. En este ...",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/N54TzfCbJOU/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/N54TzfCbJOU/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/N54TzfCbJOU/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "The Wild Project",
"liveBroadcastContent": "none",
"publishTime": "2022-11-22T16:00:07Z"
}
}, ...ETC

如果我在终端上运行curl调用,响应也很好。

但是,当我尝试在Axios NodeJS中运行它时,我得到的响应是不同的,数据部分似乎是加密的。

response = await this.axios.get(`https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=UCBYyJBCtCvgqA4NwtoPMwpQ&maxResults=10&order=date&type=video&key={MYAPIKEY}`);

我得到一个200状态响应,但在数据体中我看到这样:

data: 'x1F�bx00x00x00x00x00x02��Ks�x11���)Pު��x10��$��˶dy�x1E�ʦ\x10t��A�x06@=��/��x1Es�Cjo{IU��Ҡ$x07����"N�V��x19n' +
'hx00x7F��?..ETC

有人知道吗?

在v1.2.1中修复了此错误。

您需要在axios中添加Accept-Encoding与'application/json'。头。

axios默认为gzip

这是演示代码。

const axios = require('axios')
const config = require('./my-key.json');
const getVideo = async () => {
try {
const resp = await axios.get(
'https://www.googleapis.com/youtube/v3/search',
{
headers: {
'Content-Type': 'application/json',
'Accept-Encoding': 'application/json',
},
params: {
'part': 'snippet',
'channelId': 'UCBYyJBCtCvgqA4NwtoPMwpQ',
'maxResults': '1',
'order': 'date',
'type': 'video',
'key': config.API_KEY
}
}
);
console.log(JSON.stringify(resp.data, null, 4));
} catch (err) {
// Handle Error Here
console.error(err);
}
};
getVideo();

This my API key (reduced)

{
"API_KEY" : "AIz..your-API-key...xUs"
}

结果(我减少了视频的数量是一个)

$ node get-video.js
{
"kind": "youtube#searchListResponse",
"etag": "wu-L26udDmyHfzoJalUtCRCGeKs",
"nextPageToken": "CAEQAA",
"regionCode": "US",
"pageInfo": {
"totalResults": 4103,
"resultsPerPage": 1
},
"items": [
{
"kind": "youtube#searchResult",
"etag": "DIARyKavv5X4EEGZzoIYKd2hzGY",
"id": {
"kind": "youtube#video",
"videoId": "N54TzfCbJOU"
},
"snippet": {
"publishedAt": "2022-11-22T16:00:07Z",
"channelId": "UCBYyJBCtCvgqA4NwtoPMwpQ",
"title": "The Wild Project #171 | ¿Engañaron a Jordi?, Qatar ridículo inaugural, Desastre con Ley del Sí es Sí",
"description": "Como cada semana, nueva tertulia en The Wild Project, comentando las noticias más destacadas de los últimos días. En este ...",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/N54TzfCbJOU/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/N54TzfCbJOU/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/N54TzfCbJOU/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "The Wild Project",
"liveBroadcastContent": "none",
"publishTime": "2022-11-22T16:00:07Z"
}
}
]
}

检查您的Axios版本。我用的是:

"axios": "^1.1.3",

和每次npm安装我运行它总是得到最新的版本- 1.2.0。

该版本有一个缺陷:https://github.com/axios/axios/issues/5296

"the config transformResponse sends data as binary instead of string"

回到1.1.3版本,你应该就可以了。

我在Axios 1.2.11.2.0中也遇到了这个问题

恢复到1.1.3版本工作。运行:

npm install axios@1.1.3

最新更新