Question

I want to use Yahoo Fantasy sport API in my web application, For that I am using OAuth for Yahoo login. I have consumer key and secret key and i passed the keys successfully, When I run the following code. It redirects to Yahoo login, It asks permission for accessing the user's credentials. If i give AGREE the page Redirects to https://api.login.yahoo.com/oauth/v2/request_auth and It shows the Verifying code. If i press the close button in verification code page, it's not callback to my URL.

 @ts=Time.now.to_i
    @callback_url = "http://localhost:3000/callback"
    @nonce = SecureRandom.hex()

       consumer = OAuth::Consumer.new("my consumerkey","secret key",
          { :site => 'https://api.login.yahoo.com', 
          :http_method => :post, 
          :scheme => :header,
          :oauth_nonce => @nonce,
          :request_token_path => '/oauth/v2/get_request_token', 
          :authorize_path => '/oauth/v2/request_auth', 
          :access_token_path => '/oauth/v2/get_token', 
          :oauth_callback => "http://localhost:3000/callback",
          :oauth_timestamp => Time.now.to_i,
          :oauth_signature_method => "HMAC-SHA-1",
          :oauth_version => "1.0",
          :oauth_callback_confirmed => true,
         })

    request_token = consumer.get_request_token
    session[:request_token]=request_token
    redirect_to request_token.authorize_url
    access_token=request_token.get_access_token
    access = ActiveSupport::JSON.decode(access_token.to_json)
     if !(access.present?)
      @response = "Response failed"  
    else
      @response = access  
    end

Can you please tell me What changes to be made to get the callback for to get access_token.

Was it helpful?

Solution

I think you got confused while getting callback. change your code as follows, You will surely get access token to make Yahoo API call.

        @@access_token = nil
        @@request_token = nil
     def get_request_token
        @@consumer = OAuth::Consumer.new('consumer key',
                    'secret key',
                    {
                      :site                 => 'https://api.login.yahoo.com', 
                      :scheme               => :query_string, 
                      :http_method          => :get, 
                      :request_token_path   => '/oauth/v2/get_request_token', 
                      :access_token_path    => '/oauth/v2/get_token', 
                      :authorize_path       => '/oauth/v2/request_auth'
                })
                @@request_token = @@consumer.get_request_token( { :oauth_callback => 'http://localhost:3000/callback' } )
                session[:request_token]=@@request_token
                redirect_to @@request_token.authorize_url
                #redirect_to @@request_token.authorize_url( { :oauth_callback => 'http://localhost:3000/success' } )

     end


    def callback
    request_token = ActiveSupport::JSON.decode(@@request_token.to_json)

        if !(request_token.present?)
          $request_token_value = "Response failed"  
        else
          $request_token_value = request_token  
        end
        # access_token = @@request_token.get_access_token({:oauth_verifier=>params[:oauth_verifier],:oauth_token=>params[:oauth_token]}) 
        @@access_token =    @@request_token.get_access_token(:oauth_verifier=>params[:oauth_verifier]) 
        access_json = ActiveSupport::JSON.decode(@@access_token.to_json)
        puts "****************************"  
        puts $access_json
        puts "****************************"   
    end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top