[英]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)
它返回一個空列表。
所以我的問題是如何為作用域應用程序生成訪問令牌。 我們有什么簡單的方法來連接和下載文件嗎?
我們有類似的問題。 當您使用scope申請時,請勿select任何與團隊相關的scope,
從應用程序控制台,select 您的應用程序,單擊范圍應用程序,並取消選擇團隊 scope 下的所有內容
您現在可以返回並生成訪問令牌。
一旦您獲得了作用域應用程序的訪問令牌,那么它就非常簡單了
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.