Getting httr to work with the Noun Project API...

apis
r-user-group
authentication

#1

Thanks in advance and sorry if I'm asking a very silly question.
I am trying and failing to make httr work with the Noun Project API).
The site suggests a very simple access based just on the key and the secret.

My R code is as follows:

 token <- oauth1.0_token(
      endpoint = oauth_endpoint(
          request = "http://api.thenounproject.com/",
          authorize = "http://api.thenounproject.com/icon/15",
          access = "http://api.thenounproject.com/icon/15"),
      app = oauth_app(
          appname = "nounprojectR",
          key = key,
          secret = secret))

It opens a browser window but gives a
"Bad Request
Missing OAuth parameters"
response.

This seems like it should be a really simple problem - like a gap between how httr and the API are handling the key and secret - perhaps API is not recognising the text of the key for some reason.

I've managed to make it work with the python function OAuth1 from requests_oauthlib.
It seems like I am making a simple one legged request that returns a JSON object.

However, I would really like to make this work natively in R if at all possible.
It's part of a Hacktoberfest 2018 project for the CaRdiff User Group but we are failing at the first hurdle :frowning:
The Github repo is here: https://github.com/CaRdiffR/nounprojectR

I have raised an issue as part of the httr package and I've been advised to ask the friendly people here for help.

I imagine I'm probably doing something really stupid but I'm really at my wits end trying to make this work and would love a bit of help if anybody recognises the problem.

Thanks a lot, Paul


#2

I'm still working on this problem and I understand APIs more than I did...
It seems that I am not trying to obtain a token in any way but rather to create the GET query using based on the icon I want to create with my key and secret.

Can anyone advise about the correct httr function that will do this, please?
If httr can't do it, could curl?
Thanks in advance for any help.
Best wishes,
Paul


#3

Hi,

here is how to do it but it is advanced use because completely undocumented example I think.

For the Oauth1 API of NOUNS you need to get the signature using you key and secret. There is no exchange of token so no oauth dance. The function to get the signature is oauth_signature, then you need to pass this information in the header of you GET request. oauth_header is here to help. You need to do this for each requested url. To show how it works, I made a custom function that get the oauth signature before doing the GET request.

nouns_app <- httr::oauth_app("nouns_api", Sys.getenv("NOUNS_API_KEY"), Sys.getenv("NOUNS_API_SECRET"))

get_nouns_api <- function(endpoint, baseurl = "http://api.thenounproject.com/", app = nouns_app) {
  url <- httr::modify_url(baseurl, path = endpoint)
  info <- httr::oauth_signature(url, app = app)
  header_oauth <- httr::oauth_header(info)
  httr::GET(url, header_oauth)
}

res <- get_nouns_api("icon/15")
httr::status_code(res)
#> [1] 200
str(httr::content(res), 1)
#> List of 1
#>  $ icon:List of 23
res <- get_nouns_api("collections")
httr::status_code(res)
#> [1] 200
str(httr::content(res), 1)
#> List of 1
#>  $ collections:List of 50

Created on 2018-10-18 by the reprex package (v0.2.1)

With this, you'll be able to complete you nounsapi :package: . I am willing to help by doing a PR there if you want.

I also answered on Github