Level up your Box knowledge with brand new learning paths on Box University. Visit training.box.com to get started

how to use AS-user in powershell

Answered
New post

Comments

8 comments

  • Froziez

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

    0
    Comment actions Permalink
  • BobFlynn-IU

    ,

     

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

     

    Bob

    0
    Comment actions Permalink
  • Murtza

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

    0
    Comment actions Permalink
  • Froziez

    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

     

    0
    Comment actions Permalink
  • Froziez

    Hi, 

     

    Anyone have any idea or sample code for this?

    0
    Comment actions Permalink
  • Murtza

     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? 

    0
    Comment actions Permalink
  • Murtza

    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.

    0
    Comment actions Permalink
  • bdudley

    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
    0
    Comment actions Permalink

Please sign in to leave a comment.