python - 从 django-python 访问 Google 驱动器
问题描述
在Django
我使用社交身份验证应用程序实现社交登录时,我按照此链接进行配置。
Goolge OAuth
工作良好;google-oauth2
访问令牌存储在额外的数据字段中。
现在我想使用这个访问令牌列出谷歌驱动器文件。我试过这个。
def drive(request):
user = request.user
social = user.social_auth.get(provider='google-oauth2')
response = requests.get(
'https://www.googleapis.com/auth/drive.metadata.readonly',
params={'access_token': social.extra_data['access_token']})
print(response)
return render(request, 'home/drive.html', {'checking':response})
我收到了200
回复,但我不知道如何列出文件。
我正在使用django 2.0.3
和python 3.5
。
解决方案
将设置更改为重新提示 Google OAuth2 用户刷新 refresh_token
SOCIAL_AUTH_GOOGLE_OAUTH2_AUTH_EXTRA_ARGUMENTS = {'access_type':'离线'}
并使用 google-auth 库对 Google API 进行身份验证
def drive(request):
user = request.user
social = user.social_auth.get(provider='google-oauth2')
creds=google.oauth2.credentials.Credentials(social.extra_data['access_token'])
drive = googleapiclient.discovery.build('drive', 'v3', credentials=creds)
files = drive.files().list().execute()
参考:
推荐阅读
- google-oauth - 允许来自两个域(例如 example.com、example.org)的用户使用 Google 的 OAuth2 进行授权
- shell - 将 < 或 > 传递给 shell 命令?
- gitlab - Gitlab-ci 管道中“工作流程:规则”的目的是什么?
- r - 通过 Rstudio 中的“作业”选项卡安装 R 包
- react-native - React Native Axios 请求正在冻结 UI
- json - 我如何把它变成一个json?
- reactjs - Bootstrap Carousel 在我的反应项目中不起作用。我已经尝试了一切,但仍然无法正常工作
- bash - bash:在同一行回显具有延迟的短语
- python - 用同一数据框的另一列替换迭代的列集
- php - 如何在 Laravel 8 中让我的数组构建速度更快?