Can't correctly make refresh_token to get new token after it expires in youtube api v3

Viewed 56

I want to do some stuff with the youtube api. For instance, to get my playlists, I use this url:

playlist.list

https://youtube.googleapis.com/youtube/v3/playlists?part=($type)&mine=true&key=($api_key)

Using a get method with this header:

Authorization: Bearer ($token)
Accept: application/json'

Where part can be id, snippet and other I don't remember now. I got the api key in the developers console. Then I created Oauth2 credentials. And the token is obtained from (directly in a browser):

https://accounts.google.com/o/oauth2/auth?client_id=($client)&redirect_uri=($uri)&scope=https://www.googleapis.com/auth/youtube&response_type=token&approval_prompt=force

Where the redirect_uri is established when creating the oauth2 credentials and client_id is part of those credentials.

And this works, I can use the youtube api to do the things I need.

But, the token expires after 1 hour aprox. So I have to get the token again every time:(

Searching around, I found out that there is a refresh_token that you can use to ask for a new token.

As far as I could get from the web, this refresh_token is obtained via this url:

echo $"https://accounts.google.com/o/oauth2/auth?client_id=($client)&redirect_uri=($uri)&scope=https://www.googleapis.com/auth/youtube&response_type=code&access_type=offline&prompt=consent"

Then, to request a new token (or to refresh it), I need to post on this url:

https://accounts.google.com/o/oauth2/auth?client_id=($client)&redirect_uri=($uri)&scope=https://www.googleapis.com/auth/youtube&response_type=code&access_type=offline&prompt=consent

With content type: application/json.

I've tried several bodies (client_secret is part of the oauth2 credentials):

"client_id": ($client),
"client_secret": ($secret),
"refresh_token": ($refresh_token),
"grant_type": "authorization_code",
"access_type": "offline",
"prompt": "consent",
"scope": "https://www.googleapis.com/auth/youtube"

That one gives me this error:

╭───────────────────┬──────────────────────────────────╮
│ error             │ invalid_request                  │
│ error_description │ Missing required parameter: code │
╰───────────────────┴──────────────────────────────────╯

Using grant_type = code, gives me:

╭───────────────────┬──────────────────────────╮
│ error             │ unsupported_grant_type   │
│ error_description │ Invalid grant_type: code │
╰───────────────────┴──────────────────────────╯

So, any idea of what I'm doing wrong?

1 Answers

The corect call to use the refresh token to get a new access token is

HTTP POST https://accounts.google.com/o/oauth2/token
client_id={ClientId}.apps.googleusercontent.com&client_secret={ClientSecret}&refresh_token=1/ffYmfI0sjR54Ft9oupubLzrJhD1hZS5tWQcyAvNECCA&grant_type=refresh_token

This is a http Post call, and the body should be sent as html query string format. NOT JSON.

Your issue is with the grant_type it needs to be refresh_token

useful links

Related