Skip to main content

we need help with a powershell script accessing acronis' API v2

Thread needs solution
Beginner
Posts: 1
Comments: 0

can anyone tell me why i keep getting 401 errors saying i'm unauthorized?
i've removed the variable definitions    


 [code]
    $site = "https://us5-cloud.acronis.com"
    $api = "$site/api/2"
    $headers = @{}
    $headers.Add("Authorization", "Basic")
    $headers.add("username" , '$APIUser')
    $headers.add('password' ,'$APIPassword')

    $userCreateArgs = @{
        name = $customer_full_name
        parentid = $parentid
        kind = $kind
        language = "en"
        customer_id = $customer_id
        contact = {$contactCreatedArgs}
        } | ConvertTo-Json

    $contactCreateArgs = @{
        email = $customer_email
        address1 = $customer_billing_address
        address2 = $customer_billing_address_2
        country = "USA"
        state = $customer_billing_state
        zipcode = $customer_billing_zip
        city = $customer_billing_city
        phone = $customer_phone
        lastname = $contact_last_name
        firstname = $contact_first_name
        }

add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
    public bool CheckValidationResult(
        ServicePoint srvPoint, X509Certificate certificate,
        WebRequest request, int certificateProblem) {
        return true;
    }
}
"@
$AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'
[System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
Invoke-RestMethod "$api/tenants" -Headers $headers -Method Post -ContentType "application/json" -Body $userCreateArgs

[/code]

0 Users found this helpful
Support Engineer
Posts: 0
Comments: 57

#1

hi Michael!

 

My only assumption is that either the login proedure fails or the login name that you use in $APIUser is not suitable for your calls.

Can you describe what you are trying to implement overall?

We can't do much in terms of helping you program entire thing, this should involve professional services, but if I can help, I will!

Beginner
Posts: 0
Comments: 1

#2

For reference, see https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication#Basic_authentication_scheme

You should add one header only:

$APIUser = 'someuser'
$APIPassword = 'somepassword'
$Credentials = "${APIUser}:${APIPassword}"
$Bytes = [System.Text.Encoding]::Unicode.GetBytes($Credentials)
$EncodedCredentials = [Convert]::ToBase64String($Bytes)

$headers = @{}
$headers.Add("Authorization", "Basic ${EncodedCredentials}")

For example:

PS C:\Users\alexander.yakovlev> $APIUser = 'someuser'
PS C:\Users\alexander.yakovlev> $APIPassword = 'somepassword'
PS C:\Users\alexander.yakovlev> $Credentials = "${APIUser}:${APIPassword}"
PS C:\Users\alexander.yakovlev> $Bytes = [System.Text.Encoding]::Unicode.GetBytes($Credentials)
PS C:\Users\alexander.yakovlev> $EncodedCredentials = [Convert]::ToBase64String($Bytes)
PS C:\Users\alexander.yakovlev> $headers = @{}
PS C:\Users\alexander.yakovlev> $headers.Add("Authorization", "Basic ${EncodedCredentials}")
PS C:\Users\alexander.yakovlev> $headers

Name                           Value
----                           -----
Authorization                  Basic cwBvAG0AZQB1AHMAZQByADoAcwBvAG0AZQBwAGEAcwBzAHcAbwByAGQA
Beginner
Posts: 1
Comments: 7

#3

Dear Michael,

If you continue have some questions regarding the authorization process and others APIs usage you can find detailed documentation at Acronis Developer Network portal https://developer.acronis.com/doc/