Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# httr 1.3.1.9000

* New demo about OAuth1 One-legged using Noun Project API (@cderv, #548)

* By default, `RETRY()` now terminates on any successful request, regardless
of the value of `terminate_on`. To return to the previous behaviour,
set `terminate_on_success = FALSE` (#522).
Expand Down
1 change: 1 addition & 0 deletions demo/00Index
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
connection-sharing Demonstration of how connection sharing saves time
oauth1-nounproject Using noun project api with OAuth 1.0 one-legged
oauth1-twitter Using twitter api with OAuth 1.0
oauth1-vimeo Using vimeo api with OAuth 1.0
oauth1-yahoo Using yahoo api with OAuth 1.0
Expand Down
44 changes: 44 additions & 0 deletions demo/oauth1-nounproject.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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/
# Add the below environment variable to your session
# (helper function : usethis::edit_r_environ())
nouns_app <- oauth_app("noun_project",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind using the standard tidyverse style here?

key = Sys.getenv("NOUN_API_KEY"),
secret = Sys.getenv("NOUN_API_SECRET"))

# 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)