【问题标题】:Feeding Access Token into list_folder method, Dropbox API将访问令牌提供给 list_folder 方法,Dropbox API
【发布时间】:2017-03-30 18:20:15
【问题描述】:

我正在制作一个小型 Python 脚本,以递归方式列出用户 Dropbox 帐户中的所有文件。 OAuth2.0 流程有效,但我在另一个调用 list_folder 方法以列出这些文件/文件夹时进行了 frankensteining。

我被困在 22 点,我需要提供 OAuth 流返回的访问令牌。我可以调用什么来返回令牌?

(或者,你知道一个快速的 python 脚本已经可以满足我的要求)

from dropbox.client import DropboxOAuth2FlowNoRedirect, DropboxClient
from dropbox import rest as dbrest
import requests
import json

auth_flow = DropboxOAuth2FlowNoRedirect('<APP_KEY>', '<APP_SECRET>')
###These are filled out in my code, but I hid them here.

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 = raw_input("Enter the authorization code here: ").strip()

try:
    oauth_result = auth_flow.finish(auth_code)
except dbrest.ErrorResponse, e:
    print('Error: %s' % (e,))

url = "https://api.dropboxapi.com/2/files/list_folder"

headers = {
    "Authorization": "Bearer <ACCESS_TOKEN>",
    "Content-Type": "application/json"
}

data = {
    "path": "",
    "recursive": True,
    "include_media_info": True,
    "include_deleted": True
}

r = requests.post(url, headers=headers, data=json.dumps(data))

print(r.text)

【问题讨论】:

    标签: python oauth-2.0 dropbox-api


    【解决方案1】:

    如果您使用的是Dropbox Python SDK,则无需像您尝试的那样手动发出 HTTP POST 请求。

    您可以从应用授权流程中获取访问令牌,并制作一个Dropbox客户端,如DropboxOAuth2FlowNoRedirect documentation所示。

    然后,您可以致电files_list_folderas shown in this example,(如果ListFolderResult.has_moreTrue,则files_list_folder_continue)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多