Dropbox Python SDK,无法下载或列出文件
Dropbox Python SDK , unable to download or list files
我们计划使用 Dropbox 从客户位置获取一些 csv 文件。此 csv 文件需要由数据工程管道处理。我们正在使用 python 来处理这个。第一步,我们需要将文件从保管箱下载到本地文件夹。我首先使用 Dropbox App Console 创建了一个应用程序作为 Scoped App。在 Python 程序中,我们需要获得一个 API 访问令牌。从 scopped App 中,我无法生成访问令牌,因为我收到错误消息“您需要成为团队管理员才能生成令牌”。这是误导性的,因为这是我为测试它而创建的单个帐户,没有团队在场。我尝试了另一种方法,该方法使用用户 ID 和密码来提示输入访问令牌
这是代码:
class DropboxFolderCreation:
"""
This class is responsible for creating empty directories in dropbox account.
"""
def __init__(self):
# define your dropbox app key below
self.app_key = 'xxxxxxxxxxx'
# define your dropbox app secret key below
self.app_secret = 'xxxxxxxxxxxxx'
# define your CSV file path below
self.csv_path = 'example.csv'
def login_dropbox(self):
"""
Authorise Dropbox using OAuth 2.0
Follow instructions and authorise your Dropbox account.
"""
APP_KEY = self.app_key
APP_SECRET = self.app_secret
auth_flow = dropbox.DropboxOAuth2FlowNoRedirect(APP_KEY, APP_SECRET)
authorize_url = auth_flow.start()
print ("1. Go to: " + authorize_url)
print ("2. Click \"Allow\" (you might have to log in first).")
print ("3. Copy the authorization code.")
auth_code = input("Enter the authorization code here: ").strip()
try:
oauth_result = auth_flow.finish(auth_code)
except Exception as e:
print("Error: %s" % (e,))
return oauth_result
def read_csv(self,dbx):
"""
read .csv file and extract directory names
"""
"""wb = open_workbook(self.csv_path).sheet_by_index(0)
directory_list = []
# if csv file contains data from row 2 then set start_rowx = 1
# else set it to 0 as below
# first argument is set to 0 since we want to read column 1 of csv
csv_data = wb.col_values(0, start_rowx = 0)"""
#dbx = dropbox.Dropbox(<access_token>)
metadata, f = dbx.files_download(self.csv_path)
print(metadata)
csv_reader = csv.reader(f.content.decode().splitlines(), delimiter=',')
with open(metadata) as file:
line_count = 0
for row in csv_reader:
if line_count == 0:
print(f'Column names are {", ".join(row)}')
line_count += 1
else:
print(row)
line_count += 1
print(f'Processed {line_count} lines.')
return csv_data
def create_dirs_on_dropbox(self):
"""
Create empty directories in Dropbox account using API v2
"""
token = self.login_dropbox()
dbx = dropbox.Dropbox(token.access_token)
dirs = self.read_csv(dbx)
csv_data = self.read_csv(dbx)
if csv_data:
#doing something here
print("Successfully download file from your dropbox account ")
else:
print("could not read data from csv file")
并且在执行以下命令时:
dbx_instance = DropboxFolderCreation()
dbx_instance.create_dirs_on_dropbox()
1. Go to: https://www.dropbox.com/oauth2/authorize?response_type=code&client_id=a9hg3hhu85613yv
2. Click "Allow" (you might have to log in first).
3. Copy the authorization code.
Enter the authorization code here: dTuX5n5_iV0AAAAAAAAAKX5Rrskr-ZCroPMjuSK2qMo
已成功连接到 Dropbox,但在尝试访问文件时出现错误
错误为:
ValidationError: 'ListPriceChanges.csv' did not match pattern '(/(.|[\r\n])*|id:.*)|(rev:[0-9a-f]{9,})|(ns:[0-9]+(/.*)?)'
-我怀疑这个错误是因为我无法读取我使用此验证的文件夹列表
response = dbx.files_list_folder(path="")
print(response)
其中 returns 一个空列表。
所以我的问题是如何为作用域 App 生成访问令牌。我们有什么简单的方法来连接和下载文件吗?
我们有类似的问题。当您使用范围应用程序时,不要 select 任何与团队相关的范围,
从应用程序控制台,select您的应用程序,单击范围内的应用程序,然后删除select团队范围内的所有内容
您现在可以回来生成访问令牌。
一旦您获得了作用域应用程序的访问令牌,就非常简单了
import dropbox
dbx = dropbox.Dropbox("access token")
with open("example.csv", "wb") as f:
metadata, res = dbx.files_download(path="/example.csv")
f.write(res.content)
以上代码将从根文件夹下载 example.csv。如果你有任何文件夹下的文件说 test 然后在路径中你需要指定 /test/example.csv
我们计划使用 Dropbox 从客户位置获取一些 csv 文件。此 csv 文件需要由数据工程管道处理。我们正在使用 python 来处理这个。第一步,我们需要将文件从保管箱下载到本地文件夹。我首先使用 Dropbox App Console 创建了一个应用程序作为 Scoped App。在 Python 程序中,我们需要获得一个 API 访问令牌。从 scopped App 中,我无法生成访问令牌,因为我收到错误消息“您需要成为团队管理员才能生成令牌”。这是误导性的,因为这是我为测试它而创建的单个帐户,没有团队在场。我尝试了另一种方法,该方法使用用户 ID 和密码来提示输入访问令牌
这是代码:
class DropboxFolderCreation:
"""
This class is responsible for creating empty directories in dropbox account.
"""
def __init__(self):
# define your dropbox app key below
self.app_key = 'xxxxxxxxxxx'
# define your dropbox app secret key below
self.app_secret = 'xxxxxxxxxxxxx'
# define your CSV file path below
self.csv_path = 'example.csv'
def login_dropbox(self):
"""
Authorise Dropbox using OAuth 2.0
Follow instructions and authorise your Dropbox account.
"""
APP_KEY = self.app_key
APP_SECRET = self.app_secret
auth_flow = dropbox.DropboxOAuth2FlowNoRedirect(APP_KEY, APP_SECRET)
authorize_url = auth_flow.start()
print ("1. Go to: " + authorize_url)
print ("2. Click \"Allow\" (you might have to log in first).")
print ("3. Copy the authorization code.")
auth_code = input("Enter the authorization code here: ").strip()
try:
oauth_result = auth_flow.finish(auth_code)
except Exception as e:
print("Error: %s" % (e,))
return oauth_result
def read_csv(self,dbx):
"""
read .csv file and extract directory names
"""
"""wb = open_workbook(self.csv_path).sheet_by_index(0)
directory_list = []
# if csv file contains data from row 2 then set start_rowx = 1
# else set it to 0 as below
# first argument is set to 0 since we want to read column 1 of csv
csv_data = wb.col_values(0, start_rowx = 0)"""
#dbx = dropbox.Dropbox(<access_token>)
metadata, f = dbx.files_download(self.csv_path)
print(metadata)
csv_reader = csv.reader(f.content.decode().splitlines(), delimiter=',')
with open(metadata) as file:
line_count = 0
for row in csv_reader:
if line_count == 0:
print(f'Column names are {", ".join(row)}')
line_count += 1
else:
print(row)
line_count += 1
print(f'Processed {line_count} lines.')
return csv_data
def create_dirs_on_dropbox(self):
"""
Create empty directories in Dropbox account using API v2
"""
token = self.login_dropbox()
dbx = dropbox.Dropbox(token.access_token)
dirs = self.read_csv(dbx)
csv_data = self.read_csv(dbx)
if csv_data:
#doing something here
print("Successfully download file from your dropbox account ")
else:
print("could not read data from csv file")
并且在执行以下命令时:
dbx_instance = DropboxFolderCreation()
dbx_instance.create_dirs_on_dropbox()
1. Go to: https://www.dropbox.com/oauth2/authorize?response_type=code&client_id=a9hg3hhu85613yv
2. Click "Allow" (you might have to log in first).
3. Copy the authorization code.
Enter the authorization code here: dTuX5n5_iV0AAAAAAAAAKX5Rrskr-ZCroPMjuSK2qMo
已成功连接到 Dropbox,但在尝试访问文件时出现错误
错误为:
ValidationError: 'ListPriceChanges.csv' did not match pattern '(/(.|[\r\n])*|id:.*)|(rev:[0-9a-f]{9,})|(ns:[0-9]+(/.*)?)'
-我怀疑这个错误是因为我无法读取我使用此验证的文件夹列表
response = dbx.files_list_folder(path="")
print(response)
其中 returns 一个空列表。
所以我的问题是如何为作用域 App 生成访问令牌。我们有什么简单的方法来连接和下载文件吗?
我们有类似的问题。当您使用范围应用程序时,不要 select 任何与团队相关的范围,
从应用程序控制台,select您的应用程序,单击范围内的应用程序,然后删除select团队范围内的所有内容
您现在可以回来生成访问令牌。
一旦您获得了作用域应用程序的访问令牌,就非常简单了
import dropbox
dbx = dropbox.Dropbox("access token")
with open("example.csv", "wb") as f:
metadata, res = dbx.files_download(path="/example.csv")
f.write(res.content)
以上代码将从根文件夹下载 example.csv。如果你有任何文件夹下的文件说 test 然后在路径中你需要指定 /test/example.csv