Dropbox Python SDK , unable to download or list files

287 Views Asked by At

We are planning to use Dropbox to get some csv files transfered from client location. This csv files need to be processed by a data engineering pipeline. We are using python to process this. As a first step , we need to download the file from dropbox to the local file folder. I have created an app using Dropbox App Console as Scoped App first. In the Python program we need to get an API Access token. And from the scopped App , I was not able to generate the Access token as I was getting error stating that " You need to be a Team administrator to generate the token". This was misleading as this was a single account i created for testing it out and no teams are present. I tried with another method which is using the user id and secret to prompt for an access token

here is the code :

class DropboxFolderCreation:
    """ 
    This class is responsible for creating empty directories in  dropbox account. 
    """
    def __init__(self):
        # define your dropbox app key below
        self.app_key = 'xxxxxxxxxxx'
        # define your dropbox app secret key below
        self.app_secret = 'xxxxxxxxxxxxx'
        # define your CSV file path below
        self.csv_path = 'example.csv'
    def login_dropbox(self):
        """
         Authorise Dropbox using OAuth 2.0
         Follow instructions and authorise your Dropbox account.
         """
        APP_KEY = self.app_key
        APP_SECRET = self.app_secret
        auth_flow = dropbox.DropboxOAuth2FlowNoRedirect(APP_KEY, APP_SECRET)
        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 = input("Enter the authorization code here: ").strip()
        try: 
            oauth_result = auth_flow.finish(auth_code)
        except Exception as e:
            print("Error: %s" % (e,))
        return oauth_result
        
    def read_csv(self,dbx):
        """
         read .csv file and extract directory names
         """
        """wb = open_workbook(self.csv_path).sheet_by_index(0)
        directory_list = []
         # if csv file contains data from row 2 then set start_rowx = 1
         # else set it to 0 as below
         # first argument is set to 0 since we want to read column 1 of csv
        csv_data = wb.col_values(0, start_rowx = 0)"""
        #dbx = dropbox.Dropbox(<access_token>)

        metadata, f = dbx.files_download(self.csv_path)
        print(metadata)
        csv_reader = csv.reader(f.content.decode().splitlines(), delimiter=',')
        with open(metadata) as file:
            line_count = 0
            for row in csv_reader:
                if line_count == 0:
                    print(f'Column names are {", ".join(row)}')
                    line_count += 1
                else:
                    print(row)
                    line_count += 1

            print(f'Processed {line_count} lines.')


        return csv_data
    
    def create_dirs_on_dropbox(self):
        """
         Create empty directories in Dropbox account using API v2
         """
        
        token = self.login_dropbox()
        dbx = dropbox.Dropbox(token.access_token)
        dirs = self.read_csv(dbx)
        csv_data = self.read_csv(dbx)
        if csv_data:
            #doing something here 
             print("Successfully download  file from  your dropbox account ")
        else:
            print("could not read data from csv file")
       

And when executing the below :

dbx_instance = DropboxFolderCreation()
dbx_instance.create_dirs_on_dropbox()

1. Go to: https://www.dropbox.com/oauth2/authorize?response_type=code&client_id=a9hg3hhu85613yv
2. Click "Allow" (you might have to log in first).
3. Copy the authorization code.
Enter the authorization code here: dTuX5n5_iV0AAAAAAAAAKX5Rrskr-ZCroPMjuSK2qMo

Connection to Dropbox is successful , but getting error while trying to access the file

error as :

ValidationError: 'ListPriceChanges.csv' did not match pattern '(/(.|[\r\n])*|id:.*)|(rev:[0-9a-f]{9,})|(ns:[0-9]+(/.*)?)'

-I suspected this error is coming because I am not able to read the folder list which I verified using this

response = dbx.files_list_folder(path="")
print(response)

which returns an empty list.

So My problem is how to generate the access token for the scoped App . Do we have any simple way to connect and download the files ?

1

There are 1 best solutions below

1
On

We have similar issues . When you use scope application, do not select any scope related to teams ,

from the App console, select your application, Click on the scoped App, and deselect everything under the team scope

enter image description here

you can come back and generate the access token now.

Once you have access token for your scoped App, then it is pretty straight forward

import dropbox
dbx = dropbox.Dropbox("access token")

with open("example.csv", "wb") as f:
    metadata, res = dbx.files_download(path="/example.csv")
    f.write(res.content)

The above code will download the example.csv from the root folder . If you have the file under any folder say test then in the path you need to specify /test/example.csv