如何将oauth_token &使用 OAuth gem 将 oauth_verifier 转换为访问令牌
我正在尝试使用 OAuth gem 在我的 Ruby on Rails 应用程序中验证 Evernote。我正在使用一个教程来验证 Twitter - http:// blog.brijeshshah.com/integrate-twitter-oauth-in-your-rails-application/ 因为我找不到 Evernote。
到目前为止,我已经让用户授权我的应用程序,并且现在拥有临时凭据:
customer = OAuth::Consumer.new("xxx", "xxx",{
:site=>"https://sandbox.evernote.com/",
:request_token_path => "/oauth",
:access_token_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
现在我拥有 oauth_token 和 oauth_verifier,并且需要将它们转换为访问令牌。 Twitter 教程的这一部分似乎是 Twitter 特有的,所以我现在知道如何在 Evernote 的情况下进行处理。有人可以帮我吗?
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_token_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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Evernote 的示例代码现在包含一个使用 OAuth gem 的 Ruby OAuth 示例。您可以从 http://www.evernote.com/about/developer/api 下载示例代码/。在这种情况下,下一步是将临时凭据交换为令牌凭据:
oauth_verifier 作为回调 URL 的一部分传递到您的应用程序。
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:
The oauth_verifier is passed to your application as part of the callback URL.
嘿,伙计,我也开始了这样的道路,我将 Oauth 集成到我的其他应用程序之一中。
您应该查看github 上的oauth-plugin,因为它会为您处理所有业务。
它应该在很大程度上帮助您,此外,如果有一个不“流行”的“奇怪”oauth 提供程序,它允许您将其添加到配置文件中。这就是我对我所做的。
一项建议是覆盖 OauthConsumersController 中的所有方法并根据需要“调整”它们。我知道我必须这样做,而且,更容易看到他用他的插件做了什么并从那里调整它。天啊,也许他甚至有一条路径,您可以在他的代码中遵循您的确切问题,并且您不需要他的所有插件(因为他也使用 oauth gem)。
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).
除了 Evernote 中的有用示例之外,您可能还希望在单元测试中自动执行“重定向用户、让他们授予访问权限、重定向回来”循环。我发现这有点具有挑战性,因此在这里发布了所有这样做的代码:
单元测试中的 Evernote OAuth
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
在通过 Seth's Answer 获取 accesstoken 时,您需要在生成授权 url 时创建的请求令牌。您需要将其存储在会话对象中。如果直接存储,则在检索存储的请求令牌时会抛出错误。所以你需要将其存储在cache_store中。
为了在你的rails应用程序中处理evernote api的请求令牌和访问令牌,你可以按照以下步骤操作:
你需要设置一个会话cache_store来保存来自Evernote的OAuth令牌。将以下内容添加到 config/initializers/session_store.rb 的末尾:
这样,您可以将对象存储在会话中,并且可以在需要时检索它。
想要了解更多详细信息,您可以查看以下教程。 - https://codepen.io/asommer70/post/export-evernote-笔记与导轨
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:
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