Saya mendapatkan skrip ini dengan Python dan saya ingin mengubahnya menjadi PowerShell:

method = "GET"
URL = "PeRT RESTful API URL"
contentType = ""
publicAccessKey = "Your Public Access Key"
date = datetime.now().strftime("%a, %d %b %Y %X +0000")
cotentMd5 = ""
secretAccessKey = "Your Secret Access Key"

# URL used to create signature should not include parameters!!!
stringToSign = method + "\n" + url + "\n" + contentType + "\n" + publicAccessKey + "\n" + date + "\n" + cotentMd5
#print(stringToSign)
message = bytes(stringToSign).encode('utf-8')
secret = bytes(secretAccessKey).encode('utf-8')
signature = base64.b64encode(hmac.new(secret, message, digestmod=hashlib.sha256).digest())

headers = {
    "Authorization": publicAccessKey + ":" + signature,
    "Date" : date
}

response = requests.get(url, headers=headers)

Ini yang saya tulis:

$URL = "PeRT RESTful API URL"
$hmacsha = New-Object System.Security.Cryptography.HMACSHA256
publicAccessKey = "Your Public Access Key"
secretAccessKey = "Your Secret Access Key"
$date= Get-Date -Format "ddd, dd MMM yyyy HH:mm:ss +0000"
$stringToSign= "GET" + "`n" + $baseURL + "`n" + "" + "`n" + $publicAccessKey + "`n" + $date + "`n" + ""

$hmacsha.key= [System.Text.Encoding]::ASCII.GetBytes($secretAccessKey)
$signature = $hmacsha.ComputeHash([Text.Encoding]::ASCII.GetBytes($stringToSign))
$signature = [Convert]::ToBase64String($signature)

$headers = @{ "Authorization" = $publicAccessKey + ":" + $signature ; "Date" = $date }

$response = Invoke-RestMethod -Uri $URL -Headers $headers

Mendapatkan "HTTP Status 401 - ID Klien atau kata sandi tidak valid."

Adakah yang bisa melihat masalahnya?

0
Roi Shidlovsky 5 Mei 2020, 00:13

1 menjawab

Jawaban Terbaik

Python menggunakan UTF-8 sementara PowerShell menggunakan ASCII

Coba [system.Text.Encoding]::UTF8 alih-alih [System.Text.Encoding]::ASCII dalam kode Anda

1
Evyatar 4 Mei 2020, 21:48