Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing the bitly OAuth2 API from R

Tags:

r

bit.ly

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?

like image 756
psychemedia Avatar asked Jan 26 '26 01:01

psychemedia


2 Answers

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"
like image 56
lukeA Avatar answered Jan 27 '26 18:01

lukeA


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)
like image 21
sckott Avatar answered Jan 27 '26 17:01

sckott