# ユーザのリソースを調べる

REST APIに対する認証済みリクエストにおいて、アプリケーションがアクセスできるユーザのリポジトリやOrganizationを確実に調べる方法を学びます。

GitHub API に対して認証済み要求を行う場合、多くの場合、アプリケーションは現在のユーザーのリポジトリと組織をフェッチする必要があります。 このガイドでは、これらのリソースを確実に調べる方法について説明します。

GitHub API と対話するには、[Octokit.rb](https://github.com/octokit/octokit.rb) を使用します。 このプロジェクトの完全なソース コードは、[platform-samples](https://github.com/github/platform-samples/tree/master/api/ruby/discovering-resources-for-a-user) リポジトリにあります。

## 始めに

まだ「[認証の基本](/ja/enterprise-server@3.22/apps/oauth-apps/building-oauth-apps/authenticating-to-the-rest-api-with-an-oauth-app)」を読んでいない場合は、読んでから以下の例に取り組んでください。 次の例では、[OAuth appを登録していること](/ja/enterprise-server@3.22/apps/oauth-apps/building-oauth-apps/authenticating-to-the-rest-api-with-an-oauth-app#registering-your-app)と、[アプリケーションにユーザーの OAuth トークン](/ja/enterprise-server@3.22/apps/oauth-apps/building-oauth-apps/authenticating-to-the-rest-api-with-an-oauth-app#making-authenticated-requests)があることを前提としています。

## アプリケーションでアクセス可能なユーザのリポジトリを調べる

ユーザは、個人でリポジトリを所有する他に、別のユーザやOrganizationが所有するリポジトリのコラボレータであることもあります。 これらはまとめて、ユーザーが特権アクセス権を持つリポジトリです。これは、ユーザーが読み取りまたは書き込みアクセス権を持つプライベート リポジトリであるか パブリック リポジトリ内部リポジトリ リポジトリです。

[OAuth スコープ](/ja/enterprise-server@3.22/apps/oauth-apps/building-oauth-apps/scopes-for-oauth-apps)と [Organization のアプリケーション ポリシー](https://developer.github.com/changes/2015-01-19-an-integrators-guide-to-organization-application-policies/)によって、アプリでユーザーに対してアクセスできるリポジトリが決まります。 以下のワークフローを使用して、これらのリポジトリを調べます。

いつものように、最初に[GitHubの Octokit.rb](https://github.com/octokit/octokit.rb) Ruby ライブラリが必要になります。 そして、ページネーションを自動的に処理するように Octokit.rb を構成します。 ページネーションの詳細については、「[REST API でのページネーションの使用](/ja/enterprise-server@3.22/rest/using-the-rest-api/using-pagination-in-the-rest-api)」を参照してください。

```ruby
require 'octokit'

Octokit.auto_paginate = true
```

次に、アプリケーションの[特定ユーザー用の OAuth トークン](/ja/enterprise-server@3.22/apps/oauth-apps/building-oauth-apps/authenticating-to-the-rest-api-with-an-oauth-app#making-authenticated-requests)を渡します。

```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"]
```

これで、[アプリケーションがそのユーザーに対してアクセスできるリポジトリ](/ja/enterprise-server@3.22/rest/repos/repos#list-repositories-for-the-authenticated-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
```

## あなたのアプリがユーザーのためにアクセスできる組織を調べる

アプリケーションは、ユーザに対してOrganizationに関するあらゆるタスクを実行できます。 これらのタスクを実行するには、アプリに十分な権限を持つ [OAuth 承認](/ja/enterprise-server@3.22/apps/oauth-apps/building-oauth-apps/scopes-for-oauth-apps)が必要です。 たとえば、`read:org` スコープを使うと[チームの一覧を表示する](/ja/enterprise-server@3.22/rest/teams/teams#list-teams)ことができ、`user` スコープでは[ユーザーの Organization メンバーシップを公開する](/ja/enterprise-server@3.22/rest/orgs/members#set-public-organization-membership-for-the-authenticated-user)ことができます。 ユーザーがこれらのスコープのうち一つ以上をアプリケーションに許可すると、ユーザーの組織を取得する準備ができます。

上記のリポジトリを検出したときと同様に、まず[GitHubの Octokit.rb](https://github.com/octokit/octokit.rb) Ruby ライブラリを要求し、改ページ位置の処理を行う構成を行います。 ページネーションの詳細については、「[REST API でのページネーションの使用](/ja/enterprise-server@3.22/rest/using-the-rest-api/using-pagination-in-the-rest-api)」を参照してください。

```ruby
require 'octokit'

Octokit.auto_paginate = true
```

次に、アプリケーションの[特定ユーザーに対する OAuth トークン](/ja/enterprise-server@3.22/apps/oauth-apps/building-oauth-apps/authenticating-to-the-rest-api-with-an-oauth-app#making-authenticated-requests)を渡して、API クライアントを初期化します。

```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"]
```

これで、[アプリケーションがそのユーザーに対してアクセスできる Organization の一覧を取得する](/ja/enterprise-server@3.22/rest/orgs/orgs#list-organizations-for-the-authenticated-user)ことができます。

```ruby
client.organizations.each do |organization|
  puts "User belongs to the #{organization[:login]} organization."
end
```

### ユーザのすべてのOrganizationメンバーシップを返す

ドキュメントをよく読むと、[ユーザーのパブリックな Organization のメンバーシップの一覧を取得する API メソッド](/ja/enterprise-server@3.22/rest/orgs/orgs#list-organizations-for-a-user)に気付くかもしれません。 ほとんどのアプリケーションでは、このAPIメソッドを避けるべきです。 このメソッドは、ユーザのパブリックなOrganizationに属するメンバーだけを返し、プライベートなOrganizationに属するメンバーは返しません。

アプリケーションでは通常、アプリがアクセスを認可されているすべてのユーザーの組織を取得したいと考えます。 上記のワークフローでは、まさにこれを実行しています。