Skip to main content

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? 


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



,

 

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

 

Bob



 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".



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

 



Hi, 

 

Anyone have any idea or sample code for this?



 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? 



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.



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


Reply