> ## Documentation Index
> Fetch the complete documentation index at: https://developer.box.com/llms.txt
> Use this file to discover all available pages before exploring further.

# SDKを使用したJWT

export const Link = ({href, children, className, ...props}) => {
  const localizedHref = localizeLink(href);
  return <a href={localizedHref} className={className} {...props}>
      {children}
    </a>;
};

export const MultiRelatedLinks = ({sections = []}) => {
  if (!sections || sections.length === 0) {
    return null;
  }
  return <div className="space-y-8">
      {sections.map((section, index) => <RelatedLinks key={index} title={section.title} items={section.items} />)}
    </div>;
};

export const RelatedLinks = ({title, items = []}) => {
  const getBadgeClass = badge => {
    if (!badge) return "badge-default";
    const badgeType = badge.toLowerCase().replace(/\s+/g, "-");
    return `badge-${badge === "ガイド" ? "guide" : badgeType}`;
  };
  if (!items || items.length === 0) {
    return null;
  }
  return <div className="my-8">
      {}
      <h3 className="text-sm font-bold uppercase tracking-wider mb-4">{title}</h3>

      {}
      <div className="flex flex-col gap-3">
        {items.map((item, index) => <a key={index} href={item.href} className="py-2 px-3 rounded related_link hover:bg-[#f2f2f2] dark:hover:bg-[#111827] flex items-center gap-3 group no-underline hover:no-underline border-b-0">
            {}
            <span className={`px-2 py-1 rounded-full text-xs font-semibold uppercase tracking-wide flex-shrink-0 ${getBadgeClass(item.badge)}`}>
              {item.badge}
            </span>

            {}
            <span className="text-base">{item.label}</span>
          </a>)}
      </div>
    </div>;
};

<RelatedLinks
  title="必須のガイド"
  items={[
{ label: translate("Select Auth Method"), href: "/guides/authentication/select", badge: "GUIDE" },
{ label: translate("Setup with OAuth 2.0"), href: "/guides/authentication/oauth2/oauth2-setup", badge: "GUIDE" }
]}
/>

Box公式SDKには、JWT認証のサポートが組み込まれています。

このガイドでは、Box SDKを使用したJWTによるユーザー認証について説明します。JWT認証はBox APIを直接操作するよう設計されており、ユーザーがアプリケーションを承認するためにBoxを介してリダイレクトする必要はありません。

## 概要

JWT承認を完了するには、以下の手順を完了する必要があります。

1. 構成ファイルを読み取る
2. SDKクライアントを初期化する

このフローが終了すると、アプリケーションには、アプリケーションの代わりにAPIコールを実行するために使用できるBox SDKクライアントが用意されます。

<Note>
  JWTを使用したデフォルトの認証方法は、もともとアプリケーションのサービスアカウントに関連付けられています。このトークンを使用して実行されるAPIコールはどれも、このアプリケーションから実行されているように見えますが、明示的なアクセス権がなければ他のユーザーのファイルやフォルダにはアクセスできません。
</Note>

## 前提条件

開始する前に、以下の手順を完了しておく必要があります。

* 開発者コンソール内でBoxアプリケーションを作成する
* アプリケーション用に秘密キーの構成ファイルを作成してダウンロードし、`config.json`として保存する
* 社内で使用するためにBoxアプリケーションが承認されていることを確認する

## 1. JSON構成を読み取る

Boxアプリケーションを作成すると、アプリケーションの秘密キーとその他の詳細を含む`config.json`ファイルも作成されます。以下に、その例を示します。

```json config.json theme={null}
{
  "boxAppSettings": {
    "clientID": "abc...123",
    "clientSecret": "def...234",
    "appAuth": {
      "publicKeyID": "abcd1234",
      "privateKey": "-----BEGIN ENCRYPTED PRIVATE KEY-----\n....\n-----END ENCRYPTED PRIVATE KEY-----\n",
      "passphrase": "ghi...345"
    }
  },
  "enterpriseID": "1234567"
}
```

このオブジェクトをアプリケーションで使用するには、ファイルから読み取る必要があります。

<CodeGroup>
  ```csharp .Net theme={null}
  var reader = new StreamReader("path/to/config.json");
  var json = reader.ReadToEnd();
  var config = BoxConfig.CreateFromJsonString(json);
  ```

  ```java Java theme={null}
  Reader reader = new FileReader("path/to/config.json");
  BoxConfig config = BoxConfig.readFrom(reader);
  ```

  ```python Python theme={null}
  from boxsdk import JWTAuth

  config = JWTAuth.from_settings_file('path/to/config.json')
  ```

  ```js Node theme={null}
  var config = require("path/to/config.json");
  ```
</CodeGroup>

<Info>
  **JSONの解析**

  プログラミング言語によっては、ファイルからJSONを読み取って解析する方法が複数ある場合があります。エラー処理など、さらに詳細な説明については、使用するプログラミング言語のガイドを参照してください。
</Info>

## 2. SDKクライアントを初期化する

次の手順では、作成した構成を使用してBox SDKを構成し、アプリケーションとして接続するためにクライアントを初期化します。

<CodeGroup>
  ```csharp .Net theme={null}
  var sdk = new BoxJWTAuth(config);
  var token = sdk.AdminToken();
  BoxClient client = sdk.AdminClient(token);
  ```

  ```java Java theme={null}
  BoxDeveloperEditionAPIConnection api = BoxDeveloperEditionAPIConnection.getAppEnterpriseConnection(config);
  ```

  ```python Python theme={null}
  client = Client(config)
  ```

  ```js Node theme={null}
  var sdk = BoxSDK.getPreconfiguredInstance(config);
  var client = sdk.getAppAuthClient("enterprise");
  ```
</CodeGroup>

<Warning>
  **サービスアカウント**

  この時点では、アプリケーションは、管理対象ユーザーまたはApp Userとしてではなく、アプリケーションユーザーとして認証されます。各種ユーザーの詳細については、<Link href="/platform/user-types">ユーザータイプ</Link>に関するガイドをご覧ください。

  **まとめ**

  以下の手順に従うことで、アプリケーションはBox公式SDKのいずれかにより、JWTを使用したアプリケーションの承認を実行できるようになりました。

  1. 構成ファイルを読み取る
  2. SDKクライアントを初期化する

  このクライアントの使用方法を確認するには、<Link href="/guides/api-calls">APIコールの実行</Link>に関するガイドをご覧ください。

  **SDKとJSONウェブトークンの使用**

  各SDKのJWTの詳細については、以下を参照してください。

  * [.Net][.Net]

  * [Java][Java]

  * [Python][Python]

  * [Node][Node]

  * [IOS][IOS]
</Warning>

[.Net]: https://github.com/box/box-windows-sdk-v2/blob/legacy/docs/authentication.md#server-auth-with-jwt

[Java]: https://github.com/box/box-java-sdk/blob/legacy/doc/authentication.md#server-authentication-with-jwt

[Python]: https://github.com/box/box-python-sdk/blob/legacy/docs/usage/authentication.md#server-auth-with-jwt

[Node]: https://github.com/box/box-node-sdk/blob/legacy/docs/authentication.md#server-auth-with-jwt

[IOS]: https://github.com/box/box-ios-sdk/blob/legacy/BoxSDK/docs/usage/authentication.md#server-auth-with-jwt

<RelatedLinks
  title="関連するAPI"
  items={[
{ label: translate("Authorize user"), href: "/reference/get-authorize", badge: "GET" }
]}
/>

<RelatedLinks
  title="関連するガイド"
  items={[
{ label: translate("Platform App"), href: "/guides/applications/platform-apps/index", badge: "GUIDE" },
{ label: translate("Select Auth Method"), href: "/guides/authentication/select", badge: "GUIDE" },
{ label: translate("Setup with OAuth 2.0"), href: "/guides/authentication/oauth2/oauth2-setup", badge: "GUIDE" }
]}
/>
