Skip to main content
Answered

401 error "token_not_found" but the access token is provided

  • February 7, 2025
  • 1 reply
  • 3 views

Hello,

I am using Intercom API in a GCP cloud run to do several things, especially updating a user.

Sometimes it works, but sometimes I get an 401 error: “token_not_found”, and I checked multiple times with logs: I am always sending the access token and it’s the right one. “intercom_user_id” is also the right one.

I also checked multiple times my server and logic, and there’s no error.

Here are some request ids where this error happened: 0003sad4ua316ingi1l0, 000an1qat9nc63bsg160, 0021017ivsn8tt93ipd0.

Here is the call in python:

url = f"https://api.intercom.io/contacts/{intercom_user_id}"
headers = {
     "Authorization": f"Bearer {settings.INTERCOM_ACCESS_TOKEN}",
     "Intercom-Version": "2.12",
     "Content-Type": "application/json",
}

update_dict = intercom_profile.model_dump()

try:
    response = await httpx_client.put(url=url, headers=headers, json=update_dict)
     if response.status_code != 200:
         log.info(response.json())
         log.info(f"INTERCOM_ACCESS_TOKEN: {settings.INTERCOM_ACCESS_TOKEN}")
         return False
    return True
except Exception:
    log.exception("failed updating user")
    return False

And an example of what “update_dict” looks like:

{'role': 'user',
 'name': 'username',
 'signed_up_at': 1682037791,
 'last_seen_at': 1738934990,
 'custom_attributes': 
    {'Display name': 'display name',
    'Description': ' ',
    'First name': 'first_name',
    'Last name': 'last_name',
    'Follower count': 37767,
    ...
    }
}

Thank you,

Nawfal

Best answer by mateusz.leszkiewicz

Hi ​@Nawfal Mellah It’s Mat from the Support Engineering Team 😀

I’ve asked ChatGPT to help resolve that issue and it suggested quite good troubleshooting steps.

Since your INTERCOM_ACCESS_TOKEN is sometimes working but sometimes returning 401 token_not_found, there are a few possible reasons for this intermittent issue:

Potential Causes & Debugging Steps

 

1. Token Expiry or Rotation

• If you’re using a short-lived access token (OAuth), it might be expiring.

• Try logging the expiration time of your token and refreshing it proactively before it expires.

 

Check:

• If you’re using an OAuth token, ensure you refresh it regularly.

• If you’re using a personal access token (PAT), verify it’s still valid.

 

2. Rate Limiting or Throttling

• Intercom may rate-limit API calls, and this could be causing authentication failures.

 

Check:

• Look at Intercom’s rate limits here.

• Log the rate-limit headers (X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset) in every response.

 

3. Multiple Processes Using Different Tokens

• If you have multiple instances of your Cloud Run service running and some use an old token, this could cause failures.

 

Check:

• Log INTERCOM_ACCESS_TOKEN each time you make a request.

• Ensure no other process is overriding the token.

 

4. HTTPX Connection Pooling Issue

• If you’re using httpx in async mode, connection pooling issues can sometimes cause authentication problems.

 

Fix:

Try using a persistent client instead of creating a new one for each request:

async with httpx.AsyncClient() as client:
    response = await client.put(url, headers=headers, json=update_dict)

5. GCP Cloud Run Environment Variables

• If the token is stored in an environment variable, it may not always be loaded correctly.

 

Check:

• Print/log settings.INTERCOM_ACCESS_TOKEN right before sending the request to confirm it’s always set.

 

6. Request Formatting Issues

• Ensure that update_dict is always correctly formatted and doesn’t contain invalid fields.

 

Check:

• Try hardcoding a simple request to see if the issue persists.

 

Next Steps

• Log token values and timestamps before requests.

• Check if failures correlate with token expiration or rate limits.

 

If the issue will still happening after checking the steps above, please reach out to us via Messenger in your workspace with the fresh request ids so we can investigate this issue in details for you.

View original
Did this topic help you find an answer to your question?

1 reply

mateusz.leszkiewicz
Intercom Team
Forum|alt.badge.img+5

Hi ​@Nawfal Mellah It’s Mat from the Support Engineering Team 😀

I’ve asked ChatGPT to help resolve that issue and it suggested quite good troubleshooting steps.

Since your INTERCOM_ACCESS_TOKEN is sometimes working but sometimes returning 401 token_not_found, there are a few possible reasons for this intermittent issue:

Potential Causes & Debugging Steps

 

1. Token Expiry or Rotation

• If you’re using a short-lived access token (OAuth), it might be expiring.

• Try logging the expiration time of your token and refreshing it proactively before it expires.

 

Check:

• If you’re using an OAuth token, ensure you refresh it regularly.

• If you’re using a personal access token (PAT), verify it’s still valid.

 

2. Rate Limiting or Throttling

• Intercom may rate-limit API calls, and this could be causing authentication failures.

 

Check:

• Look at Intercom’s rate limits here.

• Log the rate-limit headers (X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset) in every response.

 

3. Multiple Processes Using Different Tokens

• If you have multiple instances of your Cloud Run service running and some use an old token, this could cause failures.

 

Check:

• Log INTERCOM_ACCESS_TOKEN each time you make a request.

• Ensure no other process is overriding the token.

 

4. HTTPX Connection Pooling Issue

• If you’re using httpx in async mode, connection pooling issues can sometimes cause authentication problems.

 

Fix:

Try using a persistent client instead of creating a new one for each request:

async with httpx.AsyncClient() as client:
    response = await client.put(url, headers=headers, json=update_dict)

5. GCP Cloud Run Environment Variables

• If the token is stored in an environment variable, it may not always be loaded correctly.

 

Check:

• Print/log settings.INTERCOM_ACCESS_TOKEN right before sending the request to confirm it’s always set.

 

6. Request Formatting Issues

• Ensure that update_dict is always correctly formatted and doesn’t contain invalid fields.

 

Check:

• Try hardcoding a simple request to see if the issue persists.

 

Next Steps

• Log token values and timestamps before requests.

• Check if failures correlate with token expiration or rate limits.

 

If the issue will still happening after checking the steps above, please reach out to us via Messenger in your workspace with the fresh request ids so we can investigate this issue in details for you.


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings