このエラーは、外部エンドポイントが OAuth トークンを正常に取得するにあたり API コールで特定のヘッダーが必要となるために発生します。これらのヘッダーには、apiKey、client_id、client_secret、または認証に必要なその他のカスタムヘッダーなどがあります。
Salesforce の指定ログイン情報を使用する場合、[外部ログイン情報] 設定で指定されているカスタムヘッダーは、指定ログイン情報エンドポイントで使用されることが想定されています。つまり、このヘッダーはトークンエンドポイントから OAuth トークンを取得するときではなく、外部サービスに対して実際に API コールを行うときに適用されます。
ただし、OAuth トークンの要求は他の API コールに先立って実行される重要なステップです。これは Salesforce が OAuth トークンと認証情報を交換するプロセスであり、以降の外部サービスへの API コールの認証に使用されます。外部ログイン情報で設定されたカスタムヘッダーは、トークンエンドポイント要求には適用されないため、このステップで必要なヘッダー (apiKey など) が不足します。その結果、OAuth トークンを取得できず、「System.CalloutException:Unable to fetch the OAuth token」エラーが発生します。
この制約は、指定ログイン情報が Salesforce で API コールの認証を管理する便利な手法である一方、現時点ではトークンエンドポイントに対してカスタムヘッダーを渡すことには対応していないということを意味します。そのため、トークン要求でカスタムヘッダーが必要な場合は、別の方法を使用する必要があります。
この問題を解決するには、次の方法があります。
a). Apex コードを使用してカスタムソリューションを実装する
この方法では、必要なカスタムヘッダーをトークン要求と以降の API コールに含めることができるため、外部エンドポイントの要件を確実に満たすことができます。
1 - カスタム Apex クラスを作成します。
Apex コードの例:
注意 - これはコードスニペットですので、シナリオに従って使用または変更してください。
public class CustomApiCallout {
public static HttpResponse makeCallout() {
HttpRequest req = new HttpRequest();
req.setEndpoint('https://your-external-endpoint.com/token');
req.setMethod('POST');
// カスタムヘッダーを設定します
req.setHeader('Content-Type', 'application/json');
req.setHeader('apiKey', 'YOUR_API_KEY');
// リクエストのボディを設定します
req.setBody('{"client_id":"YOUR_CLIENT_ID","client_secret":"YOUR_CLIENT_SECRET"}');
Http http = new Http();
HttpResponse res = http.send(req);
if (res.getStatusCode() == 200) {
// 応答を処理します
return res;
} else {
// エラーを処理します
throw new CalloutException('Failed to get OAuth token: ' + res.getStatus());
}
}
}
2 - カスタム Apex クラスを呼び出します。
呼び出しの例:
HttpResponse response = CustomApiCallout.makeCallout();
System.debug(response.getBody());
3 - 認証資格情報を保護します。
4 - 構成をテストします。
b).プロキシサービスを使用する
要求をトークンエンドポイントに転送する前に必要なヘッダーを追加するミドルウェアエンドポイントを作成します。
002471436

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.