【问题标题】:Problem using Dropbox API in python with OAuth2 code在带有 OAuth2 代码的 python 中使用 Dropbox API 的问题
【发布时间】:2018-12-14 08:54:39
【问题描述】:

我正在尝试使用 Dropbox 作为用户共享文件的媒介来设置应用程序。

我可以使用 App 令牌上传文件,但是当我尝试使用授权码时,文件无法上传;应用程序不报告错误。

下面的代码包含这两种方法:

dbx = get_dbx_with_token()

有效,但是

dbx = get_dbx_with_auth_code()

没有。任何帮助表示赞赏。

import webbrowser

from dropbox import Dropbox
from dropbox.files import WriteMode
from dropbox import DropboxOAuth2FlowNoRedirect

APP_KEY = '<my app key>'
APP_SECRET = '<my app secret>'
APP_TOKEN = '<my app token>'

def upload(dbx):
    local_file = '<path to local file>'
    remote_file = '<remote file name>'
    with open(local_file, 'rb') as f_upload:
        try:
            foo = dbx.files_upload(f_upload.read(), remote_file, 
                                   mode=WriteMode('overwrite'))
            print('done ...', foo)
        except:
            print('Upload error')

def get_dbx_with_auth_code():
    auth_flow = DropboxOAuth2FlowNoRedirect(APP_KEY, APP_SECRET)
    authorize_url = auth_flow.start()
    webbrowser.open(authorize_url)
    auth_code = input('Authorization code: ').strip()
    try:
        oauth_result = auth_flow.finish(auth_code)
    except:
        print('Token error')
        return None
    dbx = Dropbox(oauth_result.access_token)
    return dbx

def get_dbx_with_token():
    dbx = Dropbox(APP_TOKEN)
    return dbx

if __name__ == '__main__':
    dbx = get_dbx_with_token()
    #dbx = get_dbx_with_auth_code()
    upload(dbx)

dbx.files_upload 函数 (foo) 返回:

 FileMetadata(
      name='uploaded.txt', 
      id='id:<my_id>', 
      client_modified=datetime.datetime(2018, 12, 13, 18, 24, 15), 
      server_modified=datetime.datetime(2018, 12, 13, 18, 24, 15), 
      rev='013000000010ede3870', size=6, path_lower='/upload test/uploaded.txt', 
      path_display='/upload test/uploaded.txt', 
      parent_shared_folder_id=None, 
      media_info=None, 
      symlink_info=None, 
      sharing_info=None, 
      property_groups=None, 
      has_explicit_shared_members=None, content_hash='<content hash>')

get_dbx_with_token 的 FileMetadata 不同:

parent_shared_folder_id='1234567890', 
sharing_info=FileSharingInfo(read_only=False,
parent_shared_folder_id='1234567890',
modified_by='dbid:AAAyXwp1wvSzPzmqzCJ9SWFuxhc')

(顺便说一下,我上传到的文件夹是属于另一个用户的共享文件夹)

我认为它不起作用的原因是,如果我使用 get_dbx_with_auth_code(),它不会出现在我的(浏览器)Dropbox 文件夹中,但如果我使用 get_dbx_with_token(),它会出现。

【问题讨论】:

  • 你有任何输出吗?我看到您有一些打印语句,但实际上并没有打印出您在 except 块中捕获的任何异常。尝试打印这些以查看可能出现的问题。
  • @Greg 我在 upload() 尝试块中得到了“完成...”!
  • 听起来files_upload 成功完成了。是什么让你觉得这行不通?尝试打印出files_upload的返回值,查看上传的文件元数据。
  • FileMetData 看起来不错
  • 我已经用额外的信息更新了这个问题。

标签: python oauth-2.0 dropbox-api


【解决方案1】:

感谢 Greg 的 cmets,我意识到上传实际上正在发生,但我对 API 的了解有限意味着我没有意识到使用 auth_code 选项将文件放置在应用程序的特定文件夹中。现在很好,我知道在哪里可以找到它。

输出表明文件已成功上传,因此您在 Dropbox 网站上查找文件时听起来像是在不同的文件夹或帐户中查找。仔细检查您正在查看的帐户/文件夹。此外,您可能在两个流程之间使用不同的应用程序,因此请注意,如果应用程序具有“应用程序文件夹”权限,它将上传到特殊的“应用程序文件夹”为应用程序制作,默认在“/Apps”内

打印files_upload 的输出显示了正在发生的事情。

【讨论】:

    猜你喜欢
    • 2018-03-12
    • 1970-01-01
    • 2011-11-06
    • 1970-01-01
    • 2015-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多