salesforce - Salesforce cometD: 401::Request requires authentication
问题描述
I have to subscribe to cometD Salesforce channel and hence building cometD client in python. I am using the below python library.
https://github.com/dkmadigan/python-bayeux-client
And below is the handshake response I am getting
{'Host': ['xxxxx.my.salesforce.com/cometd/42.0/'], 'Content-Type': ['application/x-www-form-urlencoded'], 'Authorization': ['admin@123Pi6s9Y2QVergfergregpqqY']} message={"channel":"/meta/handshake","id":"1",
"supportedConnectionTypes":["callback-polling", "long-polling"],
"version":"1.0","minimumVersion":"1.0"} Headers({'host': ['xxxxx.my.salesforce.com/cometd/42.0/'], 'content-type': ['application/x-www-form-urlencoded'], 'authorization': ['admin@123Pi6s9Y2QVergfergregpqqY']}) {u'successful': False, u'advice': {u'reconnect': u'none'}, u'ext': {u'replay': True, u'sfdc': {u'failureReason': u'401::Request requires authentication'}, u'payload.format': True}, u'error': u'403::Handshake denied', u'id': u'1', u'channel': u'/meta/handshake'}
And I am getting 401::Request requires authentication.
In the Authorization key, I have concatenated password and Access token i.e. admin@123Pi6s9Y2QVergfergregpqqY where admin@123 is the password I use to login to Salesforce.
I have been banging my head since 2 days but not able to figure out why handshake is failing. Any suggestions?
解决方案
我认为授权密钥不正确。预期的不是您的密码,而是您在登录 Salesforce 后收到的 OAuth 访问令牌或会话 ID。查看不同的OAuth 流程,如果您正在测试,您可以使用用户名密码流程。
您可以在需要时使用以下方法获取会话 ID
import requests
import json
LOGIN_INSTANCE_URL = 'https://test.salesforce.com/services/oauth2/token'
LOGIN_USER_NAME = 'username_here'
CLIENT_ID = 'connected app consumer key'
CLIENT_SECRET = 'connected app consumer secret'
PASSWORD = 'password token'
def connect(authUrl, clientId, secret, username, password):
headers = {
}
postBody = {
'grant_type': 'password',
'client_id': clientId,
'client_secret':secret,
'username': username,
'password': password
}
try:
response = requests.post(authUrl, data = postBody, headers = headers)
#response.raise_for_status()
if (response.status_code == 200):
authResponse = response.json()
return authResponse['access_token']
else: #if not 200 see what the problem was
print response.text
except requests.exceptions.RequestException as e:
print e
print(connect(LOGIN_INSTANCE_URL, CLIENT_ID, CLIENT_SECRET, LOGIN_USER_NAME, PASSWORD))
这只是应该工作的示例代码,但您需要先创建一个连接的应用程序。对于没有用户干预的独立应用程序,JWT 流程更好。
推荐阅读
- python-3.x - 循环遍历目录下的excel文件路径并将它们传递给Python中的数据操作函数
- android - 如何从 kotlin 源文件中获取优化的 java 代码?
- flutter - 按字母顺序对文件进行排序
- python - 为什么 Python 函数会在调用之间“记住”可选参数的值
- hibernate - 如何在使用spring数据和jpa hibernate保存具有一对多关系的实体时避免唯一键违规?
- web-scraping - Splinter- Web Scraping href by find_partial_text
- reactjs - mobx 动作功能不改变存储值
- .htaccess - 如何使用 htaccess 从 URL 中删除特定文件夹名称
- extract - Splunk:如何使用正则表达式直接在搜索命令中提取字段?
- python - 如何将点连接到最近的多边形边界