Home > Enterprise >  Youtube API insert video returns INVALID_ARGUMENT
Youtube API insert video returns INVALID_ARGUMENT

Time:11-13

I am trying to use the youtube api to allow a user to upload a video from a website I'm making.

This is the request I'm sending (almost exactly the same as the example from the documentation, for now):

curl --request POST 'https://www.googleapis.com/youtube/v3/videos?part=snippet,status&key=[MY-API-KEY]' \
  -H 'authorization: Bearer [THE-USERS-ACCESS-TOKEN]' \
  -H 'accept: application/json' \
  -H 'content-type: application/json' \
  -H 'origin: http://localhost:1234' \
  --data-raw '{"snippet":{"categoryId":"22","description":"Description of uploaded video.","title":"Test video upload."},"status":{"privacyStatus":"private"}}' \
  --compressed

The response is an error that lacks a bit of details:

{
  "error": {
    "code": 400,
    "message": "Request contains an invalid argument.",
    "errors": [
      {
        "message": "Request contains an invalid argument.",
        "domain": "global",
        "reason": "badRequest"
      }
    ],
    "status": "INVALID_ARGUMENT"
  }
}

I believe my api key and credentials are OK because I've tested them with other methods (that require user credentials), and they work fine.

The documentation is not super clear about where to put the actual video file's bytes. I'm sort of expecting the method to return a resumable upload url, but the doc says nothing about that. It states that Accepted Media MIME types are video/* and application/octet-stream, but also that you should provide a video resource in the request body, and all examples show a json body and content-type...

How is this method supposed to work ?

CodePudding user response:

I believe your goal is as follows.

  • You want to upload a video file with the resumable upload using YouTube API.
  • You want to achieve this using the curl command.

In this case, how about the following sample curl command?

In order to upload a video file to YouTube with the resumable upload using YouTube API, the following 2 processes are required to be done.

1. Retrieve the location URL.

As the 1st step, the location URL for uploading the video data is retrieved.

curl -X POST -i "https://www.googleapis.com/upload/youtube/v3/videos?uploadType=resumable&part=snippet,status" \
  -H "Authorization: Bearer ### Your access token ###" \
  -H "Content-Type: application/json; charset=UTF-8" \
  -d "{\"snippet\":{\"categoryId\":\"22\",\"description\":\"Description of uploaded video.\",\"title\":\"Test video upload.\"},\"status\":{\"privacyStatus\":\"private\"}}"
  • The file metadata is from your showing curl command.
  • Please retrieve location: https://www.googleapis.com/upload/youtube/v3/videos?uploadType=resumable&part=snippet,status&upload_id=### from the response header. The video data is uploaded using this URL.

2. Upload video data.

As the 2nd step, the video data is uploaded using the retrieved location URL.

Please replace https://www.googleapis.com/upload/youtube/v3/videos?uploadType=resumable&part=snippet,status&upload_id=### with your URL. And, please modify the filename.

curl -X PUT -i 'https://www.googleapis.com/upload/youtube/v3/videos?uploadType=resumable&part=snippet,status&upload_id=###' \
-F "[email protected];type=video/mp4"
  • In this case, the video data is uploaded with a single chunk. If you want to use multiple chunks, please read the official document. Ref

Note:

  • As a simple test, I would like to recommend using a small video file.

  • When I tested the above curl commands, I confirmed that the video file could be uploaded to YouTube. If an error occurs, please confirm your access token, metadata, your video file, and so on, again.

  • If you want to upload the video file using multipart/form-data, you can also the following sample curl command.

    curl -X POST -i 'https://www.googleapis.com/upload/youtube/v3/videos?uploadType=multipart&part=snippet,status' \
      -H 'authorization: Bearer ### Your access token ###' \
      -F "metadata={\"snippet\":{\"categoryId\":\"22\",\"description\":\"Description of uploaded video.\",\"title\":\"Test video upload.\"},\"status\":{\"privacyStatus\":\"private\"}};type=application/json;charset=UTF-8" \
      -F "[email protected];type=video/mp4"
    

Reference:

  • Related