Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Axios request to google api v3 returns encrypted(?) data

I'm trying to get some youtube videos data from a channel using the google api v3.

When I run the url in my browser the information I get looks good. 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

If I run a curl call in my terminal the response is also good.

But, when I try to run it in Axios NodeJS the response I get is different, and the data portion seems encrypted.

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

I get a 200 status response, but in the body of the data I see this:

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

Does anyone have any idea?

like image 638
shake93 Avatar asked Nov 15 '25 15:11

shake93


1 Answers

In v1.2.1 fixed this error.

You need to add Accept-Encoding with 'application/json' in axios.get header.

The default of axios is gzip

This is demo code.

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"
}

Result (I reduced the number of video is one)

$ 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"
            }
        }
    ]
}
like image 106
Bench Vue Avatar answered Nov 18 '25 05:11

Bench Vue



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!