user3330601 user3330601 - 3 months ago 9
Python Question

Unclear on making an API call after authentication?

I am looking on how to use an APIs, more specifically Egnyte's API.

From their documentation, I must first get a oauth2 token, which I was able to get successfully.

Here are their documentation:



However, I am not sure what to do afterwards. I am trying to use their User management API, which I am suppose to make a call to:

https://{Egnyte Domain}.egnyte.com/pubapi/v2/users

However, how do i use their token for a requests.get call to their api?
Below is my python code, I am using the the Requests Module (http://docs.python-requests.org/en/latest/):

import requests

api_key = 'MY_API_KEY'
username = 'myUserName'
password = 'myPassword'

payload = {
'grant_type': 'password',
'client_id': api_key,
'username': username,
'password': password
}

token = requests.post("https://{Egnyte Domain}.egnyte.com/puboauth/token", params = payload)

print r.text


The response I get is:

{"access_token":"*MYToken","token_type":"bearer","expires_in":-1}


Thanks!

Answer

Ah, someone had showed me.

had to do minor adjustments to the script:

r = requests.post("https://{Egnyte Domain}.egnyte.com/puboauth/token", params=payload)
token = r.json()['access_token']

users = requests.get(url, headers={'Authorization': 'Bearer %s' % token})