PostmanからSalesforceのAPIを呼び出すときのINVALID_SESSION_ID

[Postmanのまとめに戻る](https://qiita.com/Keiji_otsubo/items/84098dc24465d5787ea6)


いつもは開発者環境に接続して色々と遊んでいるのですが、違う環境に接続した後に元の開発者環境に接続すると何故かエラーになります。エラーコードは401です。特別に何かを触ったような記憶はない。

:::note alert
[{"message":"Session expired or invalid","errorCode":"INVALID_SESSION_ID"}]
:::

グーグルさんで検索してみると同じ症状の質問/回答があります。

+ [POSTMAN: Invalid session ID even with correct security token](https://trailhead.salesforce.com/trailblazer-community/feed/0D54V00007T40TQSAZ)

ベストアンサーのリンクを読むと

+ [Salesforce Integration INVALID_SESSION_ID using Postman](https://stackoverflow.com/questions/56118080/salesforce-integration-invalid-session-id-using-postman)


:::note
I actually fixed this after banging my head on it forever. You need to go into the settings tab and turn on the Follow Authorization Header setting.
:::

確かに私の設定ではFollow Authorization headerはoffになっていました。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/328136/3669a84d-978e-b50d-3f84-540aa0718407.png)


ここをonにすると機能します。
しかし、この設定は何だろう？

Retain authorization header when a redirect happens to a different hostname.
別のホスト名へのリダイレクトが発生した場合でも、認証ヘッダーを保持します

リダイレクト？　う~ん、そう言えばコンソールを見ていると不思議なことに302のリダイレクトが発生していますね。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/328136/aecbf34a-9ba0-33c8-a5ff-f7103702a515.png)


変数の_endpoint　に　

```
https://brave-koala-c0o5lf-dev-ed.trailblaze.lightning.force.com　
```

を設定していますが、以下にリダイレクトされています。

```
https://brave-koala-c0o5lf-dev-ed.trailblaze.my.salesforce.com
```

試しに先程のFollow Authorization headerをoffにして_endpointにリダイレクト先を設定すると問題なく機能します。やはりリダイレクトが問題だったようです。

私の認識ではlightning.force.com　は変わらないだったのですが.... 

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/328136/cb03aa4a-54d0-e4af-dcb3-72d8fea26118.png)

+ [拡張ドメインを有効にする場合の [私のドメイン] の URL 形式の変更](https://help.salesforce.com/s/articleView?id=sf.domain_name_url_format_changes_enable_enhanced.htm&type=5)

追記；上記のURLとは別に以下の設定があるみたいです。

+ [[私のドメイン] のリダイレクトの管理](https://help.salesforce.com/s/articleView?id=sf.domain_name_redirections_manage.htm&type=5)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/328136/7faa57d0-bf8b-deca-869b-1ebef65c11cb.png)



インスタンスURLをみたらmy.salesforce.comになっていますね。こっちを_endpointにセットしたほうがよさそう。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/328136/5301280e-e302-7d59-ad2a-dca27c0be9d7.png)


そういえば、先程の記事の別の回答にもあったな。

I'm here in 2021 and for me the solution was not 'Follow Authorization Header', but the access token had a different instance_url than expected. Use this instance_url value in _endpoint and you are fine.



# 追記

見落としていました...

 In the _endpoint row, CURRENT VALUE column, paste the **instance_url value** that you copied in step 9.

 [Quick Start: Connect Postman to Salesforce](https://trailhead.salesforce.com/content/learn/projects/quick-start-connect-postman-to-salesforce)

+ [Connect Postman to Salesforce error](https://trailhead.salesforce.com/trailblazer-community/feed/0D5KX00000J5ttA)


# INVALID_SESSION_ID 

+ [INVALID_SESSION_ID when using Postman on SalesForce PlatForm API](https://trailhead.salesforce.com/trailblazer-community/feed/0D54V00007OuMl8)

:::note
The INVALID_SESSION_ID error when using Postman on Salesforce Platform API can occur due to various reasons, including session expiration, IP restrictions, or incorrect session handling. Here are some potential solutions to address this issue:


1. **Refresh the OAuth Access Token**: The OAuth access token might have expired, causing the session to become invalid. Try refreshing the access token using the refresh token obtained during the initial authentication process.

2. **Verify IP Restrictions**: Salesforce might have restrictions on IPs that can access the API. If you're using a different IP address than when you initially authenticated, you might need to add your IP to the allowed list in Salesforce security settings.

3. **Check Session Handling**: Ensure that Postman is handling the session correctly. This might involve setting the appropriate headers and maintaining the session ID throughout your API calls.
 
4. **Use a Different API Endpoint**: Try using a different API endpoint that doesn't involve redirects. Redirects can sometimes interfere with Postman's session handling.

5. **Clear Postman Cache**: Sometimes, cached data in Postman can interfere with API calls. Try clearing Postman's cache and cookies to eliminate any potential conflicts.

6. **Validate Connected App Settings**: Double-check the settings of your connected app in Salesforce. Make sure the app has the necessary permissions to access the API endpoints you're using.
:::