File: oauth1-nounproject.r

package info (click to toggle)
r-cran-httr 1.4.5%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 968 kB
  • sloc: sh: 9; makefile: 2
file content (43 lines) | stat: -rw-r--r-- 1,657 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
library(httr)

# Noun Project has an API secured with OAuth 1.0a One-legged. A client key and a
# secret must be used to sign requests when accessing the API. This is an
# exemple using OAuth 1.0a One legged auth mechanism
# http://oauthbible.com/#oauth-10a-one-legged

# 1. Register an application to get required keys:
# https://thenounproject.com/accounts/login/?next=/developers/apps/
nouns_app <- oauth_app("noun_project",
  key = rstudioapi::askForPassword(),
  secret = rstudioapi::askForPassword()
)

# 2. Each request must be signed using the app key and secret
#    see ?oauth_signature for more information on signature
url <- "http://api.thenounproject.com/icon/15"
signature <- oauth_signature(url, method = "GET", app = nouns_app)
res <- GET(url, oauth_header(signature))
stop_for_status(res)
content(res)

# 3. Create a wrapper function to sign each request more easily
get_nouns_api <- function(endpoint,
                          baseurl = "http://api.thenounproject.com/",
                          app = nouns_app,
                          ...) {
  url <- modify_url(baseurl, path = endpoint)
  info <- oauth_signature(url, app = app)
  header_oauth <- oauth_header(info)
  GET(url, header_oauth, ...)
}
res <- get_nouns_api("collections")
stop_for_status(res)
content(res)

# 4. Signing request requires the METHOD used. If the API has a POST for
# example, you must take it into account.
url <- "http://api.thenounproject.com/notify/publish?test=1"
signature <- oauth_signature(url, method = "POST", app = nouns_app)
res <- POST(url, oauth_header(signature), body = list(icons = 15), encode = "json")
stop_for_status(res)
content(res)