通过API上传到Dropbox的文件是二进制文件

问题描述 投票:0回答:0

我正在使用Python 3的dropbox 9.3.0项目( https://pypi.org/project/dropbox/

我可以上传/下载代码到Dropbox(用于商业),但文件到达是二进制的,而不是Dropbox本身的JSON。

即保存的JSON的内容包含1111011 100010 1101101 1100001 1101010 1101111 1110010 1010000 1100001 1 ...

我的代码如下:

def dict_to_binary(the_dict):
    str = json.dumps(the_dict)
    binary = ' '.join(format(ord(letter), 'b') for letter in str).encode("utf-8")
    return binary


def initialise_dropbox():
    dbx = dropbox.Dropbox(dropbox_access_token, headers={'Content-type': 
'application/json'})
    return dbx

def push_to_dropbox(file_object,dropbox_base_url,sub_folder,file_name,file_type,dbx,file_object_summary_str=''):
    if file_type == 'json':
        # or else dropbox complains, e.g. TypeError: expected request_binary as binary type
        binary = dict_to_binary(file_object)

    full_dbx_path = os.path.join(dropbox_base_url,sub_folder,file_name+'.'+file_type)
   dbx.files_upload(binary,full_dbx_path,mode=dropbox.files.WriteMode.overwrite)

这是相关的Dropbox文档 - https://dropbox-sdk-python.readthedocs.io/en/latest/api/dropbox.html#dropbox.dropbox.Dropbox.files_upload - 我只是对我的内容感到困惑我做错了。

我需要能够上传一个在Dropbox中是有效JSON的文件。

python python-3.x dropbox-api
© www.soinside.com 2019 - 2024. All rights reserved.