APIs & Integrations

Rcpp
Member

[SOLVED] (Python Requests) 404 Error when trying to refresh a Token

EDIT: Since I cannot reply this topic, I'm editing to add my solution, right after the problem description.


Hi, all!

 

I'm trying to refresh a Token via Python, where I use Requests library to make a `POST` to `https://api.hubapi.com/oauth/v1/token`. However, this URL returns a `404` error. Bellow I've put my `headers` and call `body`:

Headers (dummy data for secret, id and refresh):

 

 

grant_type=refresh_token&client_id=000&client_secret=000&redirect_uri=https%3A%2F%2Fmyredirect.com&refresh_token=my-refresh-token

 

 

 

 

Request Body:

 

 

{'User-Agent': 'python-requests/2.25.0', 'Accept-Encoding': 'gzip, deflate', 'accept': 'application/json', 'Connection': 'keep-alive', 'Content-Length': '215', 'Content-Type': 'application/x-www-form-urlencoded'}

 

 

 
Response Body:

 

 

{'Date': 'Wed, 02 Dec 2020 16:07:43 GMT', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive', 'Set-Cookie': '__cfduid=d4d40d2a6dec0dd842bb7a151a574434a1606925263; expires=Fri, 01-Jan-21 16:07:43 GMT; path=/; domain=.hubapi.com; HttpOnly; SameSite=Lax', 'X-Trace': '2B5F8F89F5E9195D18978D9BA01CC545D7B9BB7369000000000000000000', 'Cache-Control': 'must-revalidate,no-cache,no-store', 'Access-Control-Allow-Credentials': 'false', 'CF-Cache-Status': 'DYNAMIC', 'cf-request-id': '06c5cebbcb0000db90c307f000000001', 'Expect-CT': 'max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"', 'Strict-Transport-Security': 'max-age=31536000; includeSubDomains; preload', 'Server': 'cloudflare', 'CF-RAY': '5fb64d72dd26db90-GIG'}

 

 

 

I have already read:
https://developers.hubspot.com/docs/api/oauth/tokens
https://developers.hubspot.com/docs/api/oauth-quickstart-guide
https://developers.hubspot.com/docs/api/working-with-oauth
https://developers.hubspot.com/docs/api/intro-to-auth



My request is adapted from the Node.JS example. My authentication (getting the Tokens) was made using the Python library HS provide, but there seems to be no token refreshment method.  I'm running on circles between these documentations, 404 errors or 415 errors (when posting from PostMan or Firefox).

Thank you in advance,
Ricardo


------------------------------------------ -

SOLUTION:


It is a streight forward solution: you can use ``create_token`` method from HubSpot's library, changing the ``grant_type`` and adding your ``refresh_token``  to the parameters list in place of the ``code`` parameter. For instance:

tokens = self.hubspot.auth.oauth.default_api.create_token(
grant_type="refresh_token",
redirect_uri=self.redirect_url,
client_id=self.client_id,
client_secret=self.client_secret,
refresh_token=config.REFRESH_TOKEN
)


After this call, you will have your new Token and can make calls to the API with it. Remember: this method returns an Object, not a JSON.

Cheers!



1 Reply 1
sharonlicari
Community Manager
Community Manager

[SOLVED] (Python Requests) 404 Error when trying to refresh a Token

Thank you for sharing @Rcpp 🙂


Did you know that the Community is available in other languages?
Join regional conversations by changing your language settings !




0 Upvotes