I am trying the get data from Salesforce using simple_salesforce python package. But I am getting Response content: [{'message': 'INVALID_HEADER_TYPE', 'errorCode': 'INVALID_AUTH_HEADER'}] when I execute any query or salesforce.Account.describe()

Below is the code, I wrote. Kindly suggest what I am doing wrong. I passed my access token as session id, how can I pass it as authorization:bearer {token}? If that is the issue.

from simple_salesforce import Salesforce
import redis
import configparser
import pandas as pd
import json

config = configparser.ConfigParser()
configSFUrl = config.get("sf.server.com","SF_BASE_URLS")

def sf_get_acctoken():
    r_client = redis.Redis(
        host='xxxxxxxxx',  //my redis host name
        password='yyyyyyyy' // my redis password
    token_Val = r_client.get('zzzzzzzzzz')
    print("Access Token value from redis: {}" .format(token_Val))  // I get my access token printed here
    return token_Val

sf_token = sf_get_acctoken()
sf = Salesforce(instance=configSFUrl, session_id='.format(sf_token)')

desc = sf.Account.describe()  

sf_response = sf.query_all("SELECT ActivityDate, StartDateTime, Appointment_Status__c, Event_Type__c FROM Event WHERE StartDateTime = 'TODAY' AND Appointment_Status__c = 'Completed'")
print("JSON Output::" + json.loads(sf_response))

sf_df = pd.DataFrame(sf_response['records']).drop(columns='attributes')


This is incorrect:

sf = Salesforce(instance=configSFUrl, session_id='.format(sf_token)')

You are sending the literal string .format(sf_token). Just pass sf_token itself directly as the session_id parameter.

In Python 3, if you want to convert bytes into a string, you have to decode them:


where the parameter is the desired encoding, such as ascii or utf-8. Presumably an access token is ASCII.

  • The reason I am doing is my sf_token is in JWT (bytes). It need to be string format. If I changed it to as mentioned then I am getting this error: Traceback (most recent call last): File "c:/Users/sdatta/Documents/Workspace/ProcessJson/salesforceIntegration.py", line 27, in <module> sf = Salesforce(instance=configSFUrl, session_id=sf_token) File "C:\Users\sdatta\Documents\Workspace\.venv\lib\site-packages\simple_salesforce\api.py", line 182, in init 'Authorization': 'Bearer ' + self.session_id, TypeError: can only concatenate str (not "bytes") to str
    – sudeep
    May 29 '20 at 19:06
  • 1
    @sudeep JWT isn't an access token. You have to exchange it for the access token or extract access token from it, depending on how you acquired the JWT. Example from David Reed: gist.github.com/davidmreed/5f4b7b8436487f9da9f682ca97f12cd9
    – identigral
    May 29 '20 at 19:25
  • @identigral I have my API token cached and I am accessing the latest token using redis which is actually storing the login token key. That key when passed to session_id, I am getting the concatenate error.
    – sudeep
    May 29 '20 at 19:46
  • You said in your first comment that what you were getting was a JWT. A JWT and an access token are not the same thing. Which do you have?
    – David Reed
    May 29 '20 at 19:47
  • More generally, in Python (3), you must decode bytes into a string before you perform string operations upon it. But that's a symptom, not the real problem. You need to get clarity on how you're supposed to be authenticating here.
    – David Reed
    May 29 '20 at 19:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.