You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Twitter API授权后直接跳转,无法获取PIN码求助

问题

我尝试用Python调用Twitter API拉黑用户,写了block_user方法。运行脚本后终端输出授权URL,访问完成授权后,页面直接跳转到Twitter API应用设置里的重定向页面(当前设为default.com),看不到要粘贴到终端的PIN码,也没有任何成功提示。我试过加oauth_callback='oob'参数,但问题还是没解决,求帮忙。

我的代码

def block_user():

payload = {"target_user_id": "amazon"}

# 获取请求令牌
request_token_url = "https://api.twitter.com/oauth/request_token"
oauth = OAuth1Session(consumer_key, client_secret=consumer_secret,)

try:
    fetch_response = oauth.fetch_request_token(request_token_url)
except ValueError:
    print(
        "consumer_key或consumer_secret可能存在问题。"
    )

resource_owner_key = fetch_response.get("oauth_token")
resource_owner_secret = fetch_response.get("oauth_token_secret")
print("获取OAuth令牌: %s" % resource_owner_key)

# 获取授权
base_authorization_url = "https://api.twitter.com/oauth/authorize"
authorization_url = oauth.authorization_url(base_authorization_url)
print("请访问此链接授权: %s" % authorization_url)
verifier = input("粘贴PIN码到这里: ")

# 获取访问令牌
access_token_url = "https://api.twitter.com/oauth/access_token"
oauth = OAuth1Session(
    consumer_key,
    client_secret=consumer_secret,
    resource_owner_key=resource_owner_key,
    resource_owner_secret=resource_owner_secret,
    verifier=verifier,
    # 我试过加这个,但没用
    #oauth_callback='oob',
)
oauth_tokens = oauth.fetch_access_token(access_token_url)

access_token = oauth_tokens["oauth_token"]
access_token_secret = oauth_tokens["oauth_token_secret"]

# 发起请求
oauth = OAuth1Session(
    consumer_key,
    client_secret=consumer_secret,
    resource_owner_key=access_token,
    resource_owner_secret=access_token_secret,
)

# 发送拉黑请求
response = oauth.post(
    "https://api.twitter.com/2/users/{}/blocking".format(id), json=payload
)

if response.status_code != 200:
    raise Exception(
        "请求返回错误: {} {}".format(response.status_code, response.text)
    )

print("响应状态码: {}".format(response.status_code))

# 将响应转为JSON格式输出
json_response = response.json()
print(json.dumps(json_response, indent=4, sort_keys=True))

运行脚本时终端输出

获取OAuth令牌: 9SL...BGo
请访问此链接授权: https://api.twitter.com/oauth/authorize?oauth_token=9SL...BGo
粘贴PIN码到这里: 

解决方案

1. 修正oauth_callback='oob'的位置

你之前把oauth_callback加在获取访问令牌的OAuth1Session里,这是错误的。必须在**获取请求令牌(request token)**的阶段就传入这个参数,因为Twitter API需要在临时令牌请求阶段确认你使用PIN码模式(oob,离线模式)。修改后代码如下:

# 修改获取请求令牌的代码块
request_token_url = "https://api.twitter.com/oauth/request_token"
# 在这里传入oauth_callback='oob'
oauth = OAuth1Session(consumer_key, client_secret=consumer_secret, oauth_callback='oob')

try:
    fetch_response = oauth.fetch_request_token(request_token_url)
except ValueError:
    print("consumer_key或consumer_secret可能存在问题。")

2. 检查Twitter开发者平台应用设置

  • 登录Twitter开发者后台,进入你的应用页面,打开「Authentication Settings」
  • 确保App permissions至少设置为「Read and write」(拉黑操作需要写入权限)
  • 「Callback URI / Redirect URL」可以保留原设置(比如default.com),但代码中传入的oauth_callback='oob'会优先生效,强制使用PIN码授权模式

3. 修正其他代码问题

  • payload里的target_user_id必须是用户的数字ID(比如Amazon的官方账号ID是105083912),不能用用户名,否则API请求会失败
  • 拉黑接口https://api.twitter.com/2/users/{}/blocking中的{}需要替换为当前授权用户的数字ID,你需要先通过API获取这个ID,或者手动填入

4. 重新测试流程

修改后运行脚本,访问授权URL完成授权后,页面会直接显示PIN码,复制该PIN到终端输入即可获取访问令牌,进而执行拉黑操作。


内容的提问来源于stack exchange,提问作者mowsie2k

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 13:10:50