Search code examples
rfile-uploadgoogle-apiyoutube-api

Error when uploading a thumbnail via youtube API with R


I want to use R to update thumbnails of my youtube videos. I'm following the google documentation, but I get the following error:

"The request does not include the image content."

The curent status of my code is the following:

library(httr)
library(tuber)

video.id <- [ID]
thumbnail.path <- [path]
youtube.client.id <- [ID]
youtube.client.secret <- [secret]

yt_oauth(
    app_id = youtube.client.id,
    app_secret = youtube.client.secret,
    scope = 'partner'
)

req <- POST(
    "https://www.googleapis.com",
    path = paste0(
        "youtube/v3/thumbnails/set",
        "?videoId=", video.id
    ),
    body = httr::upload_file(thumbnail.path),
    config(token = getOption("google_token"))
)

I have similar queries for other API call (using PUT or GET), and they work. I think the issue is around the upload_file, but cannot find what should be the solution.

How can I fix that? Thanks in advance


Solution

  • If anyone has the same issue, I managed to fix it. I've tried a lot of things, but I think what really fixed it was to change the url from "youtube/v3/thumbnails/set" to "upload/youtube/v3/thumbnails/set".

    Here is my final code:

    oauth <- paste0('"Authorization: Bearer ', access.token, '"')
    full.url <- paste0(
        'https://www.googleapis.com/upload/youtube/v3/thumbnails/set',
        '?videoId=', video.id,
        '&key=', youtube.api.key,
        '&uploadType=media'
    )
    
    
    final.args <- c(
        '--request', 'POST',
        '-v', paste0('"', full.url, '"'),
        '--data-binary', paste0('@', cover.path, ''),
        '-H', oauth,
        '-H', '"Content-Type: image/png"'
    )
    
    
    system2(
        command = 'curl',
        args = final.args,
        stdout = file.res, stderr = file.err
    )