Error 400: invalid request with req_oauth_auth_code for Google Youtube API
Using req_oauth_auth_code with Google Youtube API, the browser window for user to authorize opens with
Error 400: invalid_request OAuth 2 parameters can only have a single value: scope
I do not think scope is wrong because Code works , with appropriate changes, in postman and httr.
Under httr2, I am to proceed without error by using these 2 functions instead of req_oauth_auth_code: httr2::oauth_flow_auth_code(client, httr::req_auth_bearer_token(token[[2]]) ## 2 is ascii part
Here is httr2::req_oauth_auth_code that produces the above 400 error: `
gather needed params for youtube api, playlists
{ library(httr2) request_url <- "https://www.googleapis.com/youtube/v3/playlists" auth_url = "https://accounts.google.com/o/oauth2/v2/auth" token_url="https://oauth2.googleapis.com/token"
Retrieve from ~/.Renviron
API_KEY= Sys.getenv("API_KEY") client_id = Sys.getenv("OAUTH2_ID") client_secret = Sys.getenv("OAUTH2_SECRET")
Google wants multiple scopes separated by white space.
scope = paste0("https://www.googleapis.com/auth/youtube.force-ssl ", "https://www.googleapis.com/auth/youtube") }
Begin assembly for httr2::req_oauth_auth_code()
But do not run, yet
{ auth_params=list(scope=scope, response_type="code") fields=paste(sep=",", "nextPageToken", "items(snippet(title,description,publishedAt))") token_params = list( scope=scope, grant_type="authorization_code")
Construct client
client = oauth_client(id= client_id, token_url = token_url, secret = client_secret, key = API_KEY, auth = "body", #auth = "header", name = "youtube_ALL_PLAYLISTS")
Build request object, let httr2:: do it's magic (including server setup) to return resp object.
req = request(request_url) req <- req |> req_error(is_error = ~ FALSE) |> # do not turn errors into R: req_url_query(part="snippet", mine="true", fields=fields)
X is final request object
X <- httr2::req_oauth_auth_code( req, client = client, auth_url = auth_url, cache_disk = T, scope = scope, pkce = T, auth_params = auth_params , token_params = token_params )
}
Final check, proceed to perform. After user authorizes, run the request.
{ resp <- X %>% req_perform() }
E R R O R ....
Error 400: invalid_request
OAuth 2 parameters can only have a single value: scope
`