yt
yt copied to clipboard
Oauth "code" response for authorization code is not working to generate token
The code in the params response after going through the Oauth screen on my web app is not working
"Every user who authorizes your app will be redirected to the redirect_uri with an extra code parameter that looks something like 4/Ja60jJ7_Kw0. Just pass the code to the following method to authenticate and initialize the account:"
The code in the params is not in the same format as the code in the readme. I am getting something like this (changed slightly ofc)
https://thesiteIused.com/?code=4%2F0AJ0e-g7gQK-e6WKtMoE01IwApIrf5ScmV7qA1_aRDfpz5_JIGc0rqZxomd1aKk61rn4Mtg&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube.upload%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube
I have tried using the whole code, parts of it, first 13 characters, percentage sign instead of a backslash, and everything in between. Still getting the following response.
A request to YouTube API was sent without a valid authentication: (Yt::Errors::Unauthorized)
Please note that if you're trying to call account.email, you'll need to request the userinfo.email scope.
I've faced the same situation and turn out the code is 1 time use only and is supposed to be used to exchange access token/refresh token from google. So take the code and make an http request to google to get an access token and/or a refresh token to use with the library. Here is some code to get tokens:
response = HTTParty.post(
'https://accounts.google.com/o/oauth2/token',
body: {
client_id: ENV['YOUTUBE_CLIENT_ID'],
client_secret: ENV['YOUTUBE_CLIENT_SECRET'],
grant_type: 'authorization_code',
code: @code,
redirect_uri: @redirect_uri,
},
headers: {
'Content-Type' => 'application/x-www-form-urlencoded'
}
)
raise "Error: #{response.code}" if response.code != 200
JSON.parse(response.body)
@giaunguyen2176, what is @code supposed to represent?
@synthead After signing in, google will redirect back to your registered callback url, which contains the code.