Search code examples
rubyapioauthmendeley

Obtaining an OAuth token in ruby (for the Mendeley API)


I cannot seem to figure out OAuth authentication for calls to Mendeley's authenticated API (Note that there's an unmaintained? gem for public methods here).

I create an application at their web portal, which returns an application consumer key and consumer secret. I pop these in a YAML file that I can then read in my Ruby script.

  cred = YAML.load_file("/home/cboettig/.mendeley_auth.yaml")
  key = cred[:consumer_key] 
  secret = cred[:consumer_secret]

Attempt Method 1

There seems to be a nice example of authenticated call to the Mendeley API using the Faraday gem for authentication here. Unfortunately, it does not appear to document how to get the token, (or which token it is?) for the actual call at the end. (It points to a github gist on generating the token as a SHA1 hash of the keys, but I cannot find the libary dependencies to make that work, and it seems overly complicated hack given the number of tools already available to handle OAuth....) Here's as far as I get with Method 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

Attempt Method 2

There's an existing SO question on this topic, Mendeley Custom OAuth Strategy, which I also haven't been able to follow successfully. It suggests that this can be handled using Omniauth, and it looks like this has already been implemented in the omniauth-mendeley gem. Unfortunately, I can't make heads or tails of the limited documentation, which suggests something like this:

 ## Method 2 -- 
 require 'omniauth-mendeley'
  use OmniAuth::Builder do
    provider :mendeley, ENV[key], ENV[secret]
  end

Okay, I can get key and secret, but my ruby-fu is very limited and I don't understand the syntax above. How do I execute this and then make a function call to the API (e.g. the call to profile/me shown in Method 1 attempt? Attempting to run this just gets me the error

 undefined method `use' for main:Object

Any hints for getting either of these methods to work?


Solution

  • Thank you for mentioning my article in the shazino blog.

    OAuth is thought mainly for the web.
    It requires a dance between the two websites doing the authentication.
    That's why you won't find any example outside of the web to get the token.

    In my current use, we have an iOS app retrieving the token (we developed the MendeleySDK for that).
    It provides it to our server, so we can make some API calls on behalf of the user.
    That's why I don't need to do the OAuth dance.

    Your Method 2 is the way to go. OmniAuth::Builder is a rack middleware. It's syntax is meant for use in the config.ru file.

    Try creating this file and putting the code you demonstrated in example 2 there.
    Then, in your terminal go to the file's folder and type rackup.
    You need to have the rack gem installed first.
    This should start a web server on port 9292 with the middleware you need.
    For more information on using the OmniAuth middlewares, take a look at their wiki.