How to specify user agent when using github API?

I want to obtain the list of files on a Github repo. I followed this answer but I noticed that sometimes I have a HTTP 403 error. For example, if I run the following code:


for (i in 1:10) {
  req <- GET("")

> Error: Forbidden (HTTP 403).

(Note that I don't actually want to run this GET request 10 times, it's just the easiest way I found to simulate my problem. )

Searching a bit online, I found this answer that explains that the Github API requires the GitHub username, or the name of the application, for the User-Agent header value of the request.

But adding user_agent("etiennebacher") in GET() doesn't change anything. How should I specify the user agent in this case?

Also asked on StackOverflow

Answer on StackOverflow:

As commented by @MrFlick, the HTTP 403 here means that the API rate limit was exceeded. A way to solve this is to authenticate when making the request, because the rate limit goes from 60 to 5,000 requests per hour. It is easier to do so by using the package gh (and its eponymous function). This can be done locally by specifying .token = <PAT> (where <PAT> is your GitHub Personal Access Token) in gh().

To obtain the list of files in a particular repo, you can save the output of gh() in a JSON file, and then read it with jsonlite::fromJSON(). It is then trivial to get the list of files.

Bottom line, this works:


tmp_file <- tempfile(fileext = ".json")

gh::gh("", .destfile = tmp_file, .token = <PAT>)


Bonus: how to authenticate in Github Actions

I didn't say it in the original post, but this GET request was supposed to be made in a Github action. Since you can't manually provide the PAT as a token, you need to define the token in the .yaml file, and then pass its value in the .R file.



tmp_file <- tempfile(fileext = ".json")

gh::gh("", .destfile = tmp_file, .token = github_token)


GitHub action:

    branches: master

    runs-on: macOS-latest
      - uses: actions/checkout@v2
      - uses: r-lib/actions/setup-r@master
      - run: |
          github_token <- "${{ secrets.GITHUB_TOKEN }}"
          install.packages(c("gh", "jsonlite"))
        shell: Rscript {0}

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.