I've have been accessing an supportpal API via curl just fine using the following command. (https://docs.supportpal.com/current/REST+API)
curl.exe -i -u 'APIKEY:x' -X GET https://support.url.org/api/user/user/3697
This correctly grabs the data. I've trying replicate this with python but i continually have issues with authentication and get the following error.
Failed to authenticate because of bad credentials or an invalid authorization header
The code i'm using is straight forward.
import requests
import json
url = "https://support.url.org/api/user/user/3697"
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer: {APIKEY:x}"
}
response = requests.request("GET", url, headers=headers)
print(response. Text)
I'm thinking i have an issue with the auth header, but can't figure it out.
from requests.auth import HTTPBasicAuth
import requests
url = 'https://support.url.org/api/user/user/3697'
headers = {'Accept': 'application/json'}
auth = HTTPBasicAuth('apikey', x)
req = requests.get(url, headers=headers, auth=auth)
Related
I need to create a post request, which is repeatedly failing as the auth headers are either being send in incorrect format or are missing/not matching signature, I need help on this as I am new to python and despite rigorous searching still cant find answer to this.
Below is the code which I got from postman for a get request but the same headers aren't working for post request, post request even doesn't work with postman either
import requests
url = "https://aaaaa-bbbb.xxxx.exampleapis.net/cps/v2/enrollments?enrollmentId=81053"
payload={}
files={}
headers = { 'Accept': 'application/vnd.aaaa.xxx.eeeee.v11+json', 'Authorization': 'EG1-HMAC-SHA256 c_token=cccc;access_token=aaaa;timestamp=20220526T14:06:11+0000;nonce=nnn123;signature=Abcde123=' }
response = requests.request("GET", url, headers=headers, data=payload, files=files)
print(response.text)
What I have tried so far:
from http import client
from multiprocessing.connection import Client
import requests
import json
from hostgrid import hostAuth
from urllib.parse import urljoin,urlencode
import time
import uuid
import hashlib
from hashlib import sha1
import hmac
s = requests.Session()
url = "https://aaaa-wwww.xxxx/ppp/v2/certs?contractId=A-12345"
s = requests.Session()
s.auth = hostAuth(
client_token= ' ctctct',
client_secret = 'cccc',
access_token=aaaaa'
)
payload = open('test.json')
data = json.load(payload)
timestamp = str(int(time.time()))
nonce = uuid.uuid4().hex
hmac.new(client_secret,f'{timestamp}{nonce}'.encode('ascii'),hashlib.sha256).hexdigest()
headers={
"Accept" : "application/vnd.aaaa.ccc.cert-status.v11+json",
'Content-Type' : "application/vnd.akamai.ccc.cert.v11+json",
‘Authorization’ : 'EG1-HMAC-SHA256 c_token=cccc;access_token=aaaa;timestamp=20220526T14:06:11+0000;nonce=nnn123;signature=Abcde123='
}
response = s.post(url,data,headers)
print(response.text)
I am trying to access the github API via requests with python (Answers in similar questions here and here do not help).
Using curl, I am able to get a list of recent commits for a project, e.g.
curl -H "Accept: application/vnd.github.inertia-preview+json Authorization: token a0d42faabef23ab5b5462394373fc133ca107890" https://api.github.com/repos/rsapkf/42/commit
Trying to use the same setup in python with requests I tried to use
url = "https://api.github.com/repos/rsapkf/42/commits"
headers = {"Accept": "application/vnd.github.inertia-preview+json", "Authorization": "token a0d42faabef23ab5b5462394373fc133ca107890"}
r = requests.get(url, headers=headers)
as well as
url = "https://api.github.com/repos/rsapkf/42/commits"
headers = {"Accept": "application/vnd.github.inertia-preview+json"}
my_username = "someuser"
my_token = "a0d42faabef23ab5b5462394373fc133ca107890"
r = requests.get(url, headers=headers, auth=(my_username, my_token))
but in both cases I get the response
{'documentation_url': 'https://docs.github.com/rest',
'message': 'Bad credentials'}
What am I missing here?
No Authentication is required. The following works:
url = "https://api.github.com/repos/rsapkf/42/commits"
headers = {"Accept": "application/vnd.github.inertia-preview+json"}
r = requests.get(url, headers=headers)
When using the shell, I can successfully create a new user by running
curl --user administrator:pasword "Content-Type: application/json" https://localhost:8080/midpoint/ws/rest/users -d #user.json
However when I try to do the same thing in python using requests, I get a 200 response and no user is created.
This is the script I am using:
import requests
headers = {
'Content-Type': 'application/json',
}
data = open('user.json')
response = requests.post('https://localhost:8080/midpoint/ws/rest/users', headers=headers, data=data, auth=('Administrator', 'password'))
print(response)
To me they look the same. What is different in the python request that is stopping the user from being created?
I compare the post date with curl and python requests.And i found the difference.
CURL: {"user" : "hero","pd":30}
Requests: pd=30&user=hero
Then this is my test.
import requests
import json
headers = {
'Content-Type': 'application/json',
}
with open('user.json') as j:
data = json.load(j)
response = requests.post('http://127.0.0.1:8080',
headers=headers,
json = data,
auth=('Administrator', 'password'))
print(response.headers)
I think using json = data would work as well, but I was finally successful using json dumps: data=json.dumps(data)
I've used postman for getting the access token from server and it's working fine. but when i copy the python code from postman and run that code in IDE it's showing some errors like max entries exceeded.
import requests
url = "https://web.com/oauth/token"
payload = "type=authorizatio&code=mycode&id=abcdefghi&client=kjsisjnsui675eaa9WqksiisP"
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
response = requests.request("POST", url, data=payload, headers=headers)
print(response.text)
I am trying to make API requests with proper authorization. I looked at Conversion of curl to python Requests but am having trouble putting the authorization headers.
This is what I have so far:
import base64
import http.client
import requests
import json
headers = {
"Content-Type": "application/json",
'Authorization': 'Basic %s' % base64.b64encode(b'username:password').decode('ascii'),
}
payload = {json.dumps({"one":["two:three:four"]})}
url = 'https://website/v1'
r = requests.post(url, data=payload, headers=headers)
if __name__=='__main__':
print (r.text)
The only difference between the link provided above and my code is that he has query={"tags":["test1","test2"]} I do not have that at all.
This is the curl I am trying to translate to get what I got above.
curl -X POST -u "username:password" -H "Content-Type:application/json" "https://website.com" -d '{"ids":["something:numberOne:numbertwo"]}'
Any help appreciated.