cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to learn some quick and useful tips to make your day easier? Check out how Calvin uses Replay to get feedback from other teams at Dropbox here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Upload Files Function Powershell - Bad Request

Upload Files Function Powershell - Bad Request

PDanos
New member | Level 2
Go to solution

I'm trying to upload files using Powershell to a folder inside our company Dropbox. I'm using a function I googled somewhere (I don't have the URL at hand, sorry about that):

 

function Upload-FileToDropbox { 

Param(
    [Parameter(Mandatory=$true)]
    [string]$SourceFilePath,
    [Parameter(Mandatory=$true)]
    [string]$TargetFilePath,
    [Parameter(Mandatory=$true)]
    [string]$DropBoxAccessToken
)

    $arg = '{ "path": "' + $TargetFilePath + '", "mode": "add", "autorename": true, "mute": false }'
    $authorization = "Bearer $DropBoxAccessToken"

    $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
    $headers.Add("Authorization", $authorization)
    $headers.Add("Dropbox-API-Arg", $arg)
    $headers.Add("Content-Type", 'application/octet-stream')
     
    Invoke-RestMethod -Uri https://content.dropboxapi.com/2/files/upload -Method Post -InFile $SourceFilePath -Headers $headers
}

For this script, I've created a new team app with Permission type Team member file access, and requested an access token. For the parameters in this function, I'm using something like this:

 

$SourceFilePath = "C:\TEMP\test.txt"
$TargetFilePath = "/IT/test.txt"

However, when I try to upload a file, I'm getting the following error:

Invoke-RestMethod : The remote server returned an error: (400) Bad Request.

I tried running the Invoke-RestMethod manually, creating the headers by hand, but I get the same error over and over again. What am I missing?

 

Thank you for the help!

1 Accepted Solution

Accepted Solutions

Greg-DB
Dropbox Staff
Go to solution

Whenever debugging issues like this (or to retrieve the result, e.g., the file metadata, etc., even in the case of success) be sure to read the response body. It will contain more information, such as a specific error message.

 

Based on your description though, it looks like the issue is that you're using a Business app with the team member file access feature without specifying the 'Dropbox-API-Select-User' header, which identifies which member of the Business team to operate on. 

 

So, you should either add that header with the relevant member ID, or, if you only need to upload to a specific account anyway, register a different app with the full Dropbox or app folder permission instead. Those aren't Business apps, and just operate on a single account instead of the whole team. They can still be linked to accounts on Business teams though.

View solution in original post

4 Replies 4

Greg-DB
Dropbox Staff
Go to solution

Whenever debugging issues like this (or to retrieve the result, e.g., the file metadata, etc., even in the case of success) be sure to read the response body. It will contain more information, such as a specific error message.

 

Based on your description though, it looks like the issue is that you're using a Business app with the team member file access feature without specifying the 'Dropbox-API-Select-User' header, which identifies which member of the Business team to operate on. 

 

So, you should either add that header with the relevant member ID, or, if you only need to upload to a specific account anyway, register a different app with the full Dropbox or app folder permission instead. Those aren't Business apps, and just operate on a single account instead of the whole team. They can still be linked to accounts on Business teams though.

PDanos
New member | Level 2
Go to solution

Hi Greg,

Thank you so much for your help. I seemed to overlook completely the Dropbox-API-Select-User header in the documentation. Before digging into finding the memberID, I chose to follow your suggestion on registering a new app with Full Dropbox permissions, and it did the trick. I was able to upload with no problems.

Also, it's good to know the code was good, so i don't have to change anything on that end.

Thanks again!

 

PS: I found the original link to the code I used for anyone else that wants to use it, and also, to give proper credit: http://laurentkempe.com/2016/04/07/Upload-files-to-DropBox-from-PowerShell/

Danut
New member | Level 2
Go to solution

Maybe something changed. When trying the above script, I get the following error:

 

Invoke-RestMethod : {"error_summary": "missing_scope/..", "error": {".tag": "missing_scope", "required_scope": "files.content.write"}}
At line:20 char:5
+ Invoke-RestMethod -Uri https://content.dropboxapi.com/2/files/upl ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

 

 

Could this be related with using TLS 1.2?

Greg-DB
Dropbox Staff
Go to solution

@Danut A TLS issue would fail earlier in the connection process, with a different kind of error. A 'missing_scope' error indicates that while the app is permitted to use that scope, the particular access token you're using to make the API call does not have that scope granted. Also, be aware that just adding a scope to your app via the App Console does not retroactively grant that scope to existing access tokens.

That being the case, to make any API calls that require that scope, you'll need to get a new access token with that scope.

Refer to the OAuth Guide and authorization documentation for more information. 

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    Greg-DB Dropbox Staff
  • User avatar
    Danut New member | Level 2
  • User avatar
    PDanos New member | Level 2
What do Dropbox user levels mean?