Get XERO API Access code error: unsupported_grant_type
Question:
I’m trying to get accessToken from Xero
This is their instruction:
https://developer.xero.com/documentation/guides/oauth2/auth-flow/#3-exchange-the-code
POST https://identity.xero.com/connect/token
authorization: "Basic " + base64encode(client_id + ":" + client_secret)
Content-Type: application/x-www-form-urlencoded
grant_type=authorization_code
&code=xxxxxx
&redirect_uri=https://myapp.com/redirect
This is how I do it:
tokenb4 = "{0}:{1}".format(CLIENT_ID, CLIENT_SECRET)
basic_token = base64.urlsafe_b64encode(tokenb4.encode()).decode()
response = requests.post(
"https://identity.xero.com/connect/token",
headers={
"Authorization": "Basic {0}".format(basic_token),
"Content-Type": "application/x-www-form-urlencoded"
},
params="grant_type=authorization_code
&code=MYCODE
&redirect_uri=https://developer.xero.com/"
)
Now I keep getting error 400
{"error":"unsupported_grant_type"}
redirect_uri is the same with the one I leave in the App in Xero demo company.
I’ve googled some, seems like it’s a format issue but I can’t see what’s wrong with my code.
Much appreciation if anyone could help
Answers:
ref:Python requests module sends JSON string instead of x-www-form-urlencoded param string
I changed the params to
data={
"grant_type": "authorization_code",
"code": "MYCODE",
"redirect_uri": "https://developer.xero.com/"
}
And it works! Hooray!
I was getting the same error. It turns out i was converting my body to a json object. Removing that code got it to work.
I’m trying to get accessToken from Xero
This is their instruction:
https://developer.xero.com/documentation/guides/oauth2/auth-flow/#3-exchange-the-code
POST https://identity.xero.com/connect/token
authorization: "Basic " + base64encode(client_id + ":" + client_secret)
Content-Type: application/x-www-form-urlencoded
grant_type=authorization_code
&code=xxxxxx
&redirect_uri=https://myapp.com/redirect
This is how I do it:
tokenb4 = "{0}:{1}".format(CLIENT_ID, CLIENT_SECRET)
basic_token = base64.urlsafe_b64encode(tokenb4.encode()).decode()
response = requests.post(
"https://identity.xero.com/connect/token",
headers={
"Authorization": "Basic {0}".format(basic_token),
"Content-Type": "application/x-www-form-urlencoded"
},
params="grant_type=authorization_code
&code=MYCODE
&redirect_uri=https://developer.xero.com/"
)
Now I keep getting error 400
{"error":"unsupported_grant_type"}
redirect_uri is the same with the one I leave in the App in Xero demo company.
I’ve googled some, seems like it’s a format issue but I can’t see what’s wrong with my code.
Much appreciation if anyone could help
ref:Python requests module sends JSON string instead of x-www-form-urlencoded param string
I changed the params to
data={
"grant_type": "authorization_code",
"code": "MYCODE",
"redirect_uri": "https://developer.xero.com/"
}
And it works! Hooray!
I was getting the same error. It turns out i was converting my body to a json object. Removing that code got it to work.