如何使用Google oauth2.0通过电报机器人来验证用户
这是我第一次与Google API互动,我正在使用此库中使用Python3.9 python电报机器人 我想通过电报机器人访问用户Google API日历,我似乎找不到任何文章来指导我完成它。我的关键问题(我认为)是将成功授权转移回电报机器人。
这是我的想法:
- 在电报应用程序中,用户发送“/发送”到bot
- bot接收消息并返回Google授权链接到用户
- clink on授权链接,并允许访问
- 机器人接收授权访问并完成OAuth Flow
问题在于第3和4步。标准授权链接是 https://accounts.google.com/o/oauth2/auth?Response_type = code = code&Client_Id=&clientId=&clientId>&ggt&recredect_uri = recredect_Uri =& amp; amp; ;& access_type =离线
如何将授权链接发送回电报机器人?我应该创建另一个API端点以接收该授权链接吗?或者我可以在< redirect_uri>中发送telegram api send_message()将成功消息重定向到我的机器人。
更新1
多亏了CallMestag,我设法找到了一种完成OAuth过程的方法。对于面临同样问题的人来说,这就是我所做的 先决条件:凭据是在Google Console API -Web应用程序中创建的。 redirect_uri集作为localhost:8000(在开发阶段)
- 用户发送“/发送”到bot
- bot接收消息并返回授权链接
https://accounts.google.com/o/oauth2/auth2/auth?Response_Type = code& client_id =< clientId>& redirect_uri =
http:// localhost:8000/
& scope =< scope> scope> scope> scope> & state< state>& access_type = offline
- 用户单击链接到Authenticate,它将重定向到
http:// http:// localhost:8000
。使用FastApi作为Webhook接收消息。捕获授权代码,使用google.oauthlib.flow
完成授权过程。接下来,将用户重定向到电报链接https://t.me/<; botname&gt;
- 使用用户Google google日历开始
This is my first time interacting with Google API and I'm using python3.9 with this library Python Telegram Bot
I want to access a user Google API Calendar via a telegram bot and I can't seem to find any article to guide me through it. My key problem (I think) is redirecting the success authorization flow back to telegram bot.
This is what I have in mind:
- In the telegram app, user send '/send' to bot
- Bot receive message and return a google an authorization link to user
- User clink on authorization link and allow access
- Bot receive authorization access and completes the Oauth flow
The problem lies betweeen step 3 and 4. A standard authorization link ishttps://accounts.google.com/o/oauth2/auth?response_type=code&client_id=<clientid>&redirect_uri=<redirect_uri>&scope=<scope>&state<state>&access_type=offline
How do I send the authorization link back to my telegram bot? Should I create another API endpoint to receive that authorization link? Or can I send telegram api send_message() in the <redirect_uri> to redirect the success message to my bot.
Update 1
Thanks to CallMeStag, I manage to figure out a way to complete the oauth process. For people who faced the same problem, this is what I did
Pre-requisite: Credentials is created in google console api - Web application. redirect_uri set as localhost:8000 (During development phase)
- User send '/send' to bot
- Bot receive message and return an authorization link
https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=<clientid>&redirect_uri=
http://localhost:8000/
&scope=<scope>&state<state>&access_type=offline
- User click on link to authenticate and it will redirect to
http://localhost:8000
. Used fastapi as a webhook to receive the message. Capture the authorization code, usegoogle.oauthlib.flow
to complete the authorization process. Next, redirect user back to telegram linkhttps://t.me/<botname>
- Start using user google calendar
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
目前,PTB 应用程序侦听外部更新(此 cas 中的身份验证验证)确实不是很直接 - 另请参阅 此问题。目前,您可能最容易设置一个与 Updater 并行运行的自定义 Webhook 应用程序 - 例如使用 Flask/django/starlette/fastapi/...。或者,如果您无论如何都在为您的机器人使用 Webhooks,您可以修补
Updater
来为您完成这项工作。尽管这需要一些手动工作 - 请参阅此处查看示例。一旦您能够监听来自 Google 的更新,就可以通过 PTB 常用处理程序设置来处理它们,特别是通过
TypeHandler
甚至自定义Handler
子类 - 请参阅此<一href="https://github.com/python-telegram-bot/python-telegram-bot/wiki/Frequently-Asked-Questions#i-want-to-handle-updates-from-an-external-service-in -addition-to-the-telegram-updates-how-do-i-do-that" rel="nofollow noreferrer">常见问题解答条目。关于重定向网址:您需要将用户重定向回您的机器人,因此您必须提供一个链接来执行此操作。
Bot.link
应该可以解决问题。
免责声明:我目前是
python-telegram-bot
的维护者。It's currently indeed not very straight forward for a PTB-application to listen for external updates (the auth verification in this cas) - see also this issue. Currently it might be easiest for you to set up a custom webhook application that runs in parallel to the
Updater
- e.g. using flask/django/starlette/fastapi/…. Alternatively, if you're using webhooks for your bot anyway, you can patch theUpdater
to do the job for you. Although that requires some manual work - see here for an example.Once you are able to listen to updates coming from Google, handling them can be done via PTBs usual handler setup, specifically via the
TypeHandler
or even a customHandler
subclass - see this FAQ entry.Regarding the redirect url: You'll want to redirect your user back to your bot, so you'll have to provide a link that does that.
Bot.link
should probably do the trick.Disclaimer: I'm currently the maintainer of
python-telegram-bot
.与 Google设备授权流量
integrate with the google device authorization flow