python - PyDrive 2.0 – AuthenticationError:在重定向中找不到代码
问题描述
设置
我正在使用 PyDrive 2.0 连接到 Google Drive API。
def connect_google_drive_api():
import os
# use Gdrive API to access Google Drive
os.chdir('/Users/my/fol/ders/access_google_drive')
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
gauth = GoogleAuth()
gauth.LocalWebserverAuth() # client_secrets.json need to be in the same directory as the script
drive = GoogleDrive(gauth)
return drive
工作目录/Users/mypath/access_google_drive
包含client_secrets.json
,看起来像,
{"web":{"client_id":"xxx","project_id":"invoice-creation-290413","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"xxx","redirect_uris":["http://localhost:8080/"],"javascript_origins":["http://localhost:8080"]}}
我将真实的client_id
和client_secret
替换为xxx
.
问题
当浏览器(Safari 14.0)显示Gdrive api link wants to access your Google Account
并单击Allow
时,该过程似乎卡住了。
大约 20 秒后,显示以下错误,
Failed to find "code" in the query parameters of the redirect.
Try command-line authentication
Traceback (most recent call last):
File "<ipython-input-36-792f41ab7318>", line 1, in <module>
gauth.LocalWebserverAuth()
File "/opt/anaconda3/lib/python3.7/site-packages/pydrive2/auth.py", line 125, in _decorated
code = decoratee(self, *args, **kwargs)
File "/opt/anaconda3/lib/python3.7/site-packages/pydrive2/auth.py", line 273, in LocalWebserverAuth
raise AuthenticationError("No code found in redirect")
AuthenticationError: No code found in redirect
我该如何解决这个问题?
解决方案
我正在导入connect_google_drive_api()
另一个脚本,比如 script x.py
。
connect_google_drive_api()
函数和x.py
行都可以os.chdir('/Users/my/fol/ders/access_google_drive')
将工作目录设置为所在的client_secrets.json
位置。
使用以下代码,我实际上可以毫无问题地建立连接,
def connect_google_drive_api():
# use Gdrive API to access Google Drive
from pydrive2.auth import GoogleAuth
from pydrive2.drive import GoogleDrive
gauth = GoogleAuth()
gauth.LocalWebserverAuth() # client_secrets.json need to be in the same directory as the script
drive = GoogleDrive(gauth)
return drive
推荐阅读
- java - 单击按钮后,我的应用程序不断崩溃
- r - 如何修复从pickerInput和其他输入中呈现的ggplotly闪亮?
- python - 如何修复“'CategoricalDtype'类型的对象没有len()”类型的错误?
- algorithm - 在给定的约束条件下,我可以使用哪些算法/方法来实现一周中几天的任务调度?
- max - 一种在 googleway 库中的 google_map 中设置缩放限制的方法
- swift - 如何在 Swift 4.2 的另一个视图控制器中删除 UICollectionView 中选定/突出显示的单元格
- c# - Oracle TO_DATE() 函数
- javascript - 如何为应用促销代码创建完整的功能
- c++ - 在没有默认可构造性的 constexpr 环境中获取任何类型的值
- ios - 如何在 Xamarin、IOS 中使用 SDWebImage 绑定