tbw875
tbw875

Reputation: 379

Feeding Access Token into list_folder method, Dropbox API

I'm making a small python script to list all files recursively within a user's Dropbox account. The OAuth2.0 flow works, but I am frankensteining in another call to the list_folder method to list those files/folders.

I'm stuck at like 22 where I need to provide the access token returned by the OAuth flow. What can I call to return the token?

(Or, do you know of a quick python script that already does what I am asking)

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)

Upvotes: 0

Views: 317

Answers (1)

Greg
Greg

Reputation: 16930

If you're using the Dropbox Python SDK, you don't need to make the HTTP POST request manually like you're attempting.

You can get the access token from the app authorization flow, and make a Dropbox client, as shown in the DropboxOAuth2FlowNoRedirect documentation.

Then, you can call files_list_folder as shown in this example, (and files_list_folder_continue if ListFolderResult.has_more was True).

Upvotes: 1

Related Questions