Python Google Drive API OAuth 2.0:错误连接被拒绝
我在个人GCP中启用了Google Drive API,设置了OAuth2同意屏幕,并下载了credentials.json文件。我现在在Colab中运行以下代码:Python Google Drive API OAuth 2.0:错误连接被拒绝,python,google-cloud-platform,google-drive-api,google-oauth,Python,Google Cloud Platform,Google Drive Api,Google Oauth,我在个人GCP中启用了Google Drive API,设置了OAuth2同意屏幕,并下载了credentials.json文件。我现在在Colab中运行以下代码: from __future__ import print_function import pickle import os.path from googleapiclient.discovery import build from google_auth_oauthlib.flow import InstalledAppFlow f
from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly']
def main():
"""Shows basic usage of the Drive v3 API.
Prints the names and ids of the first 10 files the user has access to.
"""
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('drive', 'v3', credentials=creds)
# Call the Drive v3 API
results = service.files().list(
pageSize=10, fields="nextPageToken, files(id, name)").execute()
items = results.get('files', [])
if not items:
print('No files found.')
else:
print('Files:')
for item in items:
print(u'{0} ({1})'.format(item['name'], item['id']))
if __name__ == '__main__':
main()
当我执行代码时,系统会提示我通过Google的OAuth登录页面登录,让我的程序访问我的Google帐户。我单击“允许”,然后立即看到此页面:
我在没有VPN或防火墙的情况下重做了这件事,我不知道这是否是我在项目中的OAuth或creds设置的问题。想法?通过运行PyCharm解决。VSCode也可以工作。无论出于何种原因,Colab都会继续抛出错误。通过运行PyCharm解决。VSCode也可以工作。无论出于何种原因,Colab都会继续抛出错误。提供解决方案: 在打开浏览器时运行_local_server()时,复制url并使用私人浏览使用此链接登录 它适合我。提供一个解决方案: 在打开浏览器时运行_local_server()时,复制url并使用私人浏览使用此链接登录
它适合我。尝试从
creds=flow中删除端口参数。运行_local_server(port=0)
默认端口为8080,因此,如果您已经使用该端口,请尝试1023以上65535以下的数字。端口号0无效。我使用1023和65535之间的各种端口、端口8080以及端口参数进行了尝试。不幸的是,没有运气:(但如果这是错误的来源,我可以更深入地研究它。运行本地服务器()
启动本地Web服务器。出现问题,服务器没有启动(AFAIK)。您是否看到任何错误消息等?请编辑您的问题,并提供有关如何运行代码等的更多详细信息。我已通过在PyCharm中而不是在Colab中本地运行该错误来修复该错误。我建议您使用找到的解决方案发布答案。尝试从creds=flow.run_local_server(port=0)中删除端口参数
默认端口为8080,因此,如果您已经使用该端口,请尝试1023以上、65535以下的数字。端口号0无效。我尝试了1023和65535之间的各种端口,端口为8080,也没有端口参数。不幸的是,没有运气:(但如果这是错误的来源,我可以更深入地了解它。run\u local\u server()
启动本地Web服务器。出现问题,服务器没有启动(AFAIK)。您是否看到任何错误消息等?编辑您的问题,并提供有关如何运行代码等的更多详细信息。我通过在PyCharm中而不是在Colab中本地运行来修复错误。我建议您发布一个包含找到的解决方案的答案。欢迎Lier!很高兴您来到这里。有几件事。如果您查看其他答案,您可以看到原来的海报回答了他自己的问题。考虑到更多的细节,特别是如果它们是旧的。另外,你提到一个链接,没有一个链接,你可以编辑你的答案,如果你喜欢的话,可以添加一个。在StAdvOpk上有一些礼仪标准,你可以在这里找到它们。欢迎Lier!很高兴你在这里。几件事。如果你看的话。在另一个答案中,你可以看到原来的海报回答了他自己的问题。考虑到更多的细节,特别是如果他们是旧的。此外,你提到一个链接,没有一个,你可以编辑你的答案,并添加一个如果你喜欢。有一些礼仪标准上的StAcExcel,你可以在这里找到它们。