The ETRADE Developer Platform uses the OAuth authorization protocol, version 1.0a. The eTrade developers guides (https://developer.etrade.com/getting-started/developer-guides) contains the following example:
Item | Value |
---|---|
Key | c5bb4dcb7bd6826c7c4340df3f791188 |
Secret | 7d30246211192cda43ede3abd9b393b9 |
Access Token | VbiNYl63EejjlKdQM6FeENzcnrLACrZ2JYD6NQROfVI= |
Access Secret | XCF9RzyQr4UEPloA+WlC06BnTfYC1P0Fwr3GUw/B0Es= |
Timestamp | 1344885636 |
Nonce | 0bba225a40d1bbac2430aa0c6163ce44 |
HTTP Method | GET |
URL | https://api.etrade.com/v1/accounts/list |
The expected signature is: UOnPVdzExTAgHkcGWLLfeTaaMSM%3D
This is the signature string and key I have built:
field | value |
---|---|
base string | GET&https%3A%2F%2Fapi.etrade.com%2Fv1%2Faccounts%2Flist&oauth_consumer_key%3Dc5bb4dcb7bd6826c7c4340df3f791188%26oauth_nonce%3D0bba225a40d1bbac2430aa0c6163ce44%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1344885636%26oauth_token%3DVbiNYl63EejjlKdQM6FeENzcnrLACrZ2JYD6NQROfVI%3D |
Key | 7d30246211192cda43ede3abd9b393b9&XCF9RzyQr4UEPloA+WlC06BnTfYC1P0Fwr3GUw/B0Es= |
But my resulting Signature is: 8alOEOXdzxx+N7+77VRyducKWJM=, which is different than the expected value. What am I doing wrong?
To hash the base string I call (this is a DELPHI class for HMAC_SHA1 hash)
result := TNetEncoding.Base64.EncodeBytesToString(THashSHA1.GetHMACAsBytes(AData, AKey));
I believe that this hash is correct as it returns the correct signature for another OAuth signature example (as shown below) at ttps://oauth.net/core/1.0a/#sig_base_example where they provided the base string and key.
field | value |
---|---|
base string | GET&http%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacation.jpg%26oauth_consumer_key%3Ddpf43f3p2l4k3l03%26oauth_nonce%3Dkllo9940pd9333jh%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1191242096%26oauth_token%3Dnnch734d00sl2jdk%26oauth_version%3D1.0%26size%3Doriginal |
key | kd94hf93k423kf44&pfkkdhi9sl3r4s00 |
signature | tR3+Ty81lMeYAr/Fid0kMTYa/WM= |
I think I just missing some little detail in the base string and possibly the key.
// ETrade test case from https://developer.etrade.com/getting-started/developer-guides
// Following the Signture example
// Item Value
// Key c 5bb4dcb7bd6826c7c4340df3f791188
// Secret 7d30246211192cda43ede3abd9b393b9
// Access Token VbiNYl63EejjlKdQM6FeENzcnrLACrZ2JYD6NQROfVI=
// Access Secret XCF9RzyQr4UEPloA+WlC06BnTfYC1P0Fwr3GUw/B0Es=
// Timestamp 1344885636
// Nonce 0bba225a40d1bbac2430aa0c6163ce44
// HTTP Method GET
// URL https://api.etrade.com/v1/accounts/list
// Resulting signature UOnPVdzExTAgHkcGWLLfeTaaMSM%3D
URL := URIEncode('https://api.etrade.com/v1/accounts/list');
URL := 'GET&' + URL + '&';
Data.Add('oauth_consumer_key=c5bb4dcb7bd6826c7c4340df3f791188');
Data.Add('&oauth_nonce=0bba225a40d1bbac2430aa0c6163ce44');
Data.Add('&oauth_signature_method=HMAC-SHA1');
Data.Add('&oauth_timestamp=1344885636');
Data.Add('&oauth_token=VbiNYl63EejjlKdQM6FeENzcnrLACrZ2JYD6NQROfVI=');
Data.Add('&oauth_version=1.0');
AData := Data[0];
for i:= 1 to Data.Count - 1 do
AData := AData + Data[i];
AData := URL + URIEncode(AData);
AKey := '7d30246211192cda43ede3abd9b393b9&XCF9RzyQr4UEPloA+WlC06BnTfYC1P0Fwr3GUw/B0Es=';
result := TNetEncoding.Base64.EncodeBytesToString(THashSHA1.GetHMACAsBytes(AData, AKey));
// the incorrect result is returned
// an example from https://oauth.net/core/1.0a/#sig_base_example
AString :=
'GET&http%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacation.jpg' +
'%26oauth_consumer_key%3Ddpf43f3p2l4k3l03' +
'%26oauth_nonce%3Dkllo9940pd9333jh' +
'%26oauth_signature_method%3DHMAC-SHA1' +
'%26oauth_timestamp%3D1191242096' +
'%26oauth_token%3Dnnch734d00sl2jdk' +
'%26oauth_version%3D1.0' +
'%26size%3Doriginal';
result := EncodeBase64(THashSHA1.GetHMACAsBytes(AString,
'kd94hf93k423kf44&pfkkdhi9sl3r4s00'));
// returns the correct hash