Question

I've setup Omniauth on my Rails 4 application to allow a user to sign in using Github.

This part works fine.

Now, I would like to list the repositories of that user (I request the scope repo when asking the user to authorise my app.

I've done a lot of Googling, but I don't understand how to do that.

Can anyone please tell me how I can use the existing authentication to list the repositories of my user?

Was it helpful?

Solution

Well, OmniAuth is just a flexible authentication library. Its only purpose is to authenticate users. Any custom behaviour you desire has to be implemented either by you or by another gem.

You can achieve what you want by simply using GitHub's well documented API:

# callbacks_controller.rb
require "open-uri"
require "json"

omniauth = env['omniauth.auth']
repositories_json = open(omniauth.extra.raw_info.repos_url,
  "Accept" => "application/vnd.github.v3+json",
  "Authorization" => "token #{omniauth.credentials.token}"
).read
repositories = JSON.parse(repositories_json)

If this is all you need, then by all means, use it. If you have other requirements besides this, then maybe you should take a look at the github gem.

Also, you should only use the scope repo if you want to access your users' private repos. And requesting access to a user's private repos is a bit intrusive, so don't do it lightly.

If you really want to access a user's private repos, then you have to append ?type=all to the end of the repos_url. So you would have to do something like:

open("#{omniauth.extra.raw_info.repos_url}?type=all" ... )
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top