localhost 拒绝在调用 google api 的数据块笔记本中进行连接
localhost refused to connect in a databricks notebook calling the google api
我阅读了 Google API 文档页面 (Drive API, pyDrive) 并创建了一个数据块笔记本来连接到 Google 驱动器。我使用文档页面中的示例代码如下:
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(
CRED_PATH, SCOPES)
creds = flow.run_local_server()
# 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()
CRED_PATH
包含 /dbfs/FileStore/shared_uploads
中的凭据文件路径。该脚本提示我 URL 授权应用程序,但在允许访问后立即重定向到显示“无法访问此站点:localhost 拒绝连接”的页面。
本地主机正在侦听默认端口 (8080):
我在 Google API 服务中检查了已注册应用程序的重定向 URI,它包括本地主机。
我不确定我应该 check/set 访问数据块中的 Google API 什么。任何想法表示赞赏
虽然我不确定这是否是适合您的情况的更好解决方法,但在您的情况下,使用服务帐户而不是您正在使用的 OAuth2 怎么样?这样,无需打开 URL 即可检索访问令牌以检索授权码,Drive API 可与您正在使用的 python 的 googleapis 一起使用。据此,我认为您的问题可能会被删除。
在您的脚本中使用服务帐号的方法如下。
用法:
1。创建服务帐户。
关于这个,可以看下面的官方文档
and/or
创建服务帐户时,会下载 JSON 数据的凭据文件。此文件用于脚本。
2。示例脚本:
将服务帐户与 googleapis 一起用于 python 的示例脚本如下。
from oauth2client.service_account import ServiceAccountCredentials
from googleapiclient.discovery import build
credentialFileOfServiceAccount = '###.json' # Please set the file path of the creadential file of service account.
creds = ServiceAccountCredentials.from_json_keyfile_name(credentialFileOfServiceAccount, ['https://www.googleapis.com/auth/drive.metadata.readonly'])
service = build('drive', 'v3', credentials=creds)
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']))
注:
- 服务帐户的 Google 驱动器与您的 Google 驱动器不同。因此,在这种情况下,当您使用服务帐户的邮件地址共享 Google 驱动器上的文件夹时(可以在凭据文件中看到此电子邮件地址。)。通过这种方式,您可以使用服务帐户获取文件并将其放入文件夹,您可以使用浏览器查看和编辑 Google 驱动器上文件夹中的文件。
参考文献:
我阅读了 Google API 文档页面 (Drive API, pyDrive) 并创建了一个数据块笔记本来连接到 Google 驱动器。我使用文档页面中的示例代码如下:
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(
CRED_PATH, SCOPES)
creds = flow.run_local_server()
# 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()
CRED_PATH
包含 /dbfs/FileStore/shared_uploads
中的凭据文件路径。该脚本提示我 URL 授权应用程序,但在允许访问后立即重定向到显示“无法访问此站点:localhost 拒绝连接”的页面。
本地主机正在侦听默认端口 (8080):
我在 Google API 服务中检查了已注册应用程序的重定向 URI,它包括本地主机。
我不确定我应该 check/set 访问数据块中的 Google API 什么。任何想法表示赞赏
虽然我不确定这是否是适合您的情况的更好解决方法,但在您的情况下,使用服务帐户而不是您正在使用的 OAuth2 怎么样?这样,无需打开 URL 即可检索访问令牌以检索授权码,Drive API 可与您正在使用的 python 的 googleapis 一起使用。据此,我认为您的问题可能会被删除。
在您的脚本中使用服务帐号的方法如下。
用法:
1。创建服务帐户。
关于这个,可以看下面的官方文档
and/or
创建服务帐户时,会下载 JSON 数据的凭据文件。此文件用于脚本。
2。示例脚本:
将服务帐户与 googleapis 一起用于 python 的示例脚本如下。
from oauth2client.service_account import ServiceAccountCredentials
from googleapiclient.discovery import build
credentialFileOfServiceAccount = '###.json' # Please set the file path of the creadential file of service account.
creds = ServiceAccountCredentials.from_json_keyfile_name(credentialFileOfServiceAccount, ['https://www.googleapis.com/auth/drive.metadata.readonly'])
service = build('drive', 'v3', credentials=creds)
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']))
注:
- 服务帐户的 Google 驱动器与您的 Google 驱动器不同。因此,在这种情况下,当您使用服务帐户的邮件地址共享 Google 驱动器上的文件夹时(可以在凭据文件中看到此电子邮件地址。)。通过这种方式,您可以使用服务帐户获取文件并将其放入文件夹,您可以使用浏览器查看和编辑 Google 驱动器上文件夹中的文件。