> For the complete documentation index, see [llms.txt](https://docs.roboflow.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.roboflow.com/reference/ja/ren-zheng/authentication/sign-in-with-roboflow-developer-reference.md).

# Sign In With Roboflow（開発者リファレンス）

Sign in with Roboflow の OAuth リファレンス、表示設定、エラー、サンプルアプリ。

Roboflow でサインインを使用するアプリ向けのリファレンス。アプリの登録、PKCE、認可フローについては、以下を参照してください [Roboflowでサインイン（はじめに）](https://docs.roboflow.com/reference/ja/ren-zheng/authentication/sign-in-with-roboflow-getting-started.md).

## サンプルアプリ

[roboflow/siwr\_example\_app](https://github.com/roboflow/siwr_example_app) は、次に対して完全な OAuth フローを実行する小さな Node.js アプリです `app.roboflow.com` および `api.roboflow.com`。トークンは Express セッション内に保持され、ブラウザにクライアントシークレットが見えることはありません。

```bash
git clone https://github.com/roboflow/siwr_example_app.git
cd siwr_example_app
cp .env.example .env
# RF_CLIENT_ID、RF_CLIENT_SECRET、SESSION_SECRET を設定
npm install && npm run dev
```

リダイレクト URI を指定して OAuth アプリを登録する `http://localhost:3001/oauth/callback` およびアプリが使用するスコープ（`openid`, `profile`, `email`, `workspace:read`, `project:read`, `model:infer`).

<table data-search="false"><thead><tr><th>ルート</th><th>目的</th></tr></thead><tbody><tr><td><code>GET /</code></td><td>ランディングページ、またはサインイン後のダッシュボード</td></tr><tr><td><code>GET /oauth/start</code></td><td>PKCE リダイレクトで OAuth を開始</td></tr><tr><td><code>GET /oauth/callback</code></td><td>コードを交換してダッシュボードを表示</td></tr><tr><td><code>POST /logout</code></td><td>トークンを失効させ、セッションをクリア</td></tr></tbody></table>

### PKCE と認可 URL

以下より [`src/oauth.ts`](https://github.com/roboflow/siwr_example_app/blob/main/src/oauth.ts):

```typescript
export function pkcePair(): { verifier: string; challenge: string } {
    const verifier = crypto.randomBytes(48).toString("base64url");
    const challenge = crypto.createHash("sha256").update(verifier).digest("base64url");
    return { verifier, challenge };
}

export function buildAuthorizeUrl(state: string, codeChallenge: string): string {
    const url = new URL(`${config.appHost}/oauth/authorize`);
    url.searchParams.set("response_type", "code");
    url.searchParams.set("client_id", config.clientId);
    url.searchParams.set("redirect_uri", config.redirectUri);
    url.searchParams.set("scope", REQUESTED_SCOPE_STRING);
    url.searchParams.set("state", state);
    url.searchParams.set("code_challenge", codeChallenge);
    url.searchParams.set("code_challenge_method", "S256");
    return url.toString();
}
```

`GET /oauth/start` 保存する `検証子` および `state` をセッションに保存し、その後このURLにリダイレクトします。

### コールバックと state の確認

以下より [`src/server.ts`](https://github.com/roboflow/siwr_example_app/blob/main/src/server.ts):

```typescript
const pending = req.session.oauth;
req.session.oauth = undefined;

if (!pending || !code || state !== pending.state) {
    // 拒否: state が不正、または code がありません
    return;
}

const data = await exchangeCodeForTokens(code, pending.verifier);
req.session.tokens = tokensFromTokenResponse(data);
```

コードを交換する前に、一度しか使えない OAuth セッションデータを必ず削除してください。 `exchangeCodeForTokens` code、 `redirect_uri`, `code_verifier`、client ID と client secret を [トークンエンドポイント](https://docs.roboflow.com/reference/ja/ren-zheng/authentication/sign-in-with-roboflow-getting-started.md#get-or-refresh-tokens).

## トークンエンドポイント認証

Workspace OAuth アプリは confidential client です。トークン、revoke、introspect エンドポイントへの各呼び出しには client secret が含まれます。アプリを「Workspace Settings > Developer」で作成または編集するときは、クライアントまたはゲートウェイに合った方式を選択してください。

<table data-search="false"><thead><tr><th>方式</th><th>動作</th></tr></thead><tbody><tr><td><code>client_secret_post</code></td><td>送信 <code>client_id</code> および <code>client_secret</code> をリクエスト本文のフォームフィールドとして送信します。既定値です。</td></tr><tr><td><code>client_secret_basic</code></td><td>HTTP を送信 <code>Authorization: Basic</code> ヘッダーに、base64 エンコードされた <code>client_id:client_secret</code>を含め、両方を本文から除外します。</td></tr></tbody></table>

MCP コネクタのように自ら登録するパブリッククライアントは、 `client_id` のみを送信し、PKCE に依存します。

## ホスト

<table data-search="false"><thead><tr><th>サービス</th><th>ベース URL</th></tr></thead><tbody><tr><td>サインイン、トークン、OIDC</td><td><code>https://app.roboflow.com</code></td></tr><tr><td>REST API</td><td><code>https://api.roboflow.com</code></td></tr></tbody></table>

## 公開範囲

どのワークスペースがあなたの OAuth アプリでサインインできるかを制御します。

<table data-search="false"><thead><tr><th>ダッシュボードのオプション</th><th>サインインできるユーザー</th></tr></thead><tbody><tr><td>内部</td><td>アプリを作成したワークスペースのメンバーのみ</td></tr><tr><td>非公開</td><td>あなたの client ID を持つすべてのワークスペース</td></tr><tr><td>公開</td><td>非公開と同じに加え、機能が有効な場合は「Sign in with Roboflow」ディレクトリに掲載されます</td></tr></tbody></table>

「External」に相当する別の公開範囲タイプはありません。ワークスペースをまたぐ統合には「非公開」を使用します。

各顧客ワークスペースでは、External OAuth apps ポリシーも設定できます。すべて許可、一覧表示された client ID のみ許可、またはすべてブロックです。ワークスペース外でサインインに失敗する場合は、顧客に対し、ワークスペース設定であなたの client ID を許可するよう依頼してください。

## エンドポイント

すべてのサインインおよびトークンエンドポイントは `app.roboflow.com`.

<table data-search="false"><thead><tr><th>目的</th><th>方式</th><th>URL</th></tr></thead><tbody><tr><td>認可（ブラウザリダイレクト）</td><td>GET</td><td><code>/oauth/authorize</code></td></tr><tr><td><a href="https://docs.roboflow.com/reference/ja/ren-zheng/authentication/sign-in-with-roboflow-getting-started.md#get-or-refresh-tokens">トークンを取得または更新</a></td><td>POST</td><td><code>/oauth/token</code></td></tr><tr><td><a href="https://docs.roboflow.com/reference/ja/ren-zheng/authentication/sign-in-with-roboflow-getting-started.md#revoke-a-token">トークンを失効させる</a></td><td>POST</td><td><code>/oauth/revoke</code></td></tr><tr><td><a href="https://docs.roboflow.com/reference/ja/ren-zheng/authentication/sign-in-with-roboflow-getting-started.md#get-user-info">ユーザー情報</a></td><td>GET</td><td><code>/oauth/userinfo</code></td></tr><tr><td><a href="#introspect-a-token">トークンをイントロスペクトする</a></td><td>POST</td><td><code>/oauth/introspect</code></td></tr><tr><td><a href="https://docs.roboflow.com/reference/ja/ren-zheng/authentication/sign-in-with-roboflow-getting-started.md#validate-a-token">トークンを検証する</a></td><td>GET</td><td><code>/oauth/validate</code></td></tr><tr><td>OIDC ディスカバリ</td><td>GET</td><td><code>/.well-known/openid-configuration</code></td></tr><tr><td>JWKS、検証用 <code>id_token</code> JWT</td><td>GET</td><td><code>/.well-known/jwks.json</code></td></tr></tbody></table>

### トークンのイントロスペクト

クライアント資格情報を使用して、トークンが有効かどうかと付与内容を返します。クライアント資格情報なしでアクセストークンを確認するには、 [トークンを検証する](https://docs.roboflow.com/reference/ja/ren-zheng/authentication/sign-in-with-roboflow-getting-started.md#validate-a-token) を使用してください。

{% openapi src="/files/7a5b370d857a9984a15f37c834e4a85a9a2fa935" path="/oauth/introspect" method="post" %}
[sign-in-with-roboflow.yaml](https://1568109427-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FgmbAGf4KNeNPXGuUuKYv%2Fuploads%2Fgit-blob-5e04f3f2d52edd3c9a11b3f2b4ac16e9bf05206c%2Fsign-in-with-roboflow.yaml?alt=media)
{% endopenapi %}

## REST API でトークンを使用する

送信 `Authorization: Bearer {access_token}` から `api.roboflow.com`。参照: [REST API で認証する](https://docs.roboflow.com/reference/ja/platform/rest-api/authenticate-with-the-rest-api.md).

## よくあるエラー

<table data-search="false"><thead><tr><th>レスポンス</th><th>意味</th><th>対処法</th></tr></thead><tbody><tr><td>400 <code>invalid_scope</code></td><td>要求されたスコープがアプリの「許可されたスコープ」リストにありません。</td><td>ダッシュボードでスコープを追加するか、リクエストから削除してください。</td></tr><tr><td>400 <code>invalid_grant</code></td><td>code または refresh token が期限切れ、使用済み、または失効済みであるか、あるいはワークスペースがもはやあなたのアプリを許可していません。</td><td>ユーザーを再度サインインさせてください。</td></tr><tr><td>401 <code>invalid_client</code></td><td>client ID または secret が間違っているか、secret が誤った方式で送信されています。</td><td>アプリのトークンエンドポイント認証設定を確認してください。</td></tr><tr><td>401 <code>OAuthException</code> で <code>api.roboflow.com</code></td><td>アクセストークンが期限切れか、失効済みです。</td><td>1回更新してから、ユーザーを再度サインインさせてください。</td></tr><tr><td>404 <code>GraphMethodException</code> で <code>api.roboflow.com</code></td><td>トークンにそのルートのスコープがないか、またはそのルートがトークンにバインドされていないワークスペースにあります。</td><td>必要なスコープでユーザーに再度同意してもらってください。</td></tr><tr><td>403 で <code>api.roboflow.com</code></td><td>ユーザーの役割ではその操作を許可されていません。</td><td>ワークスペース管理者がユーザーの役割を変更します。</td></tr><tr><td>リダイレクトの不一致</td><td><code>redirect_uri</code> が登録済みのリダイレクト URI と一致しません。</td><td>ダッシュボードの設定か認可 URL を修正してください。</td></tr></tbody></table>

## セキュリティ

* すべての認可リクエストで PKCE を使用してください。
* 確認 `state` すべてのコールバックで。
* 決して公開しない `client_secret` またはリフレッシュトークンをブラウザに公開しないでください。
* ユーザーがサインアウトしたらトークンを失効させてください。
* 必要なスコープだけを要求してください。完全な一覧は [利用可能なスコープ](https://docs.roboflow.com/reference/ja/ren-zheng/authentication/sign-in-with-roboflow-getting-started.md#available-scopes).
