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

qrjkbowd  于 2022-11-29  发布在  iOS
关注(0)|答案(2)|浏览(102)

我试图从一个使用谷歌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�\b\x00\x00\x00\x00\x00\x02��Ks�\x11���)Pު��\x10��$��˶dy�\x1E�ʦ\\\x10\t��A�\x06@=��/��\x1Es�Cjo{IU��Ҡ$\x07����"N�V��\x19\n' +
    'h\x00\x7F��?..ETC

有人知道吗?

waxmsbnn

waxmsbnn1#

您需要在axios.get头中添加带有“application/json”的Accept-Encoding
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();

这是我的API密钥(简化)

{
    "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"
            }
        }
    ]
}
c8ib6hqw

c8ib6hqw2#

检查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,您应该可以开始了。

相关问题