I want to do some stuff with the youtube api. For instance, to get my playlists, I use this url:
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?