I'd like to be able to access the bitly OAuth2 api from R and was wondering whether there were any example implementations, or better still, R libraries/wrappers for the bitly API around?
The twitteR library span off an R OAuth library, ROAuth, but this presumably doesn't support OAuth2? Or will OAuth2 accept the OAuth1 overtures?
Here is a way using httr - it's in the veins of the packages' examples on GitHub:
require(jsonlite)
require(httr)
# 1. Find OAuth settings for bit.ly:
# http://dev.bitly.com/authentication.html
bitly <- oauth_endpoint(
authorize = "https://bitly.com/oauth/authorize",
access = "https://api-ssl.bitly.com/oauth/access_token")
# 2. Register an application at http://dev.bitly.com/my_apps.html
# Insert your values below - if secret is omitted, it will look it up in
# the BITLY_CONSUMER_SECRET environmental variable.
myapp <- oauth_app("bitly",
key = ".............................", # Client ID
secret = "............................") # Client Secret
bitly_token <- oauth2.0_token(bitly, myapp, cache = FALSE)
# 4. Use API
req <- GET("https://api-ssl.bit.ly/v3/user/info", query = list(access_token = bitly_token$credentials$access_token))
stop_for_status(req)
content(req)$data$profile_url
# [1] "http://bitly.com/u/lukeanker"
I have written three fxns so far within a package that hits other APIs here: https://github.com/ropensci/raltmet/tree/master/R
The three fxns gets clickc based on users, expand URLs and shorten URLs
Install via:
install.packages("devtools")
require(devtools)
install_github("raltmet", "ropensci")
require(raltmet)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With