创建Twitter App并进行设置

创建Twitter App

https://developer.x.com/en/portal/

设置Read 和 Write权限

创建Twitter App之后默认只有Read权限,因此还要把 Write 权限也开通

设置回调地址

用户授权之后会跳转这个URL,并带上oauth_tokenoauth_verifier

  • 授权之后的回调示例:

获取Keys

  • 生成 Consumer keys , 将获得:

    • API Key

    • API Key Secret

  • 设置 Access Token and Secret,将会获得

    • Access Token

    • Access Token Secret

  • Bearer Token 暂时没有用到,暂不设置

进行测试

  • 官方文档: https://developer.x.com/en/docs/tutorials/how-to-create-a-twitter-bot-with-twitter-api-v2

  • Twitter OAuth1.0a Postman测试:

    注意: 在 Step 3-get access_token 中 , 要把 Authorization 改成 No Auth

    • https://www.postman.com/xapidevelopers/twitter-s-public-workspace/collection/0xrqazu/twitter-oauth-1-0a-flow-test

第1步: get oauth_token

  • 打开:https://www.postman.com/xapidevelopers/twitter-s-public-workspace/request/x4anuar/step-1-get-oauth-token

  • 需要填入上文中生成的 Consumer Keys

  • 请求响应:

第2步:打开浏览器进行twitter授权

  • https://www.postman.com/xapidevelopers/twitter-s-public-workspace/request/5ji2j11/step-2-go-to-browser

  • 设置第一步响应的 oauth_token 和 oauth_token_secret

  • 复制 URL , 在浏览器中打开, 然后使用自己的twitter进行授权进行测试

  • 回调之后,浏览器中可以获取 oauth_tokenoauth_verifier

第3步:获取 access_token

  • 打开: https://www.postman.com/xapidevelopers/twitter-s-public-workspace/request/yuiqcd5/step-3-get-access-token

  • 将 Authorization 设置 为 "No Auth"

  • 设置 第2步中返回的: oauth_tokenoauth_verifier

  • 请求, 响应中包含:oauth_tokenoauth_token_secret

第4步: 发一条推文

  • 打开: https://www.postman.com/xapidevelopers/twitter-s-public-workspace/request/cva25a0/create-a-tweet?tab=body

  • 设置 Consumer Key

  • 设置 第3步响应中的 oauth_tokenoauth_token_secret

  • 编辑推文内容并发送请求

查看发送结果,发送成功

Twitter账号设置"自动发推"标签 (可选)

  • 官方文档: https://developer.x.com/en/docs/apps/overview

  •     设置流程:

        You can add an Automated Account label to your bot accounts to let users on X know that your bot is an automated account. These bots perform programmed actions through the X API. When you add an Automated Account label to your bot, you build trust with your audience, legitimize your account, and set yourself apart from spammy bots. This helps people on X better understand your account's purpose when interacting with your bot.

        To attach a label to your bot account, follow these steps:

    • 效果图:

    • Go to your account settings

    • Select “Your account”

    • Select “Automation”

    • Select “Managing account”

    • Next, select the X account, which runs your bot account

    • Enter your password to log in

    • Finally, you should see confirmation that the label has been applied to your account.

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐