Python Citrix Sharefile Token Request giving 400 error

299 Views Asked by At

I am trying to make a simple request to get the access token from Citrix ShareFile, but it's throwing 400 error.

I am going exactly as it's mentioned in the documentation, except changing Python2 code with HTTPLib, with Python3 code with Requests. The code is:

url = 'https://{my_domain}.sharefile.com/oauth/token'

headers = {'Content_Type': 'application/x-www-form-urlencoded'}
params = {'grant_type':'password', 'client_id':my_client_id, 'client_secret':my_client_secret, 'username':my_username, 'password':my_password}

response = requests.post(url, params=params, headers = headers)
print(response.status_code, response.reason)

I get the following response:

400 Bad Request

I also added urllib.parse.urlencode to params, but am still getting the same response error

response = requests.post(url, params=urllib.parse.urlencode(params), headers = headers)

Request guidance on what am I doing wrong. TIA

2

There are 2 best solutions below

1
On

When I add content-type my issue solved.

Check and add valid content-type

0
On

It could be the password, in the context of Sharefile it means app_password and not the password used to login to website. Or response_type to 'code'.

SF Auth is through OAuth2 with a GrandType: OAuth2 Password Grant

This way works for me:

url = 'https://{my_domain}.sharefile.com/oauth/token'
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
data = {
    'response_type': 'code',
    'grant_type': 'password',
    'client_id': '<YOUR CLIENT ID>',
    'client_secret': '<YOUR SECRET>',
    'username': '<USERNAME>',
    'password': '<APP_PASSWORD>'  # not regular password to login using web
}
response = requests.post(url, data=data, headers=headers)

Response contains token and refresh token.