I keep getting a 401 error when I try to upload a file to YouTube on my application. This is the YouTube docs: https://developers.google.com/youtube/v3/docs/videos/insert
I'm using HTTP as the method for uploading. This is what YouTube suggests you to upload a video as you can do this without any meta data:
POST https://youtube.googleapis.com/youtube/v3/videos?key=[YOUR_API_KEY] HTTP/1.1Authorization: Bearer [YOUR_ACCESS_TOKEN]Accept: application/jsonContent-Type: application/json
I have my application setup in a react/typescript environment, with a node/express backend.
This is how I have written the api call using axios:
export const UploadNewYouTubeVideo = (uploadFile: File, token: string) => { return axios.post( `https://youtube.googleapis.com/youtube/v3/videos?key=${process.env.REACT_APP_YOUTUBE_API_KEY}`, uploadFile, { headers: { Authorization: `Bearer ${token}`,"Content-Type": "application/octet-stream", }, } );};
I know the access token passed is correct for the YouTube channel as it successfully allows me to view/delete existing videos with that token.
For the frontend, I have a very simple useState hook that changes to the inputted file on input:
<input id="file-upload" name="file-upload" type="file" className="sr-only" onChange={(e) => e?.target.files !== null && setUploadFile(e?.target?.files[0]) } />
This corresponds to a simple function that calls that api when a button called "Upload" is pushed:
const UploadVideo = () => { if (uploadFile) { setIsUploading(true); return UploadNewYouTubeVideo(uploadFile, token) .then((res) => console.log(res)) .catch((e) => console.log("Error Uploading", e.message)); } };
This is the 401 error I keep getting (note: I hid my secret key here with XXXX):
xhr.js:210 POST https://youtube.googleapis.com/youtube/v3/videos?key=XXXXXXXXXXXXXX net::ERR_FILE_NOT_FOUND
The file is definitely being saved to state and passed to the post request:
I'd appreciate any help you guys may have.
EDIT: I'm getting a 400 error now, which I think is the mediaBodyRequired error. Which im puzzled about as I'm passing through the file for upload. Any thoughts?