Hi, All
I cannot debug this positing :-(
For an API for POST requests i must follow the rules:
1) make MD5 hash of the parameters line (like "param1=1¶m2=2") plus a secret word. This hash sub works correct for sure (GET requests without parameters are OK).
2) but this hash is sent not by one more parameter, but by the request header "Signature".
3) URLencoding i do only in the single point, before calculating the parameters' hash and making GET URL.
Why the signature is always incorrect at the server side check ?
I cannot debug this positing :-(
For an API for POST requests i must follow the rules:
1) make MD5 hash of the parameters line (like "param1=1¶m2=2") plus a secret word. This hash sub works correct for sure (GET requests without parameters are OK).
2) but this hash is sent not by one more parameter, but by the request header "Signature".
3) URLencoding i do only in the single point, before calculating the parameters' hash and making GET URL.
B4X:
Private Sub Requests(GetReq As Boolean, NewJob As HttpJob, Params As Map, Files As List)
If Starter.InternetConnected = False Then Return
Dim j As HttpJob = NewJob
If Params.IsInitialized Then
If Params.Size > 0 Then
For i = 0 To Params.Size - 1
Params.Put(Params.GetKeyAt(i), su.EncodeUrl(Params.GetValueAt(i), "UTF8"))
Next
End If
End If
Dim U As String = Get_URL(j.JobName, GetReq, Params) 'URL-encoded parameters line with URL for GET requests only
'------------
If GetReq Then
j.Download(U)
Else
j.PostMultipart(U, Params, Files)
End If
j.GetRequest.SetContentEncoding("application/x-www-form-urlencoded")
Add_Signature(j, Params)
j.GetRequest.Timeout = 60000
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
....
End sub
Sub Add_Signature (j As HttpJob, m As Map)
Dim key As String = "secret_word"
If m.IsInitialized = False Or m.Size = 0 Then
Dim pars As String = ""
Else
Dim pars As String 'just URL-encoded parameters line without URL
For i = 0 To m.Size - 1
pars = pars & "&" & m.GetKeyAt(i) & "=" & m.GetValueAt(i)
Next
pars = pars.SubString(1)
Log(pars)
End If
Dim hash As String = others.GetMD5hash(pars & key)
j.GetRequest.SetHeader("Signature", hash)
End Sub
Private Sub Get_URL(method As String, get As Boolean, m As Map) As String
Dim URL1, URL2 As String
If m.IsInitialized = False Or m.Size = 0 Then 'no parameters
URL1 = URL & method
else If get = False Then 'for POST
URL1 = URL & method
Else
'for GET with parameters
For i = 1 To m.Size - 1
URL2 = URL2 & "&" & m.GetKeyAt(i) & "=" & m.GetValueAt(i)
Next
URL1 = URL & method & "?" & m.GetKeyAt(0) & "=" & m.GetValueAt(0)
If URL2 <> "" Then
URL1 = URL1 & URL2
End If
End If
Return URL1
End Sub
Why the signature is always incorrect at the server side check ?
Last edited: