我必须订阅cometD Salesforce频道,因此在python中构建cometD客户端。我正在使用下面的python库。
https://github.com/dkmadigan/python-bayeux-client
以下是我得到的握手回复
{'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'}
我得到401 ::请求需要身份验证。
在授权密钥中,我连接了密码和访问令牌,即admin @ 123Pi6s9Y2QVergfergregpqqY,其中admin @ 123是我用来登录Salesforce的密码。
我从2天开始就一直在敲打我的脑袋,但却无法弄清楚为什么握手会失败。有什么建议?
答案 0 :(得分:2)
我认为授权密钥不正确。它不是您预期的密码,而是您登录salesforce后收到的OAuth访问令牌或会话ID。查看不同的OAuth flows,如果您正在测试,可以使用用户名密码流。
以下方法可用于在需要时获取会话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))
这只是应该有效的示例代码,但您需要先创建connected app。对于没有用户干预的独立应用程序,JWT流程更好。