hmac - Amazon SES HTTPS Query API Authentication error -


i have been trying send email using amazon ses https query api. signature calculate , send aws not accepted.

i error message.

<errorresponse xmlns="http://ses.amazonaws.com/doc/2010-12-01/">   <error>     <type>sender</type>     <code>signaturedoesnotmatch</code>     <message>the request signature calculated not match signature provided. check aws secret access key , signing     method. consult service documentation details.</message>   </error>   <requestid>c97bd130-24c9-11e6-924a-b59d7ac9182b</requestid> </errorresponse> 

here java code

public class sendsesmail {  public static void main(string[] args) throws invalidkeyexception, nosuchalgorithmexception, ioexception, signatureexception {     simpledateformat format = new simpledateformat("eee, dd mmm yyyy hh:mm:ss z");     string datestring = format.format(new date());      final string endpoint = "https://email.us-east-1.amazonaws.com";     final string aws_access = "access_key";     final string aws_secret = "secret_key";      closeablehttpclient httpclient = httpclients.createdefault();     httppost post = new httppost(endpoint);      string authstring = generateauthheader(aws_access, aws_secret, datestring);      list<namevaluepair> formvals = new arraylist<>();     formvals.add(new basicnamevaluepair("action", "sendrawemail"));     formvals.add(new basicnamevaluepair("destination.toaddresses.member.1", "bhanuka.yd@gmail.com"));     formvals.add(new basicnamevaluepair("message.body.text.data", "i hope see body."));     formvals.add(new basicnamevaluepair("message.subject.data", "this unique subject"));     formvals.add(new basicnamevaluepair("source", "test@test.com"));      urlencodedformentity formentity = new urlencodedformentity(formvals);      post.setheader("content-type", contenttype.application_form_urlencoded.tostring());     post.setheader("date", datestring);     post.setheader("x-amzn-authorization", authstring);      post.setentity(formentity);     httpresponse response = httpclient.execute(post);     response.getentity().writeto(system.out);   }   public static string generateauthheader(string accesskey, string secret, string datestring) throws invalidkeyexception, nosuchalgorithmexception, signatureexception {     string authheaderval = "aws3-https awsaccesskeyid=" + accesskey + ",algorithm=hmacsha256,signature=";     authheaderval += generatesignature(datestring, secret);     return authheaderval;  }   public static string generatesignature(string message, string secret) throws nosuchalgorithmexception, invalidkeyexception {     mac sha256_hmac = mac.getinstance("hmacsha256");     secretkeyspec secretkey = new secretkeyspec(secret.getbytes(), "hmacsha256");     sha256_hmac.init(secretkey);     return base64.encodebase64urlsafestring(sha256_hmac.dofinal(message.getbytes()));  } } 

the first time tried out , gave me error

<errorresponse xmlns="http://ses.amazonaws.com/doc/2010-12-01/">  <error>    <type>sender</type>    <code>validationerror</code>    <message>1 validation error detected: value null @ 'rawmessage' failed satisfy constraint: member must not null</message>  </error>  <requestid>9a986157-24ca-11e6-9864-3fdeb433e3c8</requestid> </errorresponse> 

it because signature had "=" sign @ end , used

encodebase64urlsafestring(); 

method convert signature base64 instead of

encodebase64string(); 

so doing wrong here, please me out here thanks.

i found out can use signature v4 authenticate http request.

anyone having problem, can follow exact process in below link , able authenticate.

one thing keep in mind that, request time should in iso8601 format, in utc

here method calculate current time in utc timezone.

public static string getutctimeiso8601format() {     timezone timezone = timezone.gettimezone("utc");     calendar calendar = calendar.getinstance(timezone);     simpledateformat iso8601format = new simpledateformat("yyyymmdd't'hhmmss'z'");//iso8601 b8601dzw.d format     iso8601format.settimezone(timezone);     return iso8601format.format(calendar.gettime()); } 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -