0

Code to create a new user:

function New-DropBoxUser {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)] 
        [string]$FullName
    )

    # Split name into username #
    $FirstName, $LastName = $FullName.split(' ')
    $UserName = ($FirstName[0]+$LastName).toLower()
    $email = "[email protected]"

    $Body = @{
        "new_members" = @(@{
            "member_email" = $email;
            "member_given_name" = $FirstName;                        
            "member_surname" = $LastName;
            "send_welcome_email" = "true";
            "role" = @{
                ".tag" = "member_only"
            }
        })
    }

    Write-Host ''
    Write-Host 'Creating DropBox User...' -ForegroundColor 'Yellow' -BackgroundColor 'Black'
    $AuthToken = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
    $token = "Bearer $AuthToken" 

    $response = Invoke-RestMethod `
        -Method Post `
        -Uri "https://api.dropboxapi.com/2/team/members/add" `
        -Headers @{ Authorization = $token } `
        -ContentType "application/json; charset=utf-8" `
        -Body (ConvertTo-Json $Body)
}

Output:

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

Anyone, please tell me the fix. I tried the Endpoint "team/members/list" to fetch user list using the same $token and it works. I am totally new to Dropbox API so my code is borrowed from Google, while being good with PowerShell I tried to patch up using many different codes but none worked.

11
  • 2
    The error message may contain hints as to what you're doing wrong. I'd suggest you capture the error in a variable by adding -ErrorVariable irmError to the Invoke-RestMethod call and then hava look at $irmError afterwards Commented Aug 26, 2018 at 19:33
  • @MathiasR.Jessen, I tried the -ErrorVariable but the variable has no data. Error not captured by this. Commented Aug 26, 2018 at 20:44
  • Invoke-RestMethod ` -Method Post ` -Uri "api.dropboxapi.com/2/team/members/add" ` -Headers @{ Authorization = $token } ` -ContentType "application/json; charset=utf-8" ` -Body (ConvertTo-Json $Body) ` -ErrorVariable $ero Write-Host $ero Commented Aug 26, 2018 at 20:44
  • Tried Try catch also to catch the exception, Commented Aug 26, 2018 at 20:51
  • try { Invoke-RestMethod ` -Method Post ` -Uri "api.dropboxapi.com/2/team/members/add" ` -Headers @{ Authorization = $token } ` -ContentType "application/json; charset=utf-8" ` -Body (ConvertTo-Json $Body) ` -ErrorVariable $ero ` -ErrorAction Stop Write-Host $ero } catch {$ErrorMessage = $_.Exception.Message Write-Host $ErrorMessage} Commented Aug 26, 2018 at 20:51

1 Answer 1

0

When all else fails, RTFM, whose parameters say:

{
    "new_members": [
        {
            "member_email": "[email protected]",
            "member_given_name": "Tom",
            "member_surname": "Silverstone",
            "member_external_id": "company_id:342432",
            "send_welcome_email": true,
            "role": "member_only"
        }
    ],
    "force_async": false
}

Try removing .tag from the role definition and replace "true" with $true

$Body = @{
    "new_members" = @(@{
        "member_email" = $email
        "member_given_name" = $FirstName                        
        "member_surname" = $LastName
        "send_welcome_email" = $true
        "role" = "member_only"
    })
}
3
  • Edited in the $true for the native powershell boolean type.
    – veefu
    Commented Aug 27, 2018 at 8:09
  • Already tried the "send_welcome_email = $true but the code was failing, but after changing the role and removing the.tag it works. Commented Aug 27, 2018 at 12:26
  • I believe $true is the correct syntax. The generated JSON is : true in that case, whereas "true" yielded : "true"
    – veefu
    Commented Aug 27, 2018 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.