1

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!

Grokify
  • 15,092
  • 6
  • 60
  • 81
popopanda
  • 371
  • 5
  • 18
  • When using `requests` save yourself the headaches and use [`requests-oauthlib`](https://requests-oauthlib.readthedocs.org/en/latest/) too. It'll provide yuo with a `requests` session object that'll add the token automatically once authenticated. – Martijn Pieters Aug 13 '14 at 18:18

2 Answers2

1

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})
popopanda
  • 371
  • 5
  • 18
0

You need to use the token in the authorization header of requests. The best thing would be to create a persistent connection.

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

session = requests.Session()
session.headers['Authorization'] = "Bearer %s" % access_token
users = session.get('https://{Egnyte Domain}.egnyte.com/pubapi/v2/users')

OR

headers = {"Authorization":"Bearer %s" % access_token}
requests.get('https://{Egnyte Domain}.egnyte.com/pubapi/v2/users', headers=headers)
user6037143
  • 516
  • 5
  • 20