Pradeep K. Pant Blog


Accessing Github API with OAuth example using R

Modern API provided by Google, Twitter, Facebook, Github etc uses OAuth for authentication and authorization. In this example, I am using GitHub API. We get a JSON response which can be used to fetch specific information. In this code I have used my Github account.Code is written R programming languages.

Here are the steps:

  1. Find OAuth settings for Github
  2. Create a application in Github
  3. Add/Modify secret keys
  4. Get OAuth credentials
  5. Finally use API and parse json data to show response

## Load required modules  

# 1. Find OAuth settings for github:  

# 2. To make your own application, register at at  
## Use any URL for the homepage URL  
# ( is fine) and http://localhost:1410 as the callback url. You will need httpuv

## Add Secret keys  
## Secret keys can be get from developer github  
myapp <- oauth_app("github",  
key = "cd28c82639b7cf76fcc",  
secret = "d1c90e32e12baa81dabec79cd1ea7d8edfd6bf53")

# 3. Get OAuth credentials  
github_token <- oauth2.0_token(oauth_endpoints("github"), myapp)  
## Authentication will be done automatically

# 4. Use API  
gtoken <- config(token = github_token)  
req <- GET("", gtoken)  
output <- content(req)  
## Either of the two can be used to fetch the required info, name and date created of repo ProgrammingAssignment3  
out<-list(output[[30]]$name, output[[30]]$created_at)

BROWSE("",authenticate("Access Token","x-oauth-basic","basic"))  
# OR:  
req <- with_config(gtoken, GET(""))  

For updated code please check github



Copyright © 2007-2024 PRADEEP K. PANT

Source Code | RSS