Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The request is missing a Valid API Key while using Youtube Data API v3

I am trying to get the key and show the result on console but its not working. I am using React.js for this purpose.

import axios from "axios";

const KEY = "MY_Key Here";

export default axios.create({
 baseURL: "https://www.googleapis.com/youtube/v3",
  params: {
   part: "snippet",
   maxResults: 5,
   key: KEY,
  },
});

and App.js contain this

onTermSubmit = async (term) => {
  const response = await youtube.get("/search", {
    params: {
      q: term
    },
  });
  console.log(response);
};

Everytime i execute this it says "the request is missing valid api key" domain="Global" reason-"forbidden" and status="Denied".

like image 595
Muhammad Hassaan Ather Avatar asked Aug 23 '26 05:08

Muhammad Hassaan Ather


2 Answers

make sure you add all those params: { part: "snippet", maxResults: 5, key: KEY, }

in your App.js and remove it from youtube.js params: { q: term },

here,

and end code of your App.js gonna look like

params: { part: "snippet", maxResults: 5, key: KEY, q: term }

its because after your baseURl from youtube.js it directly go for your App.js params which consist of only one thing thats {q} so pgm never gets a chance to actually read the key

like image 61
Dinesh Kumar Avatar answered Aug 25 '26 22:08

Dinesh Kumar


Try this!

const result = await youtube.get("/search", {
      params: {
        part: "snippet",
        maxResults: 5,
        key: KEY,
        q: term,
      },
    });

The problem is with the params part, it can't parse params from two different file I think.

like image 40
ranit Avatar answered Aug 25 '26 22:08

ranit



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!