Search code examples
pythonpython-2.7apicurlpycurl

How can I process this API request to Positionly using Python and not Curl?


The API resource for Positionly is here:

I was interested in learning how to re-write Curl into Python requests, or use something like PyCurl to make things easier.

This is the Curl code (I beleive, correct me if I am wrong):

curl -X POST -d
'grant_type=password&username=USER_EMAIL&password=YOUR_PASSWORD&client_id=de3be2752e8ae27a11cd96a6b0999b0f&client_secret=8d87664221c09681c3d3bc283a50bf73'
'https://auth.positionly.com/oauth2/token'

How would this be re-written using Python requests or PyCurl?

Note: the response should be printed and potentially later saved to a file or something similar. It's no good authenticating or processing a request and then not knowing what the response was!

I am using Python 2.7.


Solution

  • Using requests:

    import requests
    r = requests.post('https://auth.positionly.com/oauth2/token', data = {'grant_type':'password', 'username':'USER_EMAIL', 'password':'YOUR_PASSWORD', 'client_id':'de3be2752e8ae27a11cd96a6b0999b0f', 'client_secret':'8d87664221c09681c3d3bc283a50bf73'})
    print(r.text)