REST API in Alfresco Content Services 7.3 - using Powershell 5.1

cancel
Showing results for 
Search instead for 
Did you mean: 
Oscript
Partner

REST API in Alfresco Content Services 7.3 - using Powershell 5.1

Jump to solution

I have limited Alfresco knowledge but a lot of Opentext Content Server experience so in the main in Alfresco Content Services things feel familiar.

I installed Alfresco Content Services (ACS) using the instructions found here which allows for a quick start and should take no more than 30 minutes or so Smiley Happy

I was interested in getting a feel for the REST services but found one aspect a little confusing - the acquisition of a valid TICKET.  It seems that this should be obtained using the Authentication API - In powershell this looks as follows and works well so all good:

function Get-AlfrescoAuthToken {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)]
        [string]$BaseUrl,
        [Parameter(Mandatory=$true)]
        [string]$UserId,
        [Parameter(Mandatory=$true)]
        [SecureString]$Password
    )

    $url = "$BaseUrl/alfresco/api/-default-/public/authentication/versions/1/tickets"
    $body = @{
        userId = $UserId
        password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Password))
    } | ConvertTo-Json

    try {
        $response = Invoke-RestMethod -Uri $url -Method Post -Body $body -ContentType 'application/json'
        return $response.entry.id
    }
    catch {
        Write-Error $_.Exception.Message
    }
}

$password = ConvertTo-SecureString '<INSERT_YOUR_PASSWORD>' -AsPlainText -Force
$token = Get-AlfrescoAuthToken -BaseUrl 'http://192.168.56.100' -UserId '<INSERT_YOUR_USER_ID>' -Password $password
Write-Host "Authentication token: $token"

What I wasn't able to successfully do however was to use this ticket in subsequent calls although surprisingly I am able to authenticate in Powershell using the following code:

$username = "INSERT_USERNAME"
$password = "INSERT_PASSWORD"
$alfrescoUrl = "http://192.168.56.100/alfresco/api/-default-/public/alfresco/versions/1/nodes/-my-/children?skipCount=0&maxItems=100"

$basicAuth = "{0}:{1}" -f $username,$password
$basicAuthEncoded = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($basicAuth))

$headers = @{
    "Authorization" = "Basic $basicAuthEncoded"
}

# actually make the call
$response = Invoke-RestMethod -Uri $alfrescoUrl -Headers $headers -Method Get

$response.list.entries.entry | Format-Table -Property name, id, isFolder, isFile, createdAt, createdByUser, modifiedAt, modifiedByUser

Can anyone provide an explanation please - perhaps the latter method is acceptable but equally perhaps I have misunderstood things ?

Many thanks in advance,

(Powershell version 5.1 used in examples)

 

1 Solution

Accepted Solutions
Oscript
Partner

Re: REST API in Alfresco Content Services 7.3 - using Powershell 5.1

Jump to solution

Answered my own question........

Lesson being - read the documentation !!

NOTE: Once the ticket has been obtained it is necessary to base64 encode it !!!!

Here is the complete working example using Powershell 5.1:

function Get-AlfrescoAuthToken {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)]
        [string]$BaseUrl,
        [Parameter(Mandatory=$true)]
        [string]$UserId,
        [Parameter(Mandatory=$true)]
        [SecureString]$Password
    )

    $url = "$BaseUrl/alfresco/api/-default-/public/authentication/versions/1/tickets"
    $body = @{
        userId = $UserId
        password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Password))
    } | ConvertTo-Json

    try {
        $response = Invoke-RestMethod -Uri $url -Method Post -Body $body -ContentType 'application/json'
        
        # need to base64 encode !!
        $encodedTicket = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($response.entry.id))
        return $encodedTicket
    }
    catch {
        Write-Error $_.Exception.Message
    }
}

Clear-Host

if($PSVersionTable.PSVersion.Major -ge 5 -and $PSVersionTable.PSVersion.Minor -ge 1) {
    Write-Host "PowerShell version is 5.1 or higher."
}
else {
    Write-Host "PowerShell version is lower than 5.1. Please upgrade to a higher version."
}

$baseUrl = 'http://192.168.56.100'

$password = ConvertTo-SecureString '<INSERT_YOUR_PASSWORD_HERE>' -AsPlainText -Force
$basicAuthEncoded = Get-AlfrescoAuthToken -BaseUrl $baseUrl -UserId 'admin' -Password $password
Write-Host "Authentication token: $basicAuthEncoded"

#############################################################################
# Lets actually try and retrieve something using the Base64 encoded ticket !
#############################################################################


$nodeUrl = "$baseUrl/alfresco/api/-default-/public/alfresco/versions/1/nodes/-my-/children?skipCount=0&maxItems=100"
$headers = @{ "Authorization" = "Basic $basicAuthEncoded" }

$response = Invoke-RestMethod -Uri $nodeUrl -Method Get -Headers $headers

# Process the response as needed
$response.list.entries.entry | Format-Table




View solution in original post

1 Reply
Oscript
Partner

Re: REST API in Alfresco Content Services 7.3 - using Powershell 5.1

Jump to solution

Answered my own question........

Lesson being - read the documentation !!

NOTE: Once the ticket has been obtained it is necessary to base64 encode it !!!!

Here is the complete working example using Powershell 5.1:

function Get-AlfrescoAuthToken {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)]
        [string]$BaseUrl,
        [Parameter(Mandatory=$true)]
        [string]$UserId,
        [Parameter(Mandatory=$true)]
        [SecureString]$Password
    )

    $url = "$BaseUrl/alfresco/api/-default-/public/authentication/versions/1/tickets"
    $body = @{
        userId = $UserId
        password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Password))
    } | ConvertTo-Json

    try {
        $response = Invoke-RestMethod -Uri $url -Method Post -Body $body -ContentType 'application/json'
        
        # need to base64 encode !!
        $encodedTicket = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($response.entry.id))
        return $encodedTicket
    }
    catch {
        Write-Error $_.Exception.Message
    }
}

Clear-Host

if($PSVersionTable.PSVersion.Major -ge 5 -and $PSVersionTable.PSVersion.Minor -ge 1) {
    Write-Host "PowerShell version is 5.1 or higher."
}
else {
    Write-Host "PowerShell version is lower than 5.1. Please upgrade to a higher version."
}

$baseUrl = 'http://192.168.56.100'

$password = ConvertTo-SecureString '<INSERT_YOUR_PASSWORD_HERE>' -AsPlainText -Force
$basicAuthEncoded = Get-AlfrescoAuthToken -BaseUrl $baseUrl -UserId 'admin' -Password $password
Write-Host "Authentication token: $basicAuthEncoded"

#############################################################################
# Lets actually try and retrieve something using the Base64 encoded ticket !
#############################################################################


$nodeUrl = "$baseUrl/alfresco/api/-default-/public/alfresco/versions/1/nodes/-my-/children?skipCount=0&maxItems=100"
$headers = @{ "Authorization" = "Basic $basicAuthEncoded" }

$response = Invoke-RestMethod -Uri $nodeUrl -Method Get -Headers $headers

# Process the response as needed
$response.list.entries.entry | Format-Table