Python 请求在控制台中有效,但在应用程序中无效
Python Requests Works in Console, but not In App
我构建了一个 Python 脚本,用于使用 python 请求库将数据上传到名为 Intercom 的应用程序。我们的想法是在自定义属性中插入一个全新的字段,其中包含我们需要为自动消息传递关闭的数据。
脚本很长,但是破解方法如下:
def post_user(self, input_user, session=None):
"""
:param user: a JSON object with all the user pieces needed to be posted to the endpoint
:param session: a Requests session to be used. If None we'll create one
"""
if not session:
session = self.create_requests_session()
else:
session = session
# JSON encode the user
payload = json.dumps(input_user, sort_keys=True, indent=2, separators=(',', ': '))
print(payload) # DEBUG CODE
# Post the data to endpoint
response = session.post(self.endpoint, data=payload)
# This will raise a status code on a bad status code
response.raise_for_status()
下面也是create_requests_session()
的方法:
def create_requests_session(self):
"""
Start a requests session. Set Auth & Headers so we don't have to send it with each request
:return: A new requests.Session object with auth & header values prepopulated.
"""
session = requests.Session()
session.auth = (self.app_id, self.api_key)
session.headers = {'Content-Type': 'application/json', 'accept': 'application/json'}
return session
当我 运行 整个脚本并单步执行第一个 input_user
时,它以字典的形式出现,看起来像。 (注意:数据是匿名的,但结构保持完全相同):
{'user_id': '123456',
'email': 'foo@bar.com',
'custom_attributes': {
'test_cell' : 'Variation'}
}
但是,在将用户传送到 json.dumps(user)
然后通过 POST 请求发送后,我收到了 404 错误。
这是奇怪的部分。我向这个完全相同的用户发送了完全相同的 JSON,除了我使用 Python 控制台创建了所有部分(注意,隐藏了一些行以保持用户数据匿名):
In[82]: sesh = requests.session()
In[86]: header
Out[86]: {'Content-Type': 'application/json', 'accept': 'application/json'}
In[87]: sesh.headers = header
In[89]: payload = json.dumps(update_user)
In[91]: response = sesh.post(endpoint, data=payload)
In[92]: response
Out[92]: <Response [200]>
在这一点上,我完全迷路了。我试图设置一个代理来尝试逐字节比较请求,但运气不佳。非常感谢任何帮助或见解。
您确定您的身份验证有效吗?
session.auth = (self.app_id, self.api_key)
特别是,app_id 应设置为与 API 键对应的键(因此,如果您使用的是测试应用,则测试应用的 app_id)。
如果 Intercom 收到它无法识别的应用程序 ID(当前)returns 404,这就是您所看到的。现在应该将其修复为 return a 401。
我构建了一个 Python 脚本,用于使用 python 请求库将数据上传到名为 Intercom 的应用程序。我们的想法是在自定义属性中插入一个全新的字段,其中包含我们需要为自动消息传递关闭的数据。
脚本很长,但是破解方法如下:
def post_user(self, input_user, session=None):
"""
:param user: a JSON object with all the user pieces needed to be posted to the endpoint
:param session: a Requests session to be used. If None we'll create one
"""
if not session:
session = self.create_requests_session()
else:
session = session
# JSON encode the user
payload = json.dumps(input_user, sort_keys=True, indent=2, separators=(',', ': '))
print(payload) # DEBUG CODE
# Post the data to endpoint
response = session.post(self.endpoint, data=payload)
# This will raise a status code on a bad status code
response.raise_for_status()
下面也是create_requests_session()
的方法:
def create_requests_session(self):
"""
Start a requests session. Set Auth & Headers so we don't have to send it with each request
:return: A new requests.Session object with auth & header values prepopulated.
"""
session = requests.Session()
session.auth = (self.app_id, self.api_key)
session.headers = {'Content-Type': 'application/json', 'accept': 'application/json'}
return session
当我 运行 整个脚本并单步执行第一个 input_user
时,它以字典的形式出现,看起来像。 (注意:数据是匿名的,但结构保持完全相同):
{'user_id': '123456',
'email': 'foo@bar.com',
'custom_attributes': {
'test_cell' : 'Variation'}
}
但是,在将用户传送到 json.dumps(user)
然后通过 POST 请求发送后,我收到了 404 错误。
这是奇怪的部分。我向这个完全相同的用户发送了完全相同的 JSON,除了我使用 Python 控制台创建了所有部分(注意,隐藏了一些行以保持用户数据匿名):
In[82]: sesh = requests.session()
In[86]: header
Out[86]: {'Content-Type': 'application/json', 'accept': 'application/json'}
In[87]: sesh.headers = header
In[89]: payload = json.dumps(update_user)
In[91]: response = sesh.post(endpoint, data=payload)
In[92]: response
Out[92]: <Response [200]>
在这一点上,我完全迷路了。我试图设置一个代理来尝试逐字节比较请求,但运气不佳。非常感谢任何帮助或见解。
您确定您的身份验证有效吗?
session.auth = (self.app_id, self.api_key)
特别是,app_id 应设置为与 API 键对应的键(因此,如果您使用的是测试应用,则测试应用的 app_id)。
如果 Intercom 收到它无法识别的应用程序 ID(当前)returns 404,这就是您所看到的。现在应该将其修复为 return a 401。