Remove ' from header in python - python

I'm trying to work with an API that requires Authorization headers, but it doesn't interpret ' at my api key.
So if i send
headers = {"Authorization": "api_key"}
res=request.post("https://endpoint.com/Login/authenticate", headers=headers)
the page throws a 500 because python sends the headers like
'Authorization': 'api_key'
While in Curl i don't have this error because it sends
Authorization: api_key
Is there a way to make python not add the ' at the headers?
I've tried to send the headers as only one string but python doesn't accept it.
Example:
headers = "Authorization: api_key"
res=request.post("https://endpoint.com/Login/authenticate", headers=headers)

Related

Unable to post authorization header in python requests

I've been trying to post a payload to an endpoint, the endpoint requires bearer token.
using the python requests, I'm trying to post the bearer token in the header as
from requests import Session
http= Session()
accessToken = getToken(url=uri,data=payload)
authorization_header_string = 'bearer ' + accessToken
requestHeaders = {"Content-type": "text/plain", "Authorization":authorization_header_string}
headerData={"content-type":"text/plain","authorization":requestHeaders}
resp= http.post(url=uri,data=payload,headers=headerData)
but all I get is 401 error, while if I use the same token in postman it works fine.
I'm using requestbin to check wherein the authorization shows up only sometimes in the header.
Try the following:
token = "your token"
headerData={"content-type": "text/plain", "authorization": "Bearer " + token}

Invalid token error while trying to access an API using python

I am trying to access an API which is :
curl "api_endpoint_here"
-H "Authorization: Token token=YOURAPIKEY"
I am using Python and tried this:
response = requests.get('https://app.resemble.ai/api/v1/projects', headers= {'Authorization': 'Token' , 'token' :'my_API_KEY'})
I have used the actual API key in place of my_API_KEY.
I am getting an error saying 'Unauthorized -- Your API key is wrong'
the key is correct.
This helped:
headers = {'Authorization': 'Token my_API_KEY '}

Squarespace returns no data using both curl and python

I am currently trying to build an integration between Quickbooks POS and squarespace. The official api documentation gives the following example:
curl "https://api.squarespace.com/1.0/commerce/products?cursor=abc" \
-i \
-H "Authorization: Bearer YOUR_API_KEY_OR_OAUTH_TOKEN" \
-H "User-Agent: YOUR_CUSTOM_APP_DESCRIPTION"
which returns with error 52 (no data) from. My attempts to do this with python also returns no data.
import requests
print("start")
headers = {
"Authorization": "API KEY",
"User-Agent": "QBPOS integration"
}
square_api = requests.Session()
data = square_api.get('https://api.squarespace.com/1.0/commerce/products', headers=headers)
print(data)
print(data.text)
and python-squarespace returns an error stating that squarespace thinks this request is bogus.
All of the response codes are <403>.
What could cause it to this, how can I fix this?
Add Bearer before the API KEY!!
import requests
headers = {
'Authorization': 'Bearer INSERT YOUR API KEY',
'User-Agent': 'QBOS INTEGERATION',
'Content-Type': 'application/json',
}
print("Hello World")
response = requests.get('https://api.squarespace.com/1.0/authorization/website', headers=headers)
print(response.text)
The above code is to check your squarespace api key is valid or not. In Authorization you forgot the Bearer, and replace API KEY with your api key which square space provided. I guess that QUBOS integration is the key name of the square space API key.
Squarespace uses some confusing terminology for it's apis. I had not enabled the api I needed.

How to send file using Request Python with some headers?

I tried to send file with some headers like:
files = {'file': (file, open(file, 'rb'), {'Content-type': 'multipart/form-data; boundary=---BOUNDARY', 'Authorization' : 'Basic ' + api_key})}
r = requests.post(base_url, files=files)
Server returned 401 error that means absent header Authorization. But I sent it
For Basic Authentication you can follow the requests docs. It's visible on the very first line of code in the example on that page.
Use the auth keyword argument to supply a 2-tuple of username and password:
response = requests.post(base_url, files=files, auth=('username', 'password'))
Edit:
If you want to send actual headers, rather than things like Basic Auth, you can do that with the headers keyword argument. This allows you to give a dict of headers you would like to send. For example:
headers = {'Content-Type': 'application/json'}\
response = requests.post(url, data=data, headers=headers)
The auth argument, should be a simplification of the above because Basic Auth is so common, but don't quote me on that one.
You can follow this example to take it from the official documentation I think you should try it first in postman, with the json then and do it from python.
This information is taken from the official documentation
python

Python Requests API call not working

I'm having an issue converting a working cURL call to an internal API to a python requests call.
Here's the working cURL call:
curl -k -H 'Authorization:Token token=12345' 'https://server.domain.com/api?query=query'
I then attempted to convert that call into a working python requests script here:
#!/usr/bin/env python
import requests
url = 'https://server.domain.com/api?query=query'
headers = {'Authorization': 'Token token=12345'}
r = requests.get(url, headers=headers, verify=False)
print r
I get a HTTP 401 or 500 error depending on how I change the headers variable around. What I do not understand is how my python request is any different then the cURL request. They are both being run from the same server, as the same user.
Any help would be appreciated
Hard to say without knowing your api, but you may have a redirect that curl is honoring that requests is not (or at least isn't send the headers on redirect).
Try using a session object to ensure all requests (and redirects) have your header.
#!/usr/bin/env python
import requests
url = 'https://server.domain.com/api?query=query'
headers = {'Authorization': 'Token token=12345'}
#start a session
s = requests.Session()
#add headers to session
s.headers.update(headers)
#use session to perform a GET request.
r = s.get(url)
print r
I figured it out, it turns out I had to specify the "accept" header value, the working script looks like this:
#!/usr/bin/env python
import requests
url = 'https://server.domain.com/api?query=query'
headers = {'Accept': 'application/app.app.v2+json', 'Authorization': 'Token token=12345'}
r = requests.get(url, headers=headers, verify=False)
print r.json()

Categories