【发布时间】:2026-02-13 18:05:01
【问题描述】:
我似乎无法弄清楚对 Mendeley 的 authenticated API 的调用的 OAuth 身份验证(请注意,公共方法 here 有一个未维护的?gem)。
我在他们的门户网站上创建了一个应用程序,该应用程序返回一个应用程序使用者密钥和使用者密码。我将它们弹出到一个 YAML 文件中,然后我可以在我的 Ruby 脚本中读取该文件。
cred = YAML.load_file("/home/cboettig/.mendeley_auth.yaml")
key = cred[:consumer_key]
secret = cred[:consumer_secret]
尝试方法一
似乎有一个nice example of authenticated call to the Mendeley API using the Faraday gem for authentication here。不幸的是,它似乎没有记录如何在最后的实际调用中获取令牌(或者它是哪个令牌?)。 (它指向一个 github gist,将令牌生成为密钥的 SHA1 哈希,但我找不到使该工作正常的库依赖项,鉴于已经可用于处理 OAuth 的工具数量,它似乎过于复杂...... .) 这是我对方法 1 的了解:
## Method 1 -- how do I get the token?
key = cred[:key]
secret = cred[:secret]
token = ???? # How to get the token?
mendeley = Mendeley.new(token, secret) # Needs a token
profile = mendeley.get('/oapi/profiles/info/me') # example API call
尝试方法二
关于这个主题有一个现有的 SO 问题,Mendeley Custom OAuth Strategy,我也未能成功关注。它表明这可以使用Omniauth 来处理,并且看起来这已经在omniauth-mendeley gem 中实现了。不幸的是,我无法确定the limited documentation 的正面或反面,这暗示了这样的事情:
## Method 2 --
require 'omniauth-mendeley'
use OmniAuth::Builder do
provider :mendeley, ENV[key], ENV[secret]
end
好的,我可以得到密钥和秘密,但是我的 ruby-fu 非常有限,我看不懂上面的语法。如何执行此操作,然后对 API 进行函数调用(例如方法 1 尝试中显示的对 profile/me 的调用?尝试运行它只会让我出错
undefined method `use' for main:Object
有什么提示可以让这两种方法发挥作用吗?
【问题讨论】:
-
我收集到
use语法与运行基于 Rails 的 Web 应用程序有关,而我正在寻找一个纯 Ruby(例如我可以在 irb 本地运行的东西)解决方案。 -
Michal 的
authorise函数完全符合我的要求:github.com/michalboo/mendeley-oapi/blob/master/authorise.rb