Invalid token error while trying to access an API using python - 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 '}

Related

Remove ' from header in 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)

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}

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.

Transform Curl Oauth2 to Python Script

Below bash script works and returns a token.
curl -X POST --user <id>:<key> 'https://<name>.auth.eu-west-1.amazoncognito.com/oauth2/token?grant_type=client_credentials' -H 'Content-Type: application/x-www-form-urlencoded'
I would now like to generate a token via a Python script. I currently struggle with using requests library, but without success. Below generates Response 400 (bad request).
import requests
parameters = {"user":"<id>:<key>", "Content-Type": "application/x-www-form-urlencoded"}
response = requests.get("https://<name>.auth.eu-west-1.amazoncognito.com/oauth2/token?grant_type=client_credentials", params=parameters)
print(response)
To fix your usage of requests:
Use post() instead of get().
Use an auth object to construct a basic auth header. (Or send credentials in the body)
Remove the content type header; requests will set this for you.
Take the grant_type out of the query string. This belongs in the request body.
While the documentation for the /oauth2/token endpoint says you need to send a basic auth header for client_credentials grant, it also works if you send the client id and secret in the request body (with no auth header).
As such, these python examples both work, and are essentially equivalent:
Credentials in request body:
import requests
url = 'https://<COGNITO_DOMAIN>.auth.<REGION>.amazoncognito.com/oauth2/token'
params = {
"client_secret": "1ixxxxxxxxxxxxxxxxc2b",
"grant_type": "client_credentials",
"client_id": "7xxxxxxxxxxxxxxxt"
}
response = requests.post(url, data=params)
print(response)
print(response.text)
Credentials in basic auth header:
import requests
url = 'https://<COGNITO_DOMAIN>.auth.<REGION>.amazoncognito.com/oauth2/token'
params = {
"grant_type": "client_credentials"
}
auth = ('7xxxxxxxxxxt', '1ixxxxxxxxxxxxxxxc2b')
r = requests.post(url, data=params, auth=auth)
print(r)
print(r.text)

python get request bearer auth token + access key

I have tried access the data using post man with bearer token and access key please see screenshot. now i wanted to know How to add accesskey in header on a request in pyhon i have already added the bearer token.
code
headers = {"Authorization": "Bearer sdfjkdsjfs088gftdd'
test = requests.get('http://blah.blah/api/1.0/blah', headers=headers)
You can add additional items to the header by just adding additional key-value pairs to your headers dictionary:
headers = {
"Authorization": "Bearer sdfjkdsjfs088gftdd",
"accesskey": accesskey, # provided accesskey is defined somewhere
}
and then you can make the same request as before:
test = requests.get(url='http://blah.blah/api/1.0/blah', headers=headers)

Categories