kylejginavan / youtube_it

An object-oriented Ruby wrapper for the YouTube GData API
http://groups.google.com/group/ruby-youtube-library
595 stars 223 forks source link

OAuth2::Error Token Invalid after refresh_access_token! method call #250

Closed kterusaki closed 9 years ago

kterusaki commented 9 years ago

I'm getting a 401 Error even after I've called the refresh_access_token! method.

I'm getting a 401 Error even after I've called the refresh_access_token! method.

class MyClass
    attr_accessor :client

    def initialize(access_token, refresh_token)
        @client = YouTubeIt::OAuth2Client.new(client_access_token:access_token, 
                client_refresh_token: refresh_token,
                client_id: ENV['GOOGLE_CLIENT_ID'],
                client_secret: ENV['GOOGLE_CLIENT_SECRET'],
                dev_key: ENV['GOOGLE_DEV_KEY'])

        # If YouTubeIt::AuthenticationError, refresh token
        begin
            @client.activity
        rescue OAuth2::Error => error
            if error.response.response.env.status == 401
                @client.refresh_access_token!
            end
        end
    end
end

My initial thought, was that this method would update the client with the new access token. However, since this didn't work I tried reinitializing the client with the new access token (OAuth2::AccessToken object) returned from the refresh_access_token! method like so:

new_access_token = @client.refresh_access_token!

@client = YoutubeIt::Oauth2Client.new(client_access_token: new_access_token.token, ....)

Any clarification on how this method works would be much appreciated.