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

sha*_*e93 4 youtube google-api node.js axios

我正在尝试使用 google api v3 从频道获取一些 youtube 视频数据。

\n

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

\n
{\n  "kind": "youtube#searchListResponse",\n  "etag": "lP1l3Vk-JQNUN9moIFDXVlQt9uY",\n  "nextPageToken": "CAoQAA",\n  "regionCode": "NL",\n  "pageInfo": {\n    "totalResults": 4109,\n    "resultsPerPage": 10\n  },\n  "items": [\n    {\n      "kind": "youtube#searchResult",\n      "etag": "DIARyKavv5X4EEGZzoIYKd2hzGY",\n      "id": {\n        "kind": "youtube#video",\n        "videoId": "N54TzfCbJOU"\n      },\n      "snippet": {\n        "publishedAt": "2022-11-22T16:00:07Z",\n        "channelId": "UCBYyJBCtCvgqA4NwtoPMwpQ",\n        "title": "The Wild Project #171 | \xc2\xbfEnga\xc3\xb1aron a Jordi?, Qatar rid\xc3\xadculo inaugural, Desastre con Ley del S\xc3\xad es S\xc3\xad",\n        "description": "Como cada semana, nueva tertulia en The Wild Project, comentando las noticias m\xc3\xa1s destacadas de los \xc3\xbaltimos d\xc3\xadas. En este ...",\n        "thumbnails": {\n          "default": {\n            "url": "https://i.ytimg.com/vi/N54TzfCbJOU/default.jpg",\n            "width": 120,\n            "height": 90\n          },\n          "medium": {\n            "url": "https://i.ytimg.com/vi/N54TzfCbJOU/mqdefault.jpg",\n            "width": 320,\n            "height": 180\n          },\n          "high": {\n            "url": "https://i.ytimg.com/vi/N54TzfCbJOU/hqdefault.jpg",\n            "width": 480,\n            "height": 360\n          }\n        },\n        "channelTitle": "The Wild Project",\n        "liveBroadcastContent": "none",\n        "publishTime": "2022-11-22T16:00:07Z"\n      }\n    }, ...ETC\n
Run Code Online (Sandbox Code Playgroud)\n

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

\n

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

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

我收到 200 状态响应,但在数据正文中我看到:

\n
data: '\\x1F\xef\xbf\xbd\\b\\x00\\x00\\x00\\x00\\x00\\x02\xef\xbf\xbd\xef\xbf\xbdKs\xef\xbf\xbd\\x11\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd)P\xde\xaa\xef\xbf\xbd\xef\xbf\xbd\\x10\xef\xbf\xbd\xef\xbf\xbd$\xef\xbf\xbd\xef\xbf\xbd\xcb\xb6dy\xef\xbf\xbd\\x1E\xef\xbf\xbd\xca\xa6\\\\\\x10\\t\xef\xbf\xbd\xef\xbf\xbdA\xef\xbf\xbd\\x06@=\xef\xbf\xbd\xef\xbf\xbd/\xef\xbf\xbd\xef\xbf\xbd\\x1Es\xef\xbf\xbdCjo{IU\xef\xbf\xbd\xef\xbf\xbd\xd2\xa0$\\x07\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd"N\xef\xbf\xbdV\xef\xbf\xbd\xef\xbf\xbd\\x19\\n' +\n    'h\\x00\\x7F\xef\xbf\xbd\xef\xbf\xbd?..ETC\n
Run Code Online (Sandbox Code Playgroud)\n

有人有什么主意吗?

\n

Ben*_*Vue 7

在 v1.2.1 中修正了这个错误。

\n

您需要Accept-Encoding在 axios.get 标头中添加“application/json”。

\n

axios默认是gzip

\n

这是演示代码。

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

这是我的 API 密钥(减少)

\n
{\n    "API_KEY" : "AIz..your-API-key...xUs"\n}\n
Run Code Online (Sandbox Code Playgroud)\n

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

\n
$ node get-video.js\n{\n    "kind": "youtube#searchListResponse",\n    "etag": "wu-L26udDmyHfzoJalUtCRCGeKs",\n    "nextPageToken": "CAEQAA",\n    "regionCode": "US",\n    "pageInfo": {\n        "totalResults": 4103,\n        "resultsPerPage": 1\n    },\n    "items": [\n        {\n            "kind": "youtube#searchResult",\n            "etag": "DIARyKavv5X4EEGZzoIYKd2hzGY",\n            "id": {\n                "kind": "youtube#video",\n                "videoId": "N54TzfCbJOU"\n            },\n            "snippet": {\n                "publishedAt": "2022-11-22T16:00:07Z",\n                "channelId": "UCBYyJBCtCvgqA4NwtoPMwpQ",\n                "title": "The Wild Project #171 | \xc2\xbfEnga\xc3\xb1aron a Jordi?, Qatar rid\xc3\xadculo inaugural, Desastre con Ley del S\xc3\xad es S\xc3\xad",\n                "description": "Como cada semana, nueva tertulia en The Wild Project, comentando las noticias m\xc3\xa1s destacadas de los \xc3\xbaltimos d\xc3\xadas. En este ...",\n                "thumbnails": {\n                    "default": {\n                        "url": "https://i.ytimg.com/vi/N54TzfCbJOU/default.jpg",\n                        "width": 120,\n                        "height": 90\n                    },\n                    "medium": {\n                        "url": "https://i.ytimg.com/vi/N54TzfCbJOU/mqdefault.jpg",\n                        "width": 320,\n                        "height": 180\n                    },\n                    "high": {\n                        "url": "https://i.ytimg.com/vi/N54TzfCbJOU/hqdefault.jpg",\n                        "width": 480,\n                        "height": 360\n                    }\n                },\n                "channelTitle": "The Wild Project",\n                "liveBroadcastContent": "none",\n                "publishTime": "2022-11-22T16:00:07Z"\n            }\n        }\n    ]\n}\n
Run Code Online (Sandbox Code Playgroud)\n