1
我已經嘗試了一切,但Amazon Load Balancer API不會接受我的簽名。AWS API - 不會接受我的簽名
無論我嘗試什麼,我都會得到一個「403 SignatureDoesNotMatch」響應。我可能錯過了一些顯而易見的東西,但花了好幾個小時的時間試圖弄清楚那是什麼,我正處在我的束縛之中。請幫忙!!謝謝。
這裏是我的代碼:
Public Sub DeregisterInstanceFromLoadBalance(ByVal strServerID As String)
Dim strURL As String
strURL = "http://elasticloadbalancing.amazonaws.com/?"
Dim strTimestamp As String = PercentEncodeRfc3986(DateTime.UtcNow.ToString("yyyy-MM-dd'T'HH:mm:ss'Z'"))
Dim strParams As String
strParams = "AWSAccessKeyId=<MY_API_KEY>" & _
"&Action=DeregisterInstancesFromLoadBalancer" & _
"&Instances.member.N=" & strServerID & _
"&LoadBalancerName=ATTB" & _
"&SignatureMethod=HmacSHA256" & _
"&SignatureVersion=2" & _
"&Timestamp=" & strTimestamp & _
"&Version=2009-05-15"
Dim strStringToSign As String = "GET\nelasticloadbalancing.amazonaws.com\n/\n" & strParams
strURL = strURL & strParams & "&Signature=" & PercentEncodeRfc3986(HashString(strStringToSign))
Dim wc As New WebClient()
Dim strResponse As String
strResponse = wc.DownloadString(strURL)
End Sub
Private Const PRIVATE_KEY As String = "<MY_PRIVATE_KEY>"
Private Function HashString(ByVal StringToHash As String) As String
Dim Key() As Byte = Encoding.UTF8.GetBytes(PRIVATE_KEY)
Dim XML() As Byte = Encoding.UTF8.GetBytes(StringToHash)
Dim myHMACSHA256 As New System.Security.Cryptography.HMACSHA256(Key)
Dim HashCode As Byte() = myHMACSHA256.ComputeHash(XML)
Return Convert.ToBase64String(HashCode)
End Function
Private Function PercentEncodeRfc3986(ByVal str As String) As String
str = HttpUtility.UrlEncode(str, System.Text.Encoding.UTF8)
str = str.Replace("'", "%27").Replace("(", "%28").Replace(")", "%29").Replace("*", "%2A").Replace("!", "%21").Replace("%7e", "~").Replace("+", "%20").Replace("%7E", "~")
Dim sbuilder As StringBuilder = New StringBuilder(str)
For i As Int32 = 0 To sbuilder.Length - 1
If sbuilder(i) = "%" Then
If (Char.IsLetter(sbuilder(i + 1)) OrElse Char.IsLetter(sbuilder(i + 2))) Then
sbuilder(i + 1) = Char.ToUpper(sbuilder(i + 1))
sbuilder(i + 2) = Char.ToUpper(sbuilder(i + 2))
End If
End If
Next
Return sbuilder.ToString()
End Function