gpt4 book ai didi

Python Dropbox API v2 : error when passing SharedLinkSettings

转载 作者:行者123 更新时间:2023-12-01 09:24:51 24 4
gpt4 key购买 nike

在我的 Python 类中,我有一个函数可以生成 csv 文件,将该文件上传到 Dropbox,然后尝试生成指向已上传文件的共享链接,过期时间为 5 分钟。

我正在尝试遵循提供的示例 here ;我的代码如下:

    # Helper upload function
def upload_file(self, dbx, file_from, file_to):
"""upload a file to Dropbox using API v2
"""

with open(file_from, 'rb') as f:
dbx.files_upload(f.read(), file_to, mode=WriteMode('overwrite'))


# Target function
def generate_expiring_dropbox_link(self, local_path, dbx_path):

dbx = dropbox.Dropbox(self.temp_dropbox_token)

expires = datetime.datetime.now() + datetime.timedelta(minutes=5)
requested_visibility = dropbox.sharing.RequestedVisibility.team_only

desired_shared_link_settings = dropbox.sharing.SharedLinkSettings(requested_visibility=requested_visibility,
expires=expires)

# open the file and upload it
self.upload_file(dbx, local_path, dbx_path)

shared_link_metadata = dbx.sharing_create_shared_link_with_settings(path=dbx_path, settings=desired_shared_link_settings)

return shared_link_metadata

我一直收到与共享链接设置相关的 API 错误:

    dropbox.exceptions.ApiError: ApiError('************', CreateSharedLinkWithSettingsError('settings_error', SharedLinkSettingsError('invalid_settings', None)))

我找不到太多这方面的文档;有人遇到过/找到解决方案吗?只是想知道是否有基于 python 的修复,或者包装 HTTP 请求是否是更好的选择。我使用的是 Python 3.6 和 Dropbox 8.9.0。

最佳答案

Dropbox API 需要 UTC 时间。您提供的本地时间可以是相对于 UTC 时间的过去时间。如果是这样,API 将拒绝设置,因为过期时间不能是过去的。

所以,而不是:

    expires = datetime.datetime.now() + datetime.timedelta(minutes=5)

做:

    expires = datetime.datetime.utcnow() + datetime.timedelta(minutes=5)

关于Python Dropbox API v2 : error when passing SharedLinkSettings,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50512663/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com