Traceback error while connecting with salesforce API: [{'message': 'INVALID_HEADER_TYPE', 'errorCode': 'INVALID_AUTH_HEADER'}]
What is the problem?
My python codes are as follows:
client_id = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
client_secret = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
redirect_url = 'http://localhost/'
cm_user = 'XXXXXXXXXXXXXXXXXXXXXX'
cm_pass = 'XXXXXXXXXXXXXXXXXXXXXX'
auth_url = 'https://login.salesforce.com/services/oauth2/token'
response = requests.post(auth_url, data = {
'client_id': client_id,
'client_secret': client_secret,
'grant_type':'password',
'username': cm_user,
'password': cm_pass
})
json_res = response.json()
access_token = json_res['access_token']
auth = {'Authorization': 'Bearer' + access_token}
instance_url = json_res['instance_url']
url = instance_url + '/services/data/v45.0/sobjects/contact/describe'
res = requests.get(url, headers=auth)
r = res.json()
print(r)
You are missing a space after the word Bearer, which renders your authorization header invalid.
Related
Trying to understand how it works oauth
How to get authorized user details using Blizzard api?
import json
import requests
client_id = ""
client_secret = ""
region = "eu"
data = {
'grant_type': 'client_credentials'
}
access_token_response = requests.post(f"https://{region}.battle.net/oauth/token", data=data, allow_redirects=False, auth=(client_id, client_secret))
access_token = json.loads(access_token_response.text)["access_token"]
api_call_headers = {
'Authorization': 'Bearer ' + access_token
}
api_call_response = requests.get(f"https://{region}.battle.net/oauth/userinfo", headers=api_call_headers)
access token is dynamically generated with each time and passed to the requests, its throwing invalid token error .
The access token is dynamically passed and bearer, i am not sure the bearer is correct format to send the token in header, Please correct the error
import requests
Access_URL = 'https://host1/uaa/oauth/token'
client_id='ReadUser1'
client_secret='clientsecret1'
grant_type='client_credentials'
BASE_URL='https://host2/hisrian-rest-api/v1/tags?nameMask=*&maxNumber=500'
response = requests.post(Access_URL,
auth=(client_id, client_secret),
data=
{'grant_type':grant_type,'client_id':client_id,'client_secret':client_secret,'content-type': 'application/x-www-form-urlencoded'})
json_response=response.json()
tokenvalue= (json_response['access_token'])
headers={'Content-Type':'application/json',
'Authorization': Bearer {}".format(tokenvalue)}
auth_response = requests.get(BASE_URL, headers=headers)
print(auth_response.json())
import requests
Access_URL = 'https://host1/uaa/oauth/token'
client_id = 'ReadUser1'
client_secret = 'clientsecret1'
grant_type = 'client_credentials'
BASE_URL = 'https://host2/hisrian-rest-api/v1/tags?
nameMask=*&maxNumber=100'
response = requests.post(Access_URL,
auth=(client_id, client_secret),
data={'grant_type': grant_type, 'client_id':
client_id, 'client_secret': client_secret, 'content-type':
'application/x-www-form-urlencoded'})
json_response = response.json()
tokenvalue = (json_response['access_token'])
headers = {'Authorization': 'Bearer ' +
tokenvalue, 'Content-Type': 'application/json'}
auth_response = requests.get(BASE_URL, headers=headers)
print(auth_response.json())
I am using the following code for request:
import requests
import print
import json
api_key = "XXX-XXX"
api_version = 'v3'
accountID = "XXX"
tradeID = "XX"
#Endpoint Parts
api_base_url = f"https://api-fxpractice.oanda.com/"
key_url = f"https://api-fxpractice.oanda.com/v3/accounts"
headers = {
'Authorization': f'bearer {api_key}',
'Content-Type': 'application/json'
}
data = {'units': 'ALL'}
endpoint_path = f"/{api_version}/accounts/{accountID}/trades/{tradeID}/close"
endpoint = f"{api_base_url}{endpoint_path}"
r = requests.put(endpoint, headers=headers, data=data)
print(r.status_code)
pprint.pprint(r.json())
Response I get:
400
{'errorMessage': "Invalid value specified for 'Authorization'"}
I am trying to convert successful python (flask) OAuth 2.0 authentication / api request into Postman.
My current process is:
From the front end, I hit an /auth endpoint in python using fetch:
fetch("/auth")
.then(function (response) {
return response.json();
})
.then(function (json) {
const code = json.code;
window.location = `[hostname.com]/auth/authorize?request_token=${code}&redirect_uri=http://[hostname]/menu`;
});
The backend flask auth endpoint looks like this:
url = 'https://[hostname].com/v3/oauth/request'
headers = CaseInsensitiveDict()
headers['Host'] = '[hostname].com'
headers['Content-Type'] = 'application/json'
headers['X-Accept'] = 'application/json'
data = json.dumps({'consumer_key': 'XXXX', 'redirect_uri':'[hostname]/success'})
resp = requests.post(url, headers=headers, data=data)
json_resp = json.loads(resp.content)
auth_code = json_resp['code']
auth_resp = {'code': auth_code}
return jsonify(auth_resp)
The access endpoint takes that auth code to get the token
cur_auth = session.get('auth_code',None)
url = 'https://[hostname.com]/v3/oauth/authorize'
headers = CaseInsensitiveDict()
headers['Host'] = '[hostname].com'
headers['Content-Type'] = 'application/json; charset=UTF-8'
headers['X-Accept'] = 'application/json'
data = json.dumps({'consumer_key': 'XXXX', 'code': cur_auth})
resp = requests.post(url, headers=headers, data=data)
json_resp = resp.json()
access_token = json_resp['access_token']
username = json_resp['username']
session['access_token']=access_token
session['username']=username
access_resp = {'access': access_token, 'user': username}
return jsonify(access_resp)
But when I try to translate this into a postman request, I can't really understand where some of the things like the consumer_key request_token and code get defined.
I'm currently getting a 400 bad request with this setup:
where the consumer_key is in Postman's client secret field, and where
https://[hostname].com/v3/oauth/request is in Postman's auth field and
https://getpocket.com/v3/oauth/authorize is in Postman's access token url field.
I’m trying to get DeviceCheck to work, where I keep getting this response from Apple’s server: 401 Unable to verify authorization token.
The device_token is being sent to my python server over a base64 encoded string in JSON payload. Any ideas what I might be doing wrong?
Here is my code example:
def device_check_query(device_token):
data = {
'device_token': device_token,
'transaction_id': str(uuid4()),
'timestamp': int(time.time() * 1000),
}
jw_token = get_jw_token()
headers = {'Authorization': 'Bearer ' + jw_token}
response = requests.post(QUERY_URL, json=data, headers=headers)
return response.content
def get_jw_token():
with open(KEY_FILE, 'r') as cert_file:
certificate = cert_file.read()
jw_token = jwt.encode(
{'iss': TEAM_ID}, certificate,
algorithm='ES256',
headers={'kid': KEY_ID})
return jw_token
you need to add in the payload the issuer key and iat then it will work, check my code below
import time
def device_check_query(device_token):
data = {
'device_token': device_token,
'transaction_id': str(uuid.uuid4()),
'timestamp': int(time.time() * 1000),
}
jw_token = get_jw_token()
headers = {'Authorization': 'Bearer ' + jw_token}
response = requests.post(url, json=data, headers=headers)
return response.content
def get_jw_token():
with open('myfile.p8', 'r') as cert_file:
certificate = cert_file.read()
jw_token = jwt.encode(
{'iss': issuer,'iat': int(time.time())}, certificate,
algorithm='ES256',
headers={'kid': keyid})
return jw_token
device_check_query(u"1234e323a22133....")