Search code examples
ruby-on-railsrubyoauth-2.0oauthetsy

Etsy oauth and fetching info


I am trying to use etsy API and validate with oauth gem. I have successfully got a successful token by doing this in the first request url:

    scopes = ["email_r", "feedback_r", "listings_r", "transactions_r"]
    oauth_consumer = OAuth::Consumer.new(Rails.application.secrets.etsy_api_key,
                                         Rails.application.secrets.etsy_api_secret,
                                         site: "https://openapi.etsy.com/v2"
    )
    oauth_consumer.options[:request_token_path] = "/oauth/request_token?scope=#{scopes.join('%20')}"
    request_token = oauth_consumer.get_request_token(oauth_callback: new_etsy_authentication_url)
    redirect_to request_token.params[:login_url]

Then the user passes the etsy validation pages and on callback url I have the following:

current_user.update(etsy_auth: {
        oauth_token: params["oauth_token"],
        oauth_verifier: params["oauth_verifier"],
        oauth_created_at: Time.current.to_i
      })

Where I save etsy oauth_token and oauth_verifier successfully.

The problem starts after that. I've tried many things to do a request to user but I always got oauth_token=rejected. Here a sample of what I've done so far:

oauth_consumer = OAuth::Consumer.new(Rails.application.secrets.etsy_api_key,Rails.application.secrets.etsy_api_secret,site: "https://openapi.etsy.com/v2")
access_token = OAuth::AccessToken.new(oauth_consumer, oauth_token: current_user.etsy_auth["oauth_token"], oauth_secret: current_user.etsy_auth["oauth_verifier"])
access_token.request(:get, "/users/__SELF__")

Should I do another request before that, to actually got another temporary oauth_token and oauth_secret?

I've tried doing this: request_token = oauth_consumer.get_request_token and I got a temporary oauth_token and oauth_token_secret as well oauth_consumer_key (which I am not sure how I should use). I got the temporary tokens and tried many combinations without much success, I am always getting oauth_token=rejected . I've still haven't figured out if and where should I use oauth_consumer_key and if oauth_verifier is actually the oauth_secret.

What am I missing? Any help is appreciated.


Solution

  • I finally managed to find what needed to do. After the initial request to Etsy, I have to store oauth_token and oauth_secret. Then Etsy returns as well the oauth_verifier.

    For fetching and doing each request after that, you need to send all three of them to work.