Skip to main content
Question

how to use AS-user in powershell

  • May 21, 2025
  • 8 replies
  • 42 views

Forum|alt.badge.img

Hi,

 

Anyone know how to use "AS-User" in powershell? 

 

i tried to set it in header but it is giving me 403 error (using co-admin to get user's folder)

 

Anyone got sample? 

8 replies

Forum|alt.badge.img

For more info, i would like to get folder detail/create/move folder of user's root (0) folder


Forum|alt.badge.img

,

 

You would have more luck posting this to the Developer Forum.

 

Bob


Forum|alt.badge.img

 This might be a settings error. Did you enable the As-User header permission in the developer console?

 

The As-User header permission is listed under Advanced Features in the developer console. The specific permission needed is "Perform actions on behalf of users".


Forum|alt.badge.img

Hi Murtza,

 

Yes, i did both enable AS-User (by box) and checked on  "Perform actions on behalf of users"

what error i got now is "Invoke-RestMethod : The remote server returned an error: (400) Bad Request"

 

this is my code below... anything i missed?

 

function GetUserfolder(){

$connecturl = Invoke-RestMethod "https://api.box.com/2.0/folders/0?fields=item_collection,name" -Method Get -Headers @{"Authorization" = "Bearer $accessToken" ; "as-user" = "userID"}
#echo $connecturl.entries.name
#echo $connecturl.entries.id

}

 

*the token is working fine... i can view my own folder

 


Forum|alt.badge.img

Hi, 

 

Anyone have any idea or sample code for this?


Forum|alt.badge.img

 I am not very familiar with Powershell, but maybe it is a syntax error with the format of the headers. Can you please post the HTTP request your code generates? 


Forum|alt.badge.img

Here is a project from GitHub that calls the Box API using Powershell. I don't think the as-user header feature is implemented in this project, but looking at some of the individual method implementations might be helpful.


Forum|alt.badge.img

Here is a working sample of a function to create a folder with as-user:

function New-BoxFolder($token, $foldername, $parentfolderid, $asuser)
{
#if no parentfolderid, set to 0... create at the root
$uri = "https://api.box.com/2.0/folders"
$headers = @{}
if($asuser)
{

$headers.Add("Authorization","Bearer $token")
$headers.Add("As-User","$asuser")
}
else
{
$headers.Add("Authorization","Bearer $token")
}
if(!$parentfolderid){$parentfolderid = "0"}
$json = '{"name": "' + $foldername + '", "parent": {"id": "'+ $parentFolderID + '"}}'
$return = Invoke-RestMethod -Uri $uri -Method Post -Headers $headers -Body $json -ContentType "application/x-www-form-urlencoded"
$return
}

New-BoxFolder -token $token -name "Test Folder" -asuser 123456