We're making changes to the Community, so you may have received some notifications - thanks for your patience and welcome back. Learn more here.

Forum Discussion

Aryahagh's avatar
Aryahagh
New member | Level 2
5 years ago

Reading programmatically several files from Dropbox business

Hi

I want to read (or only reading is not possible, download) several files in different folders ,which is stored in a Dropbox buisiness account, programmatically in python. I were able to create an app and using Access Token download data when it was created in regular account, but when I select "Dropbox Business API" while creating a new app it suggesting 4 new options and I tried all of those options but when I tried using Access Token to download the data it gave me BadInputError for example saying ("Since your API app key has team member file access permissions, you can operate on a team member\'s Dropbox by providing the "Dropbox-API-Select-User" HTTP header or "select_user" URL parameter to specify the exact user <https://www.dropbox.com/developers/documentation/http/teams>.")

How should I manage this problem? 

 

Thanks in advance 

  • If you only want to access the files in one particular account, you can register a "Dropbox API" app with the "Full Dropbox" permission and then use the /2/files/download endpoint to download files from a connected account without any extra configuration. (You can also connect the app to multiple different accounts, but each user would need to authorize their own account, to give the app a distinct access token for each one.) That would look like this:

     

    curl -X POST https://content.dropboxapi.com/2/files/download \
        --header "Authorization: Bearer <ACCESS_TOKEN>" \
        --header "Dropbox-API-Arg: {\"path\": \"/test.txt\"}"

     

    Or, using the Python SDK:

     

    dbx = dropbox.Dropbox(ACCESS_TOKEN)
    
    print(dbx.files_download("/test.txt"))

     

    If you want to be able to access the files any team member's account on a Business team, by only having a team admin authorize the app once, you will need to register a "Dropbox Business API" app with the "team member file access" permission and then use the /2/files/download endpoint to download files, but with some extra configuration as covered in the "Member file access" section of the Business API documentation. Since Dropbox Business API apps are connected to the entire Business team, you need to specify which member you want to operate on when making a user-level call, such as /2/files/download. You can do so by specifying the Dropbox-API-Select-User" header with the value being the team member ID of the member you want to call for. The team member ID starts with "dbmid:" and can be retrieved from the Dropbox Business API, such as via /2/team/members/get_info or /2/team/members/list[/continue]. That would look like this:

     

    curl -X POST https://content.dropboxapi.com/2/files/download \
        --header "Authorization: Bearer <TEAM_ACCESS_TOKEN>" \
        --header "Dropbox-API-Select-Admin: <TEAM_MEMBER_ID>" \
        --header "Dropbox-API-Arg: {\"path\": \"/test.txt\"}"

     

    Or, using the Python SDK:

     

    dbx = dropbox.DropboxTeam(TEAM_ACCESS_TOKEN).as_user(TEAM_MEMBER_ID)
    
    print(dbx.files_download("/test.txt"))

     

     

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Staff rankDropbox Staff

    If you only want to access the files in one particular account, you can register a "Dropbox API" app with the "Full Dropbox" permission and then use the /2/files/download endpoint to download files from a connected account without any extra configuration. (You can also connect the app to multiple different accounts, but each user would need to authorize their own account, to give the app a distinct access token for each one.) That would look like this:

     

    curl -X POST https://content.dropboxapi.com/2/files/download \
        --header "Authorization: Bearer <ACCESS_TOKEN>" \
        --header "Dropbox-API-Arg: {\"path\": \"/test.txt\"}"

     

    Or, using the Python SDK:

     

    dbx = dropbox.Dropbox(ACCESS_TOKEN)
    
    print(dbx.files_download("/test.txt"))

     

    If you want to be able to access the files any team member's account on a Business team, by only having a team admin authorize the app once, you will need to register a "Dropbox Business API" app with the "team member file access" permission and then use the /2/files/download endpoint to download files, but with some extra configuration as covered in the "Member file access" section of the Business API documentation. Since Dropbox Business API apps are connected to the entire Business team, you need to specify which member you want to operate on when making a user-level call, such as /2/files/download. You can do so by specifying the Dropbox-API-Select-User" header with the value being the team member ID of the member you want to call for. The team member ID starts with "dbmid:" and can be retrieved from the Dropbox Business API, such as via /2/team/members/get_info or /2/team/members/list[/continue]. That would look like this:

     

    curl -X POST https://content.dropboxapi.com/2/files/download \
        --header "Authorization: Bearer <TEAM_ACCESS_TOKEN>" \
        --header "Dropbox-API-Select-Admin: <TEAM_MEMBER_ID>" \
        --header "Dropbox-API-Arg: {\"path\": \"/test.txt\"}"

     

    Or, using the Python SDK:

     

    dbx = dropbox.DropboxTeam(TEAM_ACCESS_TOKEN).as_user(TEAM_MEMBER_ID)
    
    print(dbx.files_download("/test.txt"))

     

     

    • Aryahagh's avatar
      Aryahagh
      New member | Level 2

      Thanks Greg,

      By the way I was able to download data from the dropbox by these solutions using following command :

      dbx.files_download(path="PATHtoFILE")

      However I didn't find a way to only read the document without downloading it. What I want to do is opening a .txt file and read a certain line in it or open a video and get only one frame of it without downloading the whole video. Is there any way to do that?

       

      Thanks again

      • Greg-DB's avatar
        Greg-DB
        Icon for Dropbox Staff rankDropbox Staff

        The files_download method returns a tuple of (FileMetadatarequests.Response), so you can get the file data from requests.Response using the methods provided by that class.

        For instance, you can stream content from it piece by piece, without downloading the whole thing, using requests.Response.iter_content and/or requests.Response.iter_lines, if those work for your use case. As a contrived example:

        metadata, res = dbx.files_download("/test.txt")
        
        for data in res.iter_lines():
            print(data)
            raw_input()

        That sounds like that isn't exactly what you're looking for though unfortunately, as the iter_lines method returns a generator that lets you run through the lines; it doesn't let you retrieve a specific line. Likewise, iter_content wouldn't let you retrieve a specific video frame from an arbitrary location in a video file.