如何在Notion OAuth流程中传递额外数据?Telegram Bot开发问题
解决Notion OAuth回调中传递自定义参数的问题
方法1:使用OAuth标准的state参数(推荐)
Notion OAuth支持state参数,这个参数会在授权完成后原样返回给你的回调地址,专门用于传递自定义状态信息,非常适合关联Telegram用户和Notion用户:
- 构造授权URL时,将Telegram用户ID(或其他唯一标识)作为
state参数的值加入,例如:https://api.notion.com/v1/oauth/authorize?client_id=123&response_type=code&owner=user&redirect_uri=https://de86-171-5-224-214.ngrok-free.app/notion/oauth/callback&state=telegram_123456 - 回调时,Notion会返回
code=xxx&state=telegram_123456,你可以通过state值直接关联到对应的Telegram用户 - 注意:
state建议使用加密后的内容,避免泄露用户信息或被篡改,提升安全性
方法2:对redirect_uri中的参数进行URL编码
如果一定要在redirect_uri中携带自定义参数,需要对redirect_uri本身进行URL编码,否则Notion会将你添加的参数解析为OAuth授权URL的一部分,而非回调地址的参数:
- 原带参数的redirect_uri:
https://de86-171-5-224-214.ngrok-free.app/notion/oauth/callback?extra_data=test - 编码后的redirect_uri:
https%3A%2F%2Fde86-171-5-224-214.ngrok-free.app%2Fnotion%2Foauth%2Fcallback%3Fextra_data%3Dtest - 将编码后的地址填入授权URL的
redirect_uri参数:https://api.notion.com/v1/oauth/authorize?client_id=123&response_type=code&owner=user&redirect_uri=https%3A%2F%2Fde86-171-5-224-214.ngrok-free.app%2Fnotion%2Foauth%2Fcallback%3Fextra_data%3Dtest - 此时回调时,你的地址会收到
extra_data=test和Notion返回的code参数 - 注意:需要确保编码后的完整URL已添加到Notion集成后台的「重定向URI」允许列表中
内容的提问来源于stack exchange,提问作者gettmure
相关产品推荐
相关产品推荐

