authorization code for Github API used in R

10,141

Solution 1

The authorisation code is the code that github supplies after a correct OAuth 2.0 'dance' (to use Hadley Wickham's term). The easiest way of doing this is to use httpuv (install.packages("httpuv")). With that installed, a local webserver is setup on port 1410 and provided you've setup your github application appropriately (with a redirect to http://localhost:1410).

If you don't have httpuv installed, then httr's OAuth 2.0 function defaults to out of band authorisation. This asks GitHub to redirect to urn:ietf:wg:oauth:2.0:oob&response_type=cod which should display the authorisation code within the browser so that it can be copied and pasted. However, you've almost certainly got something different set as your redirect URL and so github complains that there is a redirect URI mismatch. I'm not sure whether github can be configured to allow the oob redirect (but I've just tried and it doesn't seem to).

The only reasons not to use httpuv are if you are using R on a machine that won't let you set up a server on port 1410 or if you are using R on a remote machine via RStudio Server or an SSH session. In the latter case, the webserver will be setup on the remote machine, but your browser will be trying to connect to port 1410 on your local machine. You could potentially get around this by doing SSH port forwarding from port 1410 on your local machine to port 1410 on the remote machine.

Note also that the demo code at https://github.com/hadley/httr/blob/master/demo/oauth2-github.r unlike the current CRAN version of the oauth2-github demo includes the secret for Hadley's application so you can run the demo as is without setting up your own application first.

Solution 2

Here's what worked for me:

Install package HTTPUV from https://github.com/rstudio/httpuv

And maybe set your \\R\library permission for the current user for running devtools::install_github("rstudio/httpuv")`

Share:
10,141

Related videos on Youtube

Roshan Vishva
Author by

Roshan Vishva

Updated on September 16, 2022

Comments

  • Roshan Vishva
    Roshan Vishva over 1 year

    I am trying Access the API to get information on http://github.com. I created in application in github (in developer application) for this URL and try to access thru R using httr libraries. The following is the code

    library(httr)
    oauth_endpoints("github")
    myapp <- oauth_app("github",key = "#####################",secret = "########################" )
    

    (key was replaced with client id and secret was replaced with secred id)

    github_token <- oauth2.0_token(oauth_endpoints("github"), myapp)
    

    This prompted me the following

    Use a local file to cache OAuth access credentials between R sessions? 1: Yes 2: No

    I selected 2 (as i tried option 1 earlier) then the following are displayed

    httpuv not installed, defaulting to out-of-band authentication
    Please point your browser to the following url: 
    
      https://github.com/login/oauth/authorize?client_id=72939e1b6d499f4f1894&scope=&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&response_type=code
    

    Enter authorization code Can any one tell me what the authorization code is?

  • hadley
    hadley almost 9 years
    For github specifically, using a personal access token and regular http auth is much easier.
  • Roshan Vishva
    Roshan Vishva almost 9 years
    Hi Nick , thx for your help i am new to web , web API etc.. however i assume issue was with setting up local webserver on 1410. I did not know i have to set it up. as per your comment i installed httpuv package. I assume it setup webserver on my local. However i tried to google on how to check webserver has been setup but could find answere . However i was able to access the API for github.com Thx