How to run cURL request using python - python

I would like to know how to run the following cURL request using python (I'm working in Jupyter notebook):
curl -i -X GET "https://graph.facebook.com/{graph-api-version}/oauth/access_token?
grant_type=fb_exchange_token&
client_id={app-id}&
client_secret={app-secret}&
fb_exchange_token={your-access-token}"
I've seen some similar questions and answers suggesting using "requests.get", but I am a complete python newbie and am not sure how to structure the syntax for whole request including the id, secret and token elements. Any help would be really appreciated.
Thanks!

no need to use curl. use the below
import requests
graph_api_version = 'a'
app_id = 'b'
app_secret = 'c'
your_access_token = 'd'
url = f"https://graph.facebook.com/{graph_api_version}/oauth/access_token?grant_type=fb_exchange_token&client_id={app_id}&client_secret={app_secret}&fb_exchange_token={your_access_token}"
r = requests.get(url)

Convert your Curl request to Python instantly here :
https://curl.trillworks.com/

you can use some lib that run commands in python like subprocess. for example: subprocess.run(["curl", "google.com"])

Related

Call API in python to run Jenkins Job - problem with authentication

I have got a jenkins job which i can run with making a post request:
curl -u albert405:{mytoken} http://172.31.32.33:8080/job/URL_Job_Trigger/build?token=ozSVoEQfLg
Could you tell me how to place this authentication (albert405:{mytoken}) into my python script:
import requests
url = 'http://172.31.32.33:8080/job/URL_Job_Trigger/build?token=ozSVoEQfLg'
x = requests.post(url)
print(x.text)
I have managed to solve it by this :
http://YOUR_JENKINS_USER_ID:YOUR_API_TOKEN#YOUR_JENKINS_URL/job/YOUR_JENKINS_JOB/build
import requests
build = requests.post("http://YOUR_JENKINS_USER_ID:YOUR_API_TOKEN#YOUR_JENKINS_URL/job/YOUR_JENKINS_JOB/build?token=TokenName")
Where is your authentication code? I see none of it.
Jenkins uses Basic Auth, which is indicated here https://wiki.jenkins.io/display/JENKINS/Remote+access+API.
In order to send auth parameters with requests it's a simple:
res =requests.post(url, auth=("albert405", "password"))
Which is the first documentation you get when googling basic auth requests: https://2.python-requests.org/en/master/user/authentication/

How pass option of curl in request in python?

As the answer and comment in https://stackoverflow.com/a/31764155/10189759
suggest that everything could be done using a curl could be done using python request.
My question is how to pass option like -u -i to the request function?
For example in this tutorial github api
curl -i -u your_username:your_token https://api.github.com/user
How could I use request to pass my args and option to the url?
You can do it like this:
import requests
response = requests.get('https://api.github.com/user', auth=('your_username', 'your_token'))

How to run delete webrequest using Python?

Currently, I use Powershell to clear cache using the following command:
Invoke-WebRequest https://url -method DELETE
How can I make my Python script do to the same type of delete web request?
This seemed to work:
import requests
url = "https://myurl"
response = requests.delete(url)

Run a curl tlsv1.2 http get request in python?

I have the following command that I run using curl in linux.
curl --tlsv1.2 --cert ~/aws-iot/certs/certificate.pem.crt --key ~/aws-iot/certs/private.pem.key --cacert ~/aws-iot/certs/root-CA.crt -X GET https://data.iot.us-east-1.amazonaws.com:8443/things/pi_3/shadow
This command returns JSON text that I want. However I want to be able to run the above command in Python3. I do not know what library to use in order to get the same JSON response.
P.S. I replace "data" with my account number in AWS to get JSON
After playing around with it on my own I was able to successfully do it in python using the requests library.
import requests
s = requests.Session()
r = s.get('https://data.iot.us-east-1.amazonaws.com:8443/things/pi_3/shadow',
cert=('/home/pi/aws-iot/certs/certificate.pem.crt', '/home/pi/aws-iot/certs/private.pem.key', '/home/pi/aws-iot/certs/root-CA.crt'))
print(r.text)

Sending data in a POST message to a RESTful web service

I need to send some JSON data in a POST message to a RESTful webservice.
Which python module should I be using for this? Is there some sample code I can refer to?
Which bit are you having trouble with? The JSON, or the POST?
For JSON, the json module has been included in Python since version 2.5. Just do json.dumps(my_data) to convert a data variable to JSON.
For the POST, there are various modules in the standard library, but the best bet is to install the third-party requests library.
Requests is probably the best library for the job. It certainly beats urllib and urllib2. You can get it and see an example at http://pypi.python.org/pypi/requests
or you can just install it with "pip install requests"
There's a few more examples using the Github API with both the requests library and others at https://github.com/issackelly/Consuming-Web-APIs-with-Python-Talk
here is what I've used for post and get requests
import httplib
connection = httplib.HTTPConnection('192.168.38.38:6543')
body_content = 'abcd123456xyz'
connection.request('POST', '/foo/bar/baa.html', body_content)
postResult = connection.getresponse()
connection.request('GET', '/foo/bar/baa.html')
response = connection.getresponse()
getResult = response.read()
It does same as this sequence of CLI commands:
curl -X POST -d "abcd123456xyz" 192.168.38.38:6543/foo/bar/baa.html
curl 192.168.38.38:6543/foo/bar/baa.html

Categories