hrbrmstr / 2017-year-in-review

Year in Review with R Rmd Template
https://rud.is/rpubs/2017-year-in-review/
34 stars 10 forks source link

GitHub API error #2

Open jrosen48 opened 6 years ago

jrosen48 commented 6 years ago

When I run the following code in the github_repos chunk:

s_ghn <- safely(gh_next) # API calls are fraught with peril, so make this one a bit safer

my_repos_file <- file.path(rt, "data", "my_repos.rds")

if (!file.exists(my_repos_file)) {
  curr_repo <- gh("/user/repos", username = "public")
  my_repos <- list()
  i <- 1
  my_repos[i] <- list(curr_repo)
  spin <-  TRUE
  while(spin) {
    curr_repo <- s_ghn(curr_repo)
    if (is.null(curr_repo$result)) break
    i <- i + 1
    curr_repo <- curr_repo$result
    my_repos[i] <- list(curr_repo)
  }
  my_repos <- unlist(my_repos, recursive=FALSE)
  write_rds(my_repos, my_repos_file)
} else {
  my_repos <- read_rds(my_repos_file)
}

The following error is returned:

Error in gh("/user/repos", username = "public") : 
  GitHub API error (401): 401 Unauthorized
  Requires authentication

I checked on a few ways to authenticate but could not find out how to authenticate with GitHub. Is there a separate step (through GitHub - or locally) that needs to be done before running this?

Thank you for sharing this awesome resource!

hrbrmstr commented 6 years ago

Thx for taking the Rmd for a spin (or a knit :-) and also taking time to post an issue/questions!

Aye. And I likely shld have noted it far better than I did. Rather than duplicate the (albeit not really easy to find gh docs), take a look here for how to get the token in your environment. https://github.com/r-lib/gh/blob/eecde016754be7769f6f401deb39cc90f3ac7cdd/R/gh_whoami.R

hrbrmstr commented 6 years ago

I've now noted this in the README and in the first Rmd comment snippet. If you do try it again and it works, I'll close the issue.

jrosen48 commented 6 years ago

Thank you! If I understand correctly, I need to get an API key in my environment by running the gh_whoami() function. When I did this the following error was returned:

> gh::gh_whoami()
No personal access token (PAT) available.
Obtain a PAT from here:
https://github.com/settings/tokens
For more on what to do with the PAT, see ?gh_whoami.

I navigated to https://github.com/settings/tokens and generated a token - and then used it in as follows:

gh::gh_whoami([TOKEN])

(I get the sense I'm completely missing the point of what this function does, which is to automatically generate a token.)

When I run the above chunk again, the following error is returned:

Error in map(.x, .f, ...) : object 'public_repos' not found

Thank you again for the help. I can hop over to gh, but if I might ask for one more tip on how I can get the token in my environment - now I really want for this to work.

Thanks!