【问题标题】:Python Requests Works in Console, but not In AppPython 请求在控制台中有效,但在应用程序中无效
【发布时间】:2015-09-14 21:26:41
【问题描述】:

我构建了一个 Python 脚本,旨在使用 Python requests 库将数据上传到名为 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]>

在这一点上,我完全迷失了。我试图设置一个代理来尝试逐字节比较请求,但没有太多运气。非常感谢任何帮助或见解。

【问题讨论】:

  • 问题可能出在您的 self.endpoint 上,您确定两者相同
  • 直接从我的代码复制粘贴:self.endpoint = 'https://api.intercom.io/users'

标签: python http request python-3.4 intercom


【解决方案1】:

您是否确保您的身份验证有效?

session.auth = (self.app_id, self.api_key)

尤其应将 app_id 设置为与 API Key 对应的那个(如果您使用的是测试应用,则应设置为测试应用的 app_id)。

如果对讲机接收到一个应用程序 ID,它(当前)无法识别它会返回 404,这就是您所看到的。现在应该修复此问题以返回 401。

【讨论】:

  • 不幸的是,事实并非如此。我将这些值存储在环境中,它们是正确的。 Plus 身份验证失败会导致 401 状态代码。
  • @JamieOsler。我刚刚在调试中验证了这一点,会话将正确的app_idapi_key 传递给它的身份验证,并且标头是正确的。为什么这很奇怪,它们与我在命令行中传递的基本身份验证密钥相同吗?然而,它在我的脚本中不起作用,但它在 Python 控制台中完美运行。
  • 我确保为同一个应用程序创建一个全新的api_key,并确保会话身份验证标头在运行时正确。任何想法为什么我仍然未经授权?我收到 error_code "token_not_found" 完整的响应内容如下:b'{"type":"error.list","request_id":"f6ff25d1-771e-49d1-8c8b-77c360bf53ed","errors":[{"code":"token_not_found","message":"Unauthorized"}]}'
  • 对此进行了很晚的跟进,但我们最终发现配置文件在 app_id 之后有一个尾随空格,这导致了身份验证失败。我认为我们以更直接的沟通方式传递了这一点,但我想我会结束这个问题。干杯!
猜你喜欢
  • 1970-01-01
  • 2015-06-21
  • 1970-01-01
  • 2017-10-19
  • 2012-07-27
  • 1970-01-01
  • 2023-03-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多