JSON Web トークン (JWT) の生成
アクセストークンを取得するには、特定の要求で JWT を生成し、秘密鍵を使用して RS256 アルゴリズムで署名する必要があります。
必要なエディション
| 必要なユーザー権限 | |
|---|---|
| API トークンを生成する | 「Enable API Access (API アクセスの有効化)」権限を持つユーザー |
ステップ 1: トークン要求エンドポイント URL を取得する
まず、トークン要求エンドポイント URL を取得する必要があります。
- Intelligence で API トークンを生成するときに取得した discoveryEndpoint URL を、ブラウザーの検索バーに挿入します。
- [Enter] キーを押します。
-
返された “token_endpoint” 項目値をコピーします。
AWS US (US1) の場合の例:
"token_endpoint": "https://idp.intelligence.salesforce.com/us1/token"
ステップ 2: JWT に署名する
JWT に署名するには、Intelligence で API トークンを生成するときに提供された秘密鍵を使用してペイロードをエンコードする必要があります。
Python を使用している場合、次のモジュールが必要です。
import jwt
import requests
import uuid
import time
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.backends import default_backend
Intelligence プラットフォームでは、JWT モジュール (jwt 1.3.1) がサポートされています。
JWT は、特定の要求で生成し、秘密鍵を使用して RS256 アルゴリズムで署名する必要があります。
JWT Claims (payload):
{
"iss": "your-client-id",
"aud": "https://idp.intelligence.salesforce.com"",
"sub": "your-client-id",
"iat": 1690129371, (time when the token was issued)
"jti": "45911cd010b846f9810b150c969b0007"
}
"iss" および "sub": serviceAccountId に相当
"iat": エポック時間 (秒)
"jti":「-」のないランダムな UUID
JWT の例
# env can be us1 / us2 / eu1 / eu2
app = 'us1'
service_account_id = ''
private_key_str = '''-----BEGIN RSA PRIVATE KEY-----\nMIIEogI....REDACTED.....xweWOhhutft/sA=\n-----END RSA PRIVATE
KEY-----'''
private_key = serialization.load_pem_private_key(
private_key_str.encode(),
password=None,
backend=default_backend()
)
private_key_jwk = jwt.jwk_from_pem(private_key_str.encode())
payload = {
"iss": service_account_id,
"aud": "https://idp.intelligence.salesforce.com"",
"sub": service_account_id,
"iat": int(time.time()),
"jti": str(uuid.uuid4()).replace('-','')
}
jwt_object = jwt.JWT()
jwt_token = jwt_object.encode(payload, private_key_jwk, alg='RS256')
ステップ 3: JWT を使用したプラットフォームトークンの取得
トークンエンドポイントは、API クライアント情報から取得した discoveryEndpoint URL で入手できます。
"token_endpoint":
"https://idp.intelligence.salesforce.com/us1/token""
| パラメーター | 型 | 説明 |
|---|---|---|
| client_id | string | クライアント ID (serviceAccountId)。 |
| client_assertion | string | JWT |
| client_assertion_type | string | クライアントアサーションのタイプ。(固定値: 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer') |
| grant_type | string | 付与タイプ。(固定値: 'client_credentials') |
要求の構造
[token_endpoint_obtained_from_the_beginning_of_Step_2]?grant_type=client_credentials&client_assertion=[your_signed_jwt_token]&client_id=[your_client_id]&client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer
応答の例
{
"access_token": "eyJraWQ...",
"expires_in": 300
}
- “access_token” — API コールの実行に使用する、発行されたアクセストークン。
- “expires_in” — アクセストークンの有効期間は 5 分 (300 秒) です。

