开发者

How to turn oauth_token & oauth_verifier into Access Token with OAuth gem

I am trying to use to OAuth gem to authenticate Evernote in my Ruby on Rails app. I'm using a tutorial for authenticating Twitter - http://blog.brijeshshah.com/integrate-twitter-oauth-in-your-rails-application/ because I couldn't find an Evernote one.

So far I have gotten the user to authorize my application and now have the temporary credentials:

customer = OAuth::Consumer.new("xxx", "xxx",{
  :site=>"https://sandbox.evernote.com/",
  :request_token_path => "/oauth",
  :access_tok开发者_C百科en_path => "/oauth",
  :authorize_path => "/OAuth.action"})
@request_token = customer.get_request_token(:oauth_callback => "http://localhost:3000/create_evernote_step_2")

session[:request_token] = @request_token.token
session[:request_token_secret] = @request_token.secret       

redirect_to @request_token.authorize_url

So now I have the oauth_token and oauth_verifier, and need to turn these into the access token. This part of the Twitter tutorial seems specific to Twitter, so I'm now sure how to process in the case of Evernote. Can anyone help me out?


Evernote's sample code now contains a Ruby OAuth example that uses the OAuth gem. You can download the sample code from http://www.evernote.com/about/developer/api/. In this case, the next step is to exchange the temporary credentials for token credentials:

access_token = @request_token.get_access_token(:oauth_verifier => oauth_verifier)

The oauth_verifier is passed to your application as part of the callback URL.


Hey man, I started down a path like this too, where I was integrating Oauth into one of my other apps.

You should check out oauth-plugin on github as it handles all that business for you.

It should help you most of the way, plus if there is a 'weird' oauth provider that isn't "popular" it allows you to add it into a config file. That's what i did with mine.

One suggestion is to overwrite all of the methods in OauthConsumersController and 'tweak' them as needed. i know i had to do it and well, it was easier seeing what he did with his plugin and tweaking it from there. Hell, maybe he even has a path you can follow in his code for your exact problem and you won't need all of his plugin (as he uses oauth gem too).


In addition to the helpful example from Evernote, you might also want to automate the "redirect user, let them grant access, get redirected back" cycle in a unit test. I found that a bit challenging, so have posted all the code for doing so here:

Evernote OAuth in a unit test


While getting the accesstoken by Seth's Answer, you need the request token which you have created while generating the authorization url. This you need to store in the session object. If you store directly, it will throw error while retrieving the stored request token. So you need to store that in the cache_store.

In order to deal with request token and access token for evernote api in your rails app, you can follow the below steps:

you need to setup a session cache_store to save your OAuth tokens from Evernote. Add the following to the end of config/initializers/session_store.rb:

Rails.application.config.session_store :cache_store, key: ‘_rails-evernote_session’

In this way, your can store the objects in session and you can retrieve it when required.

For more detailed information, you can check the following tutorial. - https://codepen.io/asommer70/post/export-evernote-notes-with-rails

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜