无法为 Twitter 帐户活动 API [python3] 注册 webhook [英] Can't register webhook for Twitter Account Activity API [python3]

查看:39
本文介绍了无法为 Twitter 帐户活动 API [python3] 注册 webhook的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用帐户活动 API 设置 Twitter 应用程序,以替换我使用用户流式传输端点的旧设置.我希望能够实时获取发送到特定 URL 的用户的 DM 消息.

I'm trying to set up a Twitter app using the Account Activity API, to replace my old set up which used the user streaming endpoint. I want to be able to get DM messages to one user sent to a particular URL in real time.

遵循 这些迁移说明,我在我的网站上设置了一个 webhook 端点,如此处所述.我已经检查过该过程是否有效,确保当我在浏览器中打开 https://example.com/webhook_endpoint?crc_token=foo 时,我会收到一个令牌作为响应.

Following these migration instructions, I've set up a webhook endpoint on my site, as described here. I've checked that process is working, by making sure that when I open https://example.com/webhook_endpoint?crc_token=foo in my browser, I get a token in response.

现在我正在尝试注册我的网络钩子,但失败了.我正在使用以下代码,并收到 403 响应.

Now I'm trying and failing to register my webhook. I'm using the following code, and getting a 403 response.

from requests_oauthlib import OAuth1Session
import urllib

CONSUMER_KEY = 'my consumer key'
CONSUMER_SECRET = 'my consumer secret'
ACCESS_TOKEN = 'my access token'
ACCESS_SECRET = 'my access secret'

twitter = OAuth1Session(CONSUMER_KEY,
                        client_secret=CONSUMER_SECRET,
                        resource_owner_key=ACCESS_TOKEN,
                        resource_owner_secret=ACCESS_SECRET)
webhook_endpoint = urllib.parse.quote_plus('https://example.com/webhook/')
url = 'https://api.twitter.com/1.1/account_activity/all/env-beta/'
    'webhooks.json?url={}'.format(webhook_endpoint)
r = twitter.post(url)

403响应内容:{"errors":[{"code":200,"message":"Forbidden."}]}

我可以使用相同的会话对象成功发布状态并且

I can successfully post a status using the same session object and

r = twitter.post('https://api.twitter.com/1.1/statuses/update.json?status=Test')

我在这里做错了什么?

推荐答案

这是由于以下原因的组合:

This turned out to be due to a combination of:

  1. 未在此处创建环境:https://developer.twitter.com/en/帐户/环境,如下所述:https://developer.twitter.com/en/docs/accounts-and-users/subscribe-account-activity/guides/getting-started-with-webhooks立>
  2. 在创建在/webhook 端点返回的令牌的函数中使用了错误的使用者密钥

这篇关于无法为 Twitter 帐户活动 API [python3] 注册 webhook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆