将访问令牌提供到list_folder方法,Dropbox API

时间:2017-03-30 18:20:16

标签: python oauth-2.0 dropbox-api

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

我被困在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)

1 个答案:

答案 0 :(得分:1)

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

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

然后,您可以致电files_list_folder as shown in this example,如果files_list_folder_continue$(document).on("click", "div.a", function() {});,则可以ListFolderResult.has_more