Sim*_*mon 5 python django google-api google-api-python-client django-allauth
我在 Django 应用程序上使用 AllAuth 来管理用户身份验证。这样我的用户就可以连接他们的 Google 帐户,并且我获得一个 google API 令牌(具有适当的范围)。
我想使用该令牌来访问 google API(在我的例子中是日历 v3),因为我的用户已经使用 OAuth2 登录我的网站,并允许我访问日历 API。
Google 只在他们的网站上提供了完整的流程(从 auth 到 api),有没有办法实现我的想法,或者根本不可能?
我已经尝试过这里drive = build('calendar', 'v3', credentials=credentials)所说的,但“凭据”需要是 oauth2client 的一部分,而不仅仅是一个简单的字符串令牌。
感谢您宝贵的时间。
西蒙
小智 3
我知道这是一个较老的问题,但我终于找到了对我有用的东西。使用 allauth 成功进行身份验证后,我这样做是为了获取客户端令牌,然后构建服务。
from googleapiclient.discovery import build
from google.oauth2.credentials import Credentials
from allauth.socialaccount.models import SocialToken, SocialApp
...
# request is the HttpRequest object
token = SocialToken.objects.get(account__user=request.user, account__provider='google')
credentials = Credentials(
token=token.token,
refresh_token=token.token_secret,
token_uri='https://oauth2.googleapis.com/token',
client_id='client-id', # replace with yours
client_secret='client-secret') # replace with yours
service = build('calendar', 'v3', credentials=credentials)
Run Code Online (Sandbox Code Playgroud)
确保SOCIALACCOUNT_STORE_TOKENS = True按照https://django-allauth.readthedocs.io/en/latest/configuration.html进行配置,否则您将不会拥有任何 SocialToken 对象。
| 归档时间: |
|
| 查看次数: |
1955 次 |
| 最近记录: |