Need to see if your shared folder is taking up space on your dropbox 👨‍💻? Find out how to check here.

Forum Discussion

MrRog's avatar
MrRog
Explorer | Level 3
5 years ago

Beginner question, Powershell, File download

Sorry, but I have only just started using API. I have built a Powershell script (with help) where from my folder (DropBox file downloads)
Now I don't know if I did the token part correctly,

I am on App console created an App, (App folder) and then on
Generate Access token, (Access token expiration (no expiration)

1. i have taken the token, this works one time and one time not

2. then I went to https://dropbox.github.io/dropbox-api-v2-explorer/#files_download_zip and entered the API token and created Get Token

This worked a few times (connection from Powershell) and then one time not anymore.

I would like help because I'm not sure if I'm doing everything right here and if maybe the Powershell script is not so right.

I would also show my appreciation for the help in some way

Possibly you can also look at this directly with me TV, Anydesk, Skype etc
or someone can tell me where I can get help

Thanks for this and stay healthy
Rog

 

3 Replies

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    5 years ago

    I can't provide API support by remote desktop, phone, or chat, but I'll be happy to help here. I could use some more information about the issue you're seeing though. Can you share the following?

    • the steps to reproduce the issue, including relevant code snippet(s), but don't include any access token(s)
    • the full text of any error or unexpected output
  • MrRog's avatar
    MrRog
    Explorer | Level 3
    5 years ago

    Hello Greg-DB

    sure, this is all for testing, when I creating the API I use , App Folder API but someone say thats worng this key is only for 4 hour not more, but Full Dropbox I dont like full acces I need only share 1 Folder

    1. Creating a APP then click Generate Access Token, I copy this token and open rul, 

    https://dropbox.github.io/dropbox-api-v2-explorer/#files_download_zip

    Insert the Token that I have Copy and click Get Token, then this Token I insert to the PS Script $AuthToken

    This is working but as y say after 4 hour the Token dosent work anymore

     

    Hope this helps, and thx for helping me

     

    code

    function ParseErrorForResponseBody($Error) {
    if ($PSVersionTable.PSVersion.Major -lt 6) {
    if ($Error.Exception.Response) {
    $Reader = New-Object System.IO.StreamReader($Error.Exception.Response.GetResponseStream())
    $Reader.BaseStream.Position = 0
    $Reader.DiscardBufferedData()
    $ResponseBody = $Reader.ReadToEnd()
    if ($ResponseBody.StartsWith('{')) {
    $ResponseBody = $ResponseBody | ConvertFrom-Json
    }
    return $ResponseBody
    }
    }
    else {
    return $Error.ErrorDetails.Message
    }
    }
    function Get-DropboxFolder {

    [CmdletBinding()]
    Param(
    [string]$DropboxFolder,
    [string]$OutputFolder,
    [string]$FolderPath
    )
    $body = '{"path":"/'+$DropboxFolder+'"}'

    $AuthToken = 'xxxxxxxxxxxxx'
    $token = "Bearer $AuthToken"

    #The file will be downloaded as ZIP to the Temp folder of the user
    #$Tempfolder = "$env:temp\Downloaded_File.zip"
    $Tempfolder = "$env:HOMEDRIVE\temp\Downloaded_File.zip"

    if (!(Test-Path $FolderPath)){
    Write-Verbose "Downloading the folder..."
    try{
    Invoke-RestMethod `
    -Method POST `
    -Uri "https://content.dropboxapi.com/2/files/download_zip" `
    -Headers @{ "Authorization" = $token; "Dropbox-API-Arg" = $body} `
    -OutFile $Tempfolder -ContentType ""

    #The file will be extracted and then removed from the temp
    write-verbose "Extracting the folder..."
    Expand-Archive -LiteralPath $Tempfolder -DestinationPath $OutputFolder -Force
    write-verbose $OutputFolder
    write-verbose $Tempfolder
    Remove-Item $Tempfolder -Force
    Write-Verbose "The folder has been downloaded."
    }
    catch {
    ParseErrorForResponseBody($_)
    }

    }else{
    Write-Verbose "Folder Exists."
    }

    }

    $DropboxFolder = "vpin"
    $OutputFolder = "$env:HOMEDRIVE\temp\1-Downloads"
    $FolderPath = "$OutputFolder\vpin"

    Get-DropboxFolder -DropboxFolder $DropboxFolder -OutputFolder $OutputFolder -FolderPath $FolderPath -verbose

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    5 years ago

    It sounds like you actually retrieved an access token from two places:

    1. your app's page on the App Console, using the "Generate" button
    2. the API v2 Explorer, using the "Get Token" button

    You do not need to do both however. If you just need an access token for your own account and app, you can just use the first option. Your app's "Access token expiration" setting only applies to your own app, not the API v2 Explorer.

     

    It sounds like you then actually used the access token from the API v2 Explorer though, which would be short-lived, and so expires after four hours. (You can tell if an access token is short-lived by looking at the beginning of the string. If it begins with "sl.", it is short-lived.) If you need a long-lived access token for your own app, you should just use option 1 and copy that access token.

     

    In any case, if/when you get an error response from the API, make sure you print out the response body for error information.

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.

The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.

If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X, Facebook or Instagram.

For more info on available support options for your Dropbox plan, see this article.

If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!