I am trying to generate a oauth access token for experian's sandbox API (they give information on credit information).
Their tutorial Says to run this (fake data) to get an access token:
curl -X POST
-d '{ "username":"youremail#email.com", "password":"YOURPASSWORD"}'
-H "Client_id: 3QC11Sm45ti8wEG0d9A5hma5XIlGG7U9"
-H "Client_secret: ipu3WQDqTEjqZDXW"
-H "Content-Type: application/json"
"https://sandbox-us-api.experian.com/oauth2/v1/token"
How would I run this in python? I tried this among a lot of other things:
data = { "username" : "youremail#email.com", "password":"YOURPASSWORD"}
headers = {"Client_id": "3QC11Sm45ti8wEG0d9A5hma5XIlGG7U9", "Client_secret": "ipu3WQDqTEjqZDXW", "Content-Type": "application/json"}
response = requests.post("https://sandbox-us-
api.experian.com/oauth2/v1/token", data=data, headers=headers)
Any help would be greatly appreciated
Almost there, just need to parse the response:
import json
data = { "username" : "youremail#email.com", "password":"YOURPASSWORD"}
headers = {"Client_id": "3QC11Sm45ti8wEG0d9A5hma5XIlGG7U9", "Client_secret": "ipu3WQDqTEjqZDXW", "Content-Type": "application/json"}
response = requests.post("https://sandbox-us-api.experian.com/oauth2/v1/token", data=data, headers=headers)
if response.status_code in [200]:
tok_dict = json.loads(response.text)
print(tok_dict)
issued_at = tok_dict["issued_at"]
expires_in = tok_dict["expires_in"]
token_type = tok_dict["token_type"]
access_token = tok_dict["access_token"]
else:
print(response.text)
Related
The API instructions that I'm following state I should do the following:
# Log in with a valid username and password to receive an authorization code.
curl -X POST "https://api.sensorpush.com/api/v1/oauth/authorize" -H "accept: application/json" -H "Content-Type: application/json" -d #- <<BODY
{ "email": "me#email.com", "password": "abc123" }
BODY
I'm not very familiar with curl requests. After doing some research, I rewrote this in Python as:
import requests
from requests.structures import CaseInsensitiveDict
url = "https://api.sensorpush.com/api/v1/oauth/authorize"
headers = CaseInsensitiveDict()
headers["Content-Type"] = "applications/json"
data = "#- <<BODY { 'email': 'me#email.com', 'password': 'abc123' } BODY"
resp = requests.post(url, headers=headers, data=data)
print(resp.status_code)
I expected to receive an authorization code, but all it returns is "412" (with the correct email and password inserted). This error code tells me access was denied, so I'm wondering what was incorrect about my Python code?
Can you try this and see if it works?
import requests
import json
url = "https://api.sensorpush.com/api/v1/oauth/authorize"
headers = {'Content-Type':'application/json', 'accept':'application/json'}
data = { "email": "me#email.com", "password": "abc123" }
data = json.dumps(data)
r = requests.post(url, data=data, headers=headers)
print(r.json())
I am very new to python and API Integration. Can anyone pls tell me how do I run this okta document using python?
curl -v -X GET
-H "Accept: application/json"
-H "Content-Type: application/json"
-H "Authorization: SSWS ${api_token}"
"https://${yourOktaDomain}/api/v1/apps"
You can use urllib for do it.
Example:
from urllib.request import Request, urlopen
api_token = "..."
yourOktaDomain = "..."
headers = {
"Accept": "application/json",
"Content-Type": "application/json",
"Autorization": "SSWS " + api_token
}
request = Request("https://" + yourOktaDomain + "/api/v1/apps", headers=headers)
response = urlopen(request)
data = response.read()
import requests
import json
yourOktaDomain = 'something.okta.com'
api_token= ''
payload = ""
headers = {
'Accept': "application/json",
'Content-Type': "application/json",
'Authorization': "SSWS "+api_token
}
response1=requests.request("GET", 'https://'+{yourOktaDomain}+'/api/v1/apps', data=payload, headers=headers)
response1.json()
So I've been trying to figure this out all day but haven't been able to make any progress. I have this curl command, which works:
curl -X POST -H "x-hermes-key: <KEY>" -H "Accept: application/json" --form file='#example_files/ex1.pdf' <URL> -kv
When I try to run its equivalent in python with requests, I get a 422 error:
header = {
"Accept": "application/json",
"X-Hermes-Key": <KEY>
}
f = {'file': open("example_files/ex1.pdf", "rb")}
r_create = requests.post(url=<URL>, headers=header, files=f)
Can anyone help me see where I'm making a mistake?
The following should match the curl command and POST with content-type=multipart/form-data.
You can set the filename, content_type, and headers explicitly in a tuple for the 'file' value.
headers = {
"Accept": "application/json",
"X-Hermes-Key": <KEY>
}
url = <URL>
files = [('file', ('ex1.pdf', open('example_files/ex1.pdf', 'rb'),
'application/octet-stream'))]
r = requests.post(url=url, headers=headers, files=files)
print(r.status_code)
I have this curl request I would like to convert to python 3
curl -X "POST" "https://conversations.messagebird.com/v1/send" \\
-H "Authorization: AccessKey YOUR-API-KEY" \\
-H "Content-Type: application/json" \\
--data '{ "to":"+31XXXXXXXXX", "from":"WHATSAPP-CHANNEL-ID", "type":"text", "content":{ "text":"Hello!" }, "reportUrl":"https://example.com/reports" }'
can anyone help me please?
I've tried the following request, but not working :
import requests
header = {"Authorization":"AccessKey YOUR-API-KEY"}
data = { "to":"+31XXXXXXXXX", "from":"WHATSAPP-CHANNEL-ID", "type":"text", "content":{"text":"Hello!" }, "reportUrl":"https://example.com/reports"}
url = 'https://conversations.messagebird.com/v1/send'
response = requests.post(url, data=data, headers=header)
print(response.text)
I'm having the error message :
<Response [400]>
{"errors":[{"code":21,"description":"JSON is not a valid format"}]}
you can use json instead of data,
requests.post('http://httpbin.org/post', json={"key": "value"})
you need dump data
requests.post(url, data=json.dumps(data), headers=headers)
thers is another question like yours.
According to their documentation, the cURL is correct and would be implemented as is in Python:
import requests
header = {
"Authorization": "AccessKey YOUR-API-KEY",
"Content-Type": "application/json"
}
data = {
"to": "+31XXXXXXXXX",
"from": "WHATSAPP-CHANNEL-ID",
"type": "text",
"content": {"text":"Hello!"},
"reportUrl": "https://example.com/reports"
}
url = 'https://conversations.messagebird.com/v1/send'
response = requests.post(url, json=data, headers=header)
print(response.json())
I need to write Python equivalent code for below mentioned working curl(I have replaced the credentials for obvious reason, but it gives back 200 status.).
curl -X POST \
'https://api.lever.co/v1/candidates?dedupe=true&perform_as=user_123' \
-H 'Authorization: Basic token_123' \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: multipart/form-data' \
-H 'Postman-Token: 58cafa90-7ae4-47db-a144-4e9d430ffc94' \
-H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
-F 'files[]=#/Users/gaurav/lever_resume.pdf' \
-F 'emails[]=a#b.com'
So, I ended up in writing this snippet.
user_email = 'user#domain.com'
admin_id = '20f3975a-543f-4ca8-b215-2f851232a0ad'
client_id = '893728937298'
client_secret = '32032'
file_path = '/Users/ttn/Desktop/a.txt'
file_name = 'a.txt'
logging.basicConfig(level=logging.DEBUG)
url = "https://api.lever.co/v1/candidates"
files = {
'files[]': (file_name, open(file_path,'rb')),
}
auth = HTTPBasicAuth(client_id, client_secret)
querystring = {
"perform_as": admin_id,
"dedupe": 'true'
}
payload = {
'emails[]': user_email
}
headers = {
'Content-Type': "multipart/form-data",
"Cache-Control": "no-cache",
"content-type": "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
}
response = requests.post(url,
headers=headers,
params=querystring,
data=payload,
auth=auth,
files=files)
req = response.request
# print(curlify.to_curl(req))
print('\n==== Headers', req.headers)
print('\n==== Body', req.body)
print('\n==== form-data', str(req))
print(response.text)
Question
Since Python version of Curl is not working(giving 502 error instead of 200), so How can I compare the two? Can I generate the Curl out of Python's request`?
Can someone spot mistake in my Python version? I am suspecting some problem at form-data being passed (to collect evidence, I need answer to above question)
Edit
There seems to be a curlify package. But It looks like it does not support distinction between -d and -F parameters.
Try this:
import requests
headers = {
'Authorization': 'Basic token_123',
'Cache-Control': 'no-cache',
'Content-Type': 'multipart/form-data',
'Postman-Token': '58cafa90-7ae4-47db-a144-4e9d430ffc94',
'content-type': 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW',
}
params = (
('dedupe', 'true'),
('perform_as', 'user_123'),
)
files = {
'files[]': ('/Users/gaurav/lever_resume.pdf', open('/Users/gaurav/lever_resume.pdf', 'rb')),
'emails[]': (None, 'a#b.com'),
}
response = requests.post('https://api.lever.co/v1/candidates', headers=headers, params=params, files=files)
#NB. Original query string below. It seems impossible to parse and
#reproduce query strings 100% accurately so the one below is given
#in case the reproduced version is not "correct".
# response = requests.post('https://api.lever.co/v1/candidates?dedupe=true&perform_as=user_123', headers=headers, files=files)oduced version is not "correct".
# response = requests.post('https://api.lever.co/v1/candidates?dedupe=true&perform_as=user_123', headers=headers, files=files)
Reference: https://curl.trillworks.com/#python