<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Webhooks and bearer tokens in APIs &amp; Integrations</title>
    <link>https://community.hubspot.com/t5/APIs-Integrations/Webhooks-and-bearer-tokens/m-p/1122076#M80853</link>
    <description>&lt;P data-start="80" data-end="352"&gt;Hello&amp;nbsp;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/711931"&gt;@TracyO1970&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P data-start="80" data-end="352"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P data-start="80" data-end="352"&gt;It might be possible to handle token expiration by implementing a caching mechanism in your code. One approach could be to store the token in memory or a database along with its expiration time, so your webhook always retrieves a valid token before sending data.&lt;/P&gt;
&lt;P data-start="80" data-end="352"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P data-start="354" data-end="624"&gt;You might try creating a function that checks whether the token is still valid before making a request. If it’s expired, the function could automatically request a new token from the vendor’s API and update the stored value.&amp;nbsp; Something like this might work:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;import time
import requests

TOKEN_CACHE = {
    "access_token": None,
    "expires_at": 0  # Timestamp when the token expires
}

VENDOR_API_URL = "https://yourvendor.com/api/token"  # Replace with actual token endpoint
VENDOR_API_SECRET = "your_secret_key"  # Store securely

def get_access_token():
    current_time = time.time()
    
    # Maybe check if the token is still valid
    if TOKEN_CACHE["access_token"] and TOKEN_CACHE["expires_at"] &amp;gt; current_time:
        return TOKEN_CACHE["access_token"]
    
    # If expired, possibly request a new token
    response = requests.post(VENDOR_API_URL, json={"secret": VENDOR_API_SECRET})
    if response.status_code == 200:
        token_data = response.json()
        TOKEN_CACHE["access_token"] = token_data.get("access_token")
        TOKEN_CACHE["expires_at"] = current_time + 3600  # Assuming 1-hour validity
        
        return token_data.get("access_token")
    else:
        print("Could not retrieve token, might need to debug further.")
        return None
&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For the webhook request, you could modify it so that it always fetches the latest token before sending data:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;def send_webhook_data(payload):
    token = get_access_token()
    if not token:
        print("No valid token available.")
        return

    headers = {
        "Authorization": f"Bearer {token}",
        "Content-Type": "application/json"
    }

    webhook_url = "https://yourwebhook.com/receive-data"  # Replace with actual webhook URL
    response = requests.post(webhook_url, json=payload, headers=headers)

    if response.status_code == 401:  # Maybe try refreshing the token
        TOKEN_CACHE["access_token"] = None  # Force token refresh
        token = get_access_token()
        if not token:
            print("Still no valid token, request might fail.")
            return
        
        headers["Authorization"] = f"Bearer {token}"
        response = requests.post(webhook_url, json=payload, headers=headers)

    return response.status_code, response.json()
&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This might help by ensuring that your webhook always has a fresh token without requiring manual intervention. However, if the vendor’s token mechanism is more complex, this approach might not be sufficient, and additional error handling could be necessary.&lt;BR /&gt;&lt;BR /&gt;Thanks&amp;nbsp;&lt;BR /&gt;Shubham Nigam&lt;BR /&gt;&lt;A href="https://indianhubspotexpert.com/" target="_blank" rel="noopener"&gt;Indian HubSpot Expert&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 13 Mar 2025 16:24:30 GMT</pubDate>
    <dc:creator>SNigam</dc:creator>
    <dc:date>2025-03-13T16:24:30Z</dc:date>
    <item>
      <title>Webhooks and bearer tokens</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Webhooks-and-bearer-tokens/m-p/1122005#M80850</link>
      <description>&lt;P&gt;I am trying to use a webhook to send data from hubspot when a certain piece of data is updated on a deal.&amp;nbsp; I have custom code that is getting the access token (this vendor's token only lasts an hour so it requires frequent refreshing - and this is not an oAuth endpoint).&amp;nbsp; If I store the secret manually and use it - it works perfectly - but once the token expires I have to manually refresh.&amp;nbsp; Is there a way I can get the access token from my custom code and send that?&amp;nbsp; I am hoping there is something I am missing because tokens frequently need refreshing.&amp;nbsp; Any help would be greatly appreciated!&amp;nbsp; Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Mar 2025 14:40:47 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Webhooks-and-bearer-tokens/m-p/1122005#M80850</guid>
      <dc:creator>TracyO1970</dc:creator>
      <dc:date>2025-03-13T14:40:47Z</dc:date>
    </item>
    <item>
      <title>Re: Webhooks and bearer tokens</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Webhooks-and-bearer-tokens/m-p/1122076#M80853</link>
      <description>&lt;P data-start="80" data-end="352"&gt;Hello&amp;nbsp;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/711931"&gt;@TracyO1970&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P data-start="80" data-end="352"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P data-start="80" data-end="352"&gt;It might be possible to handle token expiration by implementing a caching mechanism in your code. One approach could be to store the token in memory or a database along with its expiration time, so your webhook always retrieves a valid token before sending data.&lt;/P&gt;
&lt;P data-start="80" data-end="352"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P data-start="354" data-end="624"&gt;You might try creating a function that checks whether the token is still valid before making a request. If it’s expired, the function could automatically request a new token from the vendor’s API and update the stored value.&amp;nbsp; Something like this might work:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;import time
import requests

TOKEN_CACHE = {
    "access_token": None,
    "expires_at": 0  # Timestamp when the token expires
}

VENDOR_API_URL = "https://yourvendor.com/api/token"  # Replace with actual token endpoint
VENDOR_API_SECRET = "your_secret_key"  # Store securely

def get_access_token():
    current_time = time.time()
    
    # Maybe check if the token is still valid
    if TOKEN_CACHE["access_token"] and TOKEN_CACHE["expires_at"] &amp;gt; current_time:
        return TOKEN_CACHE["access_token"]
    
    # If expired, possibly request a new token
    response = requests.post(VENDOR_API_URL, json={"secret": VENDOR_API_SECRET})
    if response.status_code == 200:
        token_data = response.json()
        TOKEN_CACHE["access_token"] = token_data.get("access_token")
        TOKEN_CACHE["expires_at"] = current_time + 3600  # Assuming 1-hour validity
        
        return token_data.get("access_token")
    else:
        print("Could not retrieve token, might need to debug further.")
        return None
&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For the webhook request, you could modify it so that it always fetches the latest token before sending data:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;def send_webhook_data(payload):
    token = get_access_token()
    if not token:
        print("No valid token available.")
        return

    headers = {
        "Authorization": f"Bearer {token}",
        "Content-Type": "application/json"
    }

    webhook_url = "https://yourwebhook.com/receive-data"  # Replace with actual webhook URL
    response = requests.post(webhook_url, json=payload, headers=headers)

    if response.status_code == 401:  # Maybe try refreshing the token
        TOKEN_CACHE["access_token"] = None  # Force token refresh
        token = get_access_token()
        if not token:
            print("Still no valid token, request might fail.")
            return
        
        headers["Authorization"] = f"Bearer {token}"
        response = requests.post(webhook_url, json=payload, headers=headers)

    return response.status_code, response.json()
&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This might help by ensuring that your webhook always has a fresh token without requiring manual intervention. However, if the vendor’s token mechanism is more complex, this approach might not be sufficient, and additional error handling could be necessary.&lt;BR /&gt;&lt;BR /&gt;Thanks&amp;nbsp;&lt;BR /&gt;Shubham Nigam&lt;BR /&gt;&lt;A href="https://indianhubspotexpert.com/" target="_blank" rel="noopener"&gt;Indian HubSpot Expert&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Mar 2025 16:24:30 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Webhooks-and-bearer-tokens/m-p/1122076#M80853</guid>
      <dc:creator>SNigam</dc:creator>
      <dc:date>2025-03-13T16:24:30Z</dc:date>
    </item>
    <item>
      <title>Re: Webhooks and bearer tokens</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Webhooks-and-bearer-tokens/m-p/1122562#M80880</link>
      <description>&lt;P&gt;Thank you so much - very helpful!&lt;/P&gt;</description>
      <pubDate>Fri, 14 Mar 2025 17:47:48 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Webhooks-and-bearer-tokens/m-p/1122562#M80880</guid>
      <dc:creator>TracyO1970</dc:creator>
      <dc:date>2025-03-14T17:47:48Z</dc:date>
    </item>
  </channel>
</rss>

