Henry Navarro
Henry Navarro

Reputation: 953

Error trying to get token in YouTube Analytics API in R

I am trying to connect the YouTube Analytics API following the next documentation:

https://developers.google.com/youtube/reporting/guides/authorization/server-side-web-apps

According to the documentation, a "code" parameter must be obtained. With this step I have no problems and what I get is:

Encoded:

4%2FAADopgcKaRx1vdX0mKP9Te-gwLwA54sgV14x6Qug28p944l3MasxIu9GLE6I4xFVMZrQbId_zXLQlOBtvV1ffsI#

No encoded:

4/AADopgcKaRx1vdX0mKP9Te-gwLwA54sgV14x6Qug28p944l3MasxIu9GLE6I4xFVMZrQbId_zXLQlOBtvV1ffsI

So, the next step according the documentation is "Exchange authorization code for refresh and access tokens". Basically obtain a token, and what they do is:

enter image description here

I do the same in R running the next code:

Code in R to make a POST

library(httr)

login<-list(
  code="4%2FAADopgcKaRx1vdX0mKP9Te-gwLwA54sgV14x6Qug28p944l3MasxIu9GLE6I4xFVMZrQbId_zXLQlOBtvV1ffsI#",
  client_id="118642749887-1vvc0ckk3m601dv4rk6hefbmpqdjkvob.apps.googleusercontent.com",
  client_secret="2IFSNl88dd5peFlwVupXLCM6",
  redirect_uri="http://localhost:1410/code",
  grant_type="authorization_code"
)


url<-"https://www.googleapis.com/oauth2/v4/token?"

req <- POST(url,
            add_headers(
                        "Content-Type" = "application/x-www-form-urlencoded"
                       ),
            body = login,
            verbose()
)

Response

>req
Response [https://www.googleapis.com/oauth2/v4/token?]
  Date: 2018-03-07 13:40
  Status: 400
  Content-Type: application/json; charset=UTF-8
  Size: 85 B
{
 "error": "unsupported_grant_type",
 "error_description": "Invalid grant_type: "
}

It means that I am providing a wrong parameter grant_type, but I am doing what documentation says.

Someone can help me to solve this problem?

Thanks in advance.

Upvotes: 1

Views: 393

Answers (1)

Steven M. Mortimer
Steven M. Mortimer

Reputation: 1706

The oauth dance of exchanging authorization code for refresh and access tokens is completely managed by httr, so you don't need to explicitly do any of that work. Here is an example using my own key and secret to pull down the top 5 search results on Youtube for the query "surfing".

key <- "526767977974-i8pn4vvaga2utiqmeblfnpakflgq964n.apps.googleusercontent.com"
secret <- "tNJixXCExE30f_ARBzb6e4hC"
myapp <- oauth_app("google", key, secret)
token <- oauth2.0_token(oauth_endpoints("google"),
                        myapp,
                        scope=c("https://www.googleapis.com/auth/youtube.readonly", 
                                "https://www.googleapis.com/auth/yt-analytics.readonly"))

url = "https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=5&q=surfing"
req <- GET(url, token)
content(req)

That uses the Youtube Data API. If you want to use the Analytics API it typically involves running reports on channels that you own. Here is an example call where I retrieve my own channel report

query_parms <- paste0("ids=channel==MINE",
                      "&dimensions=video",
                      "&startDate=2018-02-01",
                      "&endDate=2018-02-28",
                      "&metrics=estimatedMinutesWatched,views,likes,subscribersGained",
                      "&maxResults=10",
                      "&sort=-estimatedMinutesWatched")

url <- paste0("https://youtubeanalytics.googleapis.com/v2/reports?", query_parms)
req <- GET(url, token)
parsed_req <- content(req)

Upvotes: 1

Related Questions