<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic REST API in Alfresco Content Services 7.3 - using Powershell 5.1 in Alfresco Forum</title>
    <link>https://connect.hyland.com/t5/alfresco-forum/rest-api-in-alfresco-content-services-7-3-using-powershell-5-1/m-p/144115#M38257</link>
    <description>&lt;P&gt;I have limited Alfresco knowledge but a lot of Opentext Content Server experience so in the main in Alfresco Content Services things feel familiar.&lt;BR /&gt;&lt;BR /&gt;I installed Alfresco Content Services (ACS) using the instructions found &lt;A href="https://github.com/Alfresco/alfresco-ansible-deployment/blob/master/docs/deployment-guide.md" target="_blank" rel="noopener nofollow noreferrer"&gt;here&lt;/A&gt;&amp;nbsp;which allows for a quick start and should take no more than 30 minutes or so&amp;nbsp;&lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://connect.hyland.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;&lt;BR /&gt;&lt;BR /&gt;I was interested in getting a feel for the REST services but found one aspect a little confusing - the acquisition of a valid TICKET.&amp;nbsp; It seems that this should be obtained using the &lt;A href="http://192.168.56.100/alfresco/api/-default-/public/authentication/versions/1/tickets" target="_blank" rel="noopener nofollow noreferrer"&gt;Authentication API&lt;/A&gt; - In powershell this looks as follows and works well so all good:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;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 '&amp;lt;INSERT_YOUR_PASSWORD&amp;gt;' -AsPlainText -Force
$token = Get-AlfrescoAuthToken -BaseUrl 'http://192.168.56.100' -UserId '&amp;lt;INSERT_YOUR_USER_ID&amp;gt;' -Password $password
Write-Host "Authentication token: $token"&lt;/PRE&gt;&lt;P&gt;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:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;$username = "INSERT_USERNAME"
$password = "INSERT_PASSWORD"
$alfrescoUrl = "http://192.168.56.100/alfresco/api/-default-/public/alfresco/versions/1/nodes/-my-/children?skipCount=0&amp;amp;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&lt;/PRE&gt;&lt;P&gt;Can anyone provide an explanation please - perhaps the latter method is acceptable but equally perhaps I have misunderstood things ?&lt;BR /&gt;&lt;BR /&gt;Many thanks in advance,&lt;BR /&gt;&lt;BR /&gt;(Powershell version 5.1 used in examples)&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 23 Mar 2023 16:23:45 GMT</pubDate>
    <dc:creator>Oscript</dc:creator>
    <dc:date>2023-03-23T16:23:45Z</dc:date>
    <item>
      <title>REST API in Alfresco Content Services 7.3 - using Powershell 5.1</title>
      <link>https://connect.hyland.com/t5/alfresco-forum/rest-api-in-alfresco-content-services-7-3-using-powershell-5-1/m-p/144115#M38257</link>
      <description>&lt;P&gt;I have limited Alfresco knowledge but a lot of Opentext Content Server experience so in the main in Alfresco Content Services things feel familiar.&lt;BR /&gt;&lt;BR /&gt;I installed Alfresco Content Services (ACS) using the instructions found &lt;A href="https://github.com/Alfresco/alfresco-ansible-deployment/blob/master/docs/deployment-guide.md" target="_blank" rel="noopener nofollow noreferrer"&gt;here&lt;/A&gt;&amp;nbsp;which allows for a quick start and should take no more than 30 minutes or so&amp;nbsp;&lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://connect.hyland.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;&lt;BR /&gt;&lt;BR /&gt;I was interested in getting a feel for the REST services but found one aspect a little confusing - the acquisition of a valid TICKET.&amp;nbsp; It seems that this should be obtained using the &lt;A href="http://192.168.56.100/alfresco/api/-default-/public/authentication/versions/1/tickets" target="_blank" rel="noopener nofollow noreferrer"&gt;Authentication API&lt;/A&gt; - In powershell this looks as follows and works well so all good:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;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 '&amp;lt;INSERT_YOUR_PASSWORD&amp;gt;' -AsPlainText -Force
$token = Get-AlfrescoAuthToken -BaseUrl 'http://192.168.56.100' -UserId '&amp;lt;INSERT_YOUR_USER_ID&amp;gt;' -Password $password
Write-Host "Authentication token: $token"&lt;/PRE&gt;&lt;P&gt;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:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;$username = "INSERT_USERNAME"
$password = "INSERT_PASSWORD"
$alfrescoUrl = "http://192.168.56.100/alfresco/api/-default-/public/alfresco/versions/1/nodes/-my-/children?skipCount=0&amp;amp;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&lt;/PRE&gt;&lt;P&gt;Can anyone provide an explanation please - perhaps the latter method is acceptable but equally perhaps I have misunderstood things ?&lt;BR /&gt;&lt;BR /&gt;Many thanks in advance,&lt;BR /&gt;&lt;BR /&gt;(Powershell version 5.1 used in examples)&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Mar 2023 16:23:45 GMT</pubDate>
      <guid>https://connect.hyland.com/t5/alfresco-forum/rest-api-in-alfresco-content-services-7-3-using-powershell-5-1/m-p/144115#M38257</guid>
      <dc:creator>Oscript</dc:creator>
      <dc:date>2023-03-23T16:23:45Z</dc:date>
    </item>
    <item>
      <title>Re: REST API in Alfresco Content Services 7.3 - using Powershell 5.1</title>
      <link>https://connect.hyland.com/t5/alfresco-forum/rest-api-in-alfresco-content-services-7-3-using-powershell-5-1/m-p/144116#M38258</link>
      <description>&lt;P&gt;Answered my own question........&lt;BR /&gt;&lt;BR /&gt;Lesson being - read the documentation !!&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;NOTE: Once the ticket has been obtained it is necessary to base64 encode it !!!!&lt;/STRONG&gt;&lt;BR /&gt;&lt;BR /&gt;Here is the complete working example using Powershell 5.1:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;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'
        
&lt;EM&gt;&lt;STRONG&gt;        # need to base64 encode !!
        $encodedTicket = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($response.entry.id))&lt;/STRONG&gt;&lt;/EM&gt;
        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 '&amp;lt;INSERT_YOUR_PASSWORD_HERE&amp;gt;' -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&amp;amp;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

&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Mar 2023 08:22:02 GMT</pubDate>
      <guid>https://connect.hyland.com/t5/alfresco-forum/rest-api-in-alfresco-content-services-7-3-using-powershell-5-1/m-p/144116#M38258</guid>
      <dc:creator>Oscript</dc:creator>
      <dc:date>2023-03-24T08:22:02Z</dc:date>
    </item>
  </channel>
</rss>

