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

Twitter OAuth 2.0 PKCE:如何将授权码兑换为访问令牌?

Twitter OAuth 2.0 PKCE 授权码兑换访问令牌指南

请求URL

兑换授权码的接口URL为:
https://api.twitter.com/2/oauth2/token

请求格式要求

  1. 请求方法:必须使用POST
  2. Content-Type:设置为application/x-www-form-urlencoded
  3. 必填参数(所有参数均需以表单形式提交):
    • code:从回调URL中获取的授权码
    • grant_type:固定值authorization_code
    • redirect_uri:需与发起授权请求时使用的回调URI完全一致(包括协议、域名、路径)
    • client_id:你的Twitter开发者应用ID
    • code_verifier:生成授权URL时使用的PKCE验证码验证器(注意不是code_challenge)

示例请求(curl)

curl -X POST "https://api.twitter.com/2/oauth2/token" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "code=你的授权码&grant_type=authorization_code&redirect_uri=https://你的回调地址&client_id=你的应用ID&code_verifier=你的验证码验证器"

注意事项

  • 所有参数必须严格匹配授权步骤中的对应值,尤其是redirect_uri和code_verifier,不一致会导致认证失败
  • 无需添加Authorization Basic头,直接通过client_id参数传递应用身份信息
  • 成功响应会返回JSON格式数据,包含access_token(访问令牌)、token_type、expires_in等字段

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 00:20:51