0

Getting "unsupported_grant_type" when trying to refresh token using python

Hi,

I've been trying to get a new access token & refresh token using an existing refresh token that I have. I am following the documentation as stated on the website https://developer.xero.com/documentation/oauth2/auth-flow but I keep getting an error saying "unsupported_grant_type" although I do define grant_type = refresh_token. Here's my code, any help would be greatly appreciated.

import json
from base64 import b64encode

client_id = xxx
client_secret =  xxx
RefreshToken = xxx
    
b64_id_secret = b64encode(client_id + ':' + client_secret)
    
def XeroRefreshToken(refresh_token):
    token_refresh_url = 'https://identity.xero.com/connect/token'
    response = requests.post(token_refresh_url,
                            headers = {
                                'Authorization' : 'Basic ' + b64_id_secret,
                                'Content-Type': 'application/x-www-form-urlencoded'
                            },
                            data = {
                                'grant_type' : 'refresh_token',
                                'refresh_token' : refresh_token
                            })
    json_response = response.json()
    print(json_response)
    
    new_refresh_token = json_response['refresh_token']

XeroRefreshToken(RefreshToken)
droopsnoot
  • 931
  • 1
  • 7
  • 11
dia986
  • 1
  • 1
  • Don't you need to send the `client_id` as part of the body when you're trying to refresh a token? – droopsnoot Feb 22 '22 at 18:05
  • client_id and client_secret are passed in as a part of the b64_id_secret variable. – dia986 Feb 24 '22 at 13:40
  • Oh, OK, that's different to how I do it. I realise now that I don't have a client_secret, so that's probably the difference. For mine, I put the grant-type, client_id and refresh_token in the post body, and the access token and tenant id in the header. – droopsnoot Feb 24 '22 at 18:06

0 Answers0