This repository was archived by the owner on Nov 1, 2017. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork1.1k
Sync changes from upstream repository#679
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
12 changes: 12 additions & 0 deletionscontent/changes/2015-01-08-discovering-resources-for-a-user.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--- | ||
kind: change | ||
title: "New guide: Discovering resources for a user" | ||
created_at: 2015-01-08 | ||
author_name: jasonrudolph | ||
--- | ||
Is your application taking advantage of the recommended workflow for discovering a user's repositories and organizations? With the [recent improvements to the API](/changes/2014-12-08-organization-permissions-api-preview/), the process is simpler than ever. In our newest guide, we show you how to [reliably identify the resources that your app can access for a given user](/guides/discovering-resources-for-a-user/). | ||
If you have any questions or feedback, we'd love to [hear from you][contact]. | ||
[contact]: https://github.com/contact?form%5Bsubject%5D=API+v3:+Discovering+resources+for+a+user |
106 changes: 106 additions & 0 deletionscontent/guides/discovering-resources-for-a-user.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
--- | ||
title: Discovering resources for a user | GitHub API | ||
--- | ||
# Discovering resources for a user | ||
* TOC | ||
{:toc} | ||
When making authenticated requests to the GitHub API, applications often need to fetch the current user's repositories and organizations. In this guide, will explain how to reliably discover those resources. | ||
To interact with the GitHub API, we'll be using [Octokit.rb][octokit.rb]. You can find the complete source code for this project in the [platform-samples][platform samples] repository. | ||
## Getting started | ||
If you haven't already, you should read the ["Basics of Authentication"][basics-of-authentication] guide before working through the examples below. The examples below assume that you have [registered an OAuth application][register-oauth-app] and that your [application has an OAuth token for a user][make-authenticated-request-for-user]. | ||
## Discover the repositories that your app can access for a user | ||
In addition to having their own personal repositories, a user may be a collaborator on repositories owned by other users and organizations. Collectively, these are the repositories where the user has privileged access: either it's a private repository where the user has read or write access, or it's a public repository where the user has write access. | ||
[OAuth scopes][scopes] determine which of those repositories your app can access for a user. Use the workflow below to discover those repositories. | ||
As always, first we'll require [GitHub's Octokit.rb][octokit.rb] Ruby library. Then we'll configure Octokit.rb to automatically handle [pagination][pagination] for us. | ||
#!ruby | ||
require 'octokit' | ||
Octokit.auto_paginate = true | ||
Next, we want to opt in to the [upcoming improvements to the repository listing API][list-repositories-for-current-user]. To do so, we'll set the media type that gives us access to that functionality. | ||
#!ruby | ||
Octokit.default_media_type = "application/vnd.github.moondragon+json" | ||
Now, we'll pass in our application's [OAuth token for a given user][make-authenticated-request-for-user]: | ||
#!ruby | ||
# !!! DO NOT EVER USE HARD-CODED VALUES IN A REAL APP !!! | ||
# Instead, set and test environment variables, like below. | ||
client = Octokit::Client.new :access_token => ENV["OAUTH_ACCESS_TOKEN"] | ||
Then, we're ready to fetch the [repositories that our application can access for the user][list-repositories-for-current-user]: | ||
#!ruby | ||
client.repositories.each do |repository| | ||
full_name = repository[:full_name] | ||
has_push_access = repository[:permissions][:push] | ||
access_type = if has_push_access | ||
"write" | ||
else | ||
"read-only" | ||
end | ||
puts "User has #{access_type} access to #{full_name}." | ||
end | ||
## Discover the organizations that your app can access for a user | ||
Applications can perform all sorts of organization-related tasks for a user. To perform these tasks, the app needs an [OAuth authorization][scopes] with sufficient permission. For example, the `read:org` scope allows you to [list teams][list-teams], and the `user` scope lets you [publicize the user’s organization membership][publicize-membership]. Once a user has granted one or more of these scopes to your app, you're ready to fetch the user’s organizations. | ||
Just as we did when discovering repositories above, we'll start by requiring [GitHub's Octokit.rb][octokit.rb] Ruby library and configuring it to take care of [pagination][pagination] for us: | ||
#!ruby | ||
require 'octokit' | ||
Octokit.auto_paginate = true | ||
Next, we'll opt in to the [upcoming enhancements to the organization listing API][list-orgs-for-current-user]. To do so, we'll set the media type that gives us access to that functionality. | ||
#!ruby | ||
Octokit.default_media_type = "application/vnd.github.moondragon+json" | ||
Now, we'll pass in our application's [OAuth token for a given user][make-authenticated-request-for-user] to initialize our API client: | ||
#!ruby | ||
# !!! DO NOT EVER USE HARD-CODED VALUES IN A REAL APP !!! | ||
# Instead, set and test environment variables, like below. | ||
client = Octokit::Client.new :access_token => ENV["OAUTH_ACCESS_TOKEN"] | ||
Then, we can [list the organizations that our application can access for the user][list-orgs-for-current-user]: | ||
#!ruby | ||
client.organizations.each do |organization| | ||
puts "User belongs to the #{organization[:login]} organization." | ||
end | ||
### Don’t rely on public organizations | ||
If you've read the docs from cover to cover, you may have noticed an [API method for listing a user's public organization memberships][list-public-orgs]. Most applications should avoid this API method. This method only returns the user's public organization memberships, not their private organization memberships. | ||
As an application, you typically want all of the user's organizations (public and private) that your app is authorized to access. The workflow above will give you exactly that. | ||
[basics-of-authentication]: /guides/basics-of-authentication/ | ||
[list-public-orgs]: /v3/orgs/#list-user-organizations | ||
[list-repositories-for-current-user]: /v3/repos/#list-your-repositories | ||
[list-orgs-for-current-user]: /v3/orgs/#list-your-organizations | ||
[list-teams]: /v3/orgs/teams/#list-teams | ||
[make-authenticated-request-for-user]: /guides/basics-of-authentication/#making-authenticated-requests | ||
[octokit.rb]: https://github.com/octokit/octokit.rb | ||
[pagination]: /v3/#pagination | ||
[platform samples]: https://github.com/github/platform-samples/tree/master/api/ruby/discovering-resources-for-a-user | ||
[publicize-membership]: /v3/orgs/members/#publicize-a-users-membership | ||
[register-oauth-app]: /guides/basics-of-authentication/#registering-your-app | ||
[scopes]: /v3/oauth/#scopes |
1 change: 1 addition & 0 deletionslayouts/guides.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.