Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a demo for one-legged OAuth1 using Noun Project API #548

Merged
merged 8 commits into from Dec 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

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

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

* Facebook OAuth demo updated to use device flow (#510). This allows you to
continue using the FB api from R under their new security policy.

Expand Down
1 change: 1 addition & 0 deletions demo/00Index
@@ -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-yahoo Using yahoo api with OAuth 1.0
oauth1-withings Using withings api with OAuth 1.0
Expand Down
43 changes: 43 additions & 0 deletions demo/oauth1-nounproject.r
@@ -0,0 +1,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",
Copy link
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 = 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)
4 changes: 2 additions & 2 deletions demo/oauth2-facebook.r
Expand Up @@ -49,7 +49,7 @@ token <- content(resp)$access_token
# 3. Use API
req <- GET("https://graph.facebook.com/v2.3/me", query = list(
fields = "name,picture",
access_token = token)
)
access_token = token
))
stop_for_status(req)
str(content(req))