HTTP request from cURL example


#1

I trying to made this stackoverflow request using HTTP package

curl -H 'Content-Type: application/json' \
     -H "x-referer:https://artists.youtube.com" \
     "https://content.googleapis.com/youtubei/v1/browse?alt=json&key=AIzaSyCzEW7JUJdSql0-2V4tHUb6laYm4iAE_dM" \
     -d '{
     "context": {
          "client": {
               "clientName": "WEB_MUSIC_ANALYTICS",
               "clientVersion": "0.2",
               "theme": "MUSIC",
               "hl": "en",
               "gl": "FR",
               "experimentIds": []
          },
          "capabilities": {
          },
          "request": {
               "internalExperimentFlags": []
          }
     },
     "browseId": "FEmusic_analytics",
     "query": "chart_params_type=WEEK&perspective=CHART&flags=viral_video_chart&selected_chart=VIRAL_VIDEOS"
    }

Like this

import {HTTP} from 'meteor/http'

export default function (root, arg, context) {

  const response = HTTP.call('POST', 'https://content.googleapis.com/youtubei/v1/browse', {
    header: {
      'x-referer': 'https://artists.youtube.com',
      'content-type': 'application/json',
    },
    params: {
      "alt": 'json',
      "key": 'AIzaSyCzEW7JUJdSql0-2V4tHUb6laYm4iAE_dM',
      "browseId": "FEmusic_analytics",
      "context": {
        "client": {
          "clientName": "WEB_MUSIC_ANALYTICS",
          "clientVersion": "0.2",
          "theme": "MUSIC",
          "hl": "en",
          "gl": "FR",
          "experimentIds": []
        },
        "capabilities": {
        },
        "request": {
          "internalExperimentFlags": []
        }
      }
    },
    query: "chart_params_type=WEEK&perspective=CHART&flags=viral_video_chart&selected_chart=VIRAL_VIDEOS"
  })
  console.log({response})
}

but i got this response

"failed [403] { “error”: { “code”: 403, “message”: “Requests from referer \u003cempty\u003e are blocked.”, “status”: “PERMISSION_DENIED”, “details”: [ { “@type”: “type.googleapis.com/google.rpc.Help”, “links”: [ { “description”: “Google developer console API key”, “url”: “https://console.developers.google.com/project/786424913528/apiui/credential” } ] } ] } } "

what am I doing wrong :expressionless:


#2

My mistake! -_-
now work!

  const response = HTTP.call('POST', 'https://content.googleapis.com/youtubei/v1/browse', {
    headers: {
      'x-referer': 'https://artists.youtube.com',
      'content-type': 'application/json'
    },
    params: {
      "alt": 'json',
      "key": 'AIzaSyCzEW7JUJdSql0-2V4tHUb6laYm4iAE_dM'
    },
    data: {
      "browseId": "FEmusic_analytics",
      "context": {
        "client": {
          "clientName": "WEB_MUSIC_ANALYTICS",
          "clientVersion": "0.2",
          "theme": "MUSIC",
          "hl": "en",
          "gl": "FR",
          "experimentIds": []
        },
        "capabilities": {
        },
        "request": {
          "internalExperimentFlags": []
        }
      },
      query: "chart_params_type=WEEK&perspective=CHART&flags=viral_video_chart&selected_chart=VIRAL_VIDEOS"
    }
  })