0

I have a curl command. I want to execute in in Python and fetch the response to pass it through other code.

curl https://api.box.com/oauth2/token -d 'grant_type=refresh_token' -d 'refresh_token=Ew38UXVKS3kc0axFt6MdDklUnoHpipxTzDWBmKlXAG9ImvGafbbaUhQndv89e677' -d 'client_id=3qkm5h4wh765b3khiws0z8hdpkc56jhs' -d 'client_secret=h9AeXzZL3KATJuaHmimFFBRBDZQrp9tr' -X POST

How can I execute the script in Python and get the response and pass it through other code?

When I am executing the curl script in CMD, I am getting this response:

{"access_token":"uz843jpIiEWnu0CcuT9as2XbA3UEQTR67","expires_in":4261,"restricted_to":[],"refresh_token":"GsDaP6VyUpHN8vDHbz9ktAjLfMLN0dFL6PMIK4fmDH8eKRqR360vDhQTBhIMZxy67","token_type":"bearer"}

From the above response I need to take the access_token value.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Debasis
  • 83
  • 9

2 Answers2

0

you should use requests library (pip install requests)

import requests
url = 'https://api.box.com/oauth2/token'
data = {
    'grant_type':'refresh_token',
    'refresh_token':'***',
    'client_id':'***',
    'client_secret':'***'
}
response = requests.post(url, data).json()
print(response)
avloss
  • 2,389
  • 2
  • 22
  • 26
  • Thanks for the updates. But I am seeing below error while trying to execute the script. – Debasis May 27 '20 at 17:25
  • "Response 200" usually means success, I'm not sure how you're getting that error. – avloss May 27 '20 at 17:45
  • I am assuming it as an error as i am unable to see the response.can you please let me know how can i see the response as below.{"access_token":"uz843jpIiEWnu0CcuT9as2XbA3UEQTR67","expires_in":4261,"restricted_to":[],"refresh_token":"GsDaP6VyUpHN8vDHbz9ktAjLfMLN0dFL6PMIK4fmDH8eKRqR360vDhQTBhIMZxy67","token_type":"bearer"} – Debasis May 27 '20 at 17:47
0

Like avloss said - try out requests.

Another great resource is the application Postman

It will let you try out any http calls you'd like, and then can also translate that into/out of curl and/or python requests code for you (and a bunch of other languages).

I find it really useful when trying to figure how to use requests for anything more than simple http calls.

Richard
  • 3,024
  • 2
  • 17
  • 40
  • Hi Richard, I am not getting the response after i executed the command as above. – Debasis May 27 '20 at 17:45
  • which code? avloss' python? what I'd suggest is first try getting you query working in Postman. Then after it's working there use Postman to generate the equivalent python requests call. – Richard May 27 '20 at 20:39