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

使用Splitwise Python SDK无法获取access token,不清楚code参数如何传递

关于getOAuth2AccessToken方法code参数的说明

这个参数是OAuth2授权码流程中的授权码,获取流程如下:

  • 你调用getOAuth2AuthorizeURL(redirect_uri)拿到的url是用户授权页地址,将这个地址复制到浏览器打开
  • 登录你的Splitwise账号,同意应用的权限申请后,页面会自动跳转到你填写的redirect_uri(你示例里填的是http://localhost/)
  • 跳转后地址栏的URL会带上query参数,格式为http://localhost/?code=xxx&state=xxx,这里的xxx部分的code值就是你需要传入的参数

注意事项

  • code有效期极短,一般只有几分钟,拿到后需要立刻调用getOAuth2AccessToken兑换access_token,过期就需要重新走授权流程
  • 跳转地址里的state参数需要和之前getOAuth2AuthorizeURL返回的state值做一致性校验,避免CSRF攻击
  • 你代码里填的redirect_uri必须和Splitwise开发者后台注册应用时填写的回调地址完全一致,否则会报错

本地测试示例补充

# 前面的初始化代码和你写的一致
from splitwise import Splitwise

consumer_key = <xxx> 
consumer_secret = <xxx>
s = Splitwise(consumer_key, consumer_secret)

redirect_uri = 'http://localhost/'
url, state = s.getOAuth2AuthorizeURL(redirect_uri)

print("请打开以下地址完成授权:", url)
# 授权后复制跳转后的地址栏URL粘贴到这里
callback_url = input("请粘贴跳转后的完整URL:")

# 提取code参数,这里用urllib.parse做解析
from urllib.parse import urlparse, parse_qs
parsed_url = urlparse(callback_url)
query_params = parse_qs(parsed_url.query)

# 校验state
if query_params.get('state')[0] != state:
    raise Exception("state校验不通过,请求非法")

code = query_params.get('code')[0]
access_token = s.getOAuth2AccessToken(code, redirect_uri)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 04:24:04