1

I need to fetch the data of item with API - https://doc.sitecore.com/xp/en/developers/90/sitecore-experience-manager/the-restful-api-for-the-itemservice.html

I am trying this with PowerShell and tried to login like below:

    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    
    $url = 'https://sc102sc.dev.local/sitecore/api/ssc/auth/login'
    
    $password = 'b'
    
    $Body = @{
        'Domain'='sitecore'
        'UserName' = 'jagmeet'
        'Password' = $password
    } | ConvertTo-Json
    
    $LoginResponse = Invoke-WebRequest -Uri  $url -Method Post -Headers @{"Content-Type" = "application/x-www-form-urlencoded"} `
    -Body $Body -SessionVariable 'Session'
    
    $Session
    
    $LoginResponse

but every time, it is throwing an error and not allowing to login:

enter image description here

In the logs, it says below:

enter image description here

can anyone assist me with this?

2 Answers 2

1

I've used this script to fetch the data of items with API and works well. Try this script.

$url = 'https://yourserver/sitecore/api/ssc/auth/login'

$Body = @{
    domain = 'sitecore'
    userName = 'admin'
    password = 'b'
}

$json = $Body | convertto-json

Invoke-WebRequest -Uri $url -ContentType 'application/json' -Method 'POST' -Body $json -usebasicparsing -sessionvariable mysession


$jsonContent = Invoke-WebRequest 'https://yourserver/sitecore/api/ssc/item/8792E70C-E8F1-475C-9AA9-FA57228C8EA1' -WebSession $mysession -METHOD 'GET' -usebasicparsing
$jsonContent.content | convertfrom-json

Hope it helps!

1
  • So no need to login before running. Thats awesome. Thanks Jun 15, 2023 at 18:25
1

I think it's because you convert your $Body object to a JSON string which doesn't conform to the Content-Type you state that you're sending. Getting rid of that conversion works for me.

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

$url = 'https://sc102sc.dev.local/sitecore/api/ssc/auth/login'

$password = 'b'

$Body = @{
    'Domain'='sitecore'
    'UserName' = 'jagmeet'
    'Password' = $password
}# | ConvertTo-Json

$LoginResponse = Invoke-WebRequest -Uri  $url -Method Post -Headers @{"Content-Type" = "application/x-www-form-urlencoded"} `
-Body $Body -SessionVariable 'Session'

$Session

$LoginResponse
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.