0

I'm trying to get data from the API which needs to handle aws-authentication, my question is how can I generate Authorization and X-Amz-Date? I have to pass 3 parameter as header: Content-Type, Authorization and X-Amz-Date. As you can find in image:enter image description here

here is the function that generate Authorization String:

public static String gerateOAuthAWS(Context co) throws Exception {
    JodaTimeAndroid.init(co);
    DateTimeFormatter fmt = DateTimeFormat.forPattern("EEE', 'dd' 'MMM' 'yyyy' 'HH:mm:ss' 'Z").withLocale(Locale.US);
    String ZONE = "GMT";
    DateTime dt = new DateTime();
    DateTime dtLondon = dt.withZone(DateTimeZone.forID(ZONE)).plusHours(1);
    String formattedDate = dtLondon.toString(fmt);
    String oauth = "AWS4-HMAC-SHA256 Credential="+ ACCESS_KEY+"/us-east-1/execute-api/aws4_request, SignedHeaders=content-type;host;x-amz-date, Signature="+
            getSignatureKey(SECRET_KEY,formattedDate,"us-east-1","execute-api");
    return  oauth;
}


static byte[] HmacSHA256(String data, byte[] key) throws Exception {
    String algorithm="HmacSHA256";
    Mac mac = Mac.getInstance(algorithm);
    mac.init(new SecretKeySpec(key, algorithm));
    return mac.doFinal(data.getBytes("UTF8"));
}

static String getSignatureKey(String key, String dateStamp, String regionName, String serviceName) throws Exception {
    byte[] kSecret = ("AWS4" + key).getBytes("UTF8");
    byte[] kDate = HmacSHA256(dateStamp, kSecret);
    byte[] kRegion = HmacSHA256(regionName, kDate);
    byte[] kService = HmacSHA256(serviceName, kRegion);
    byte[] kSigning = HmacSHA256("aws4_request", kService);
    return Base64.encodeToString(kSigning,Base64.DEFAULT).replaceAll("\n", "");
}

Content-Type is "application/x-www-form-urlencoded" and generate X-Amz-Date something as: "201805138T120046Z"

then pass them through retrofit methods:

@GET("prod/video")
Call<ArrayList<Video>> getAllVideos(@Header("Content-Type")String content_type,
                                    @Header("X-Amz-Date")String amz_date,
                                    @Header("Authorization")String auth);

the result returns null and I'm sure the issue is related the authorization since it worked before well.

thanks for your helps :)

Mahsa
  • 123
  • 1
  • 11

1 Answers1

-2

i always said to my friends why do you use retrofit or valley , if it's seems complicated to you !

instead you can use JSOUP or OKHTTP it's much easier and I realy love JSOUP

an example that you can connect and send you data:

private void fcmIdentity(final String fcmKey) {
    new Thread(new Runnable() {
      @Override
      public void run() {
        try {
          SSLHelper.enableSSLSocket();
          Connection.Response response = Jsoup
            .connect(Urls.identity)
            .header("Accept", "application/json")
            .header("KEY_2", "VALUE_2")
            .method(Connection.Method.POST)
            .ignoreContentType(true)
            .ignoreHttpErrors(true)
            .validateTLSCertificates(true)
            .followRedirects(true)
            .data("fcm", "" + fcmKey)
            .data("identity", preferences.getString("FCM_ID", ""))
            .execute();

          Log.i("fcmIdentity", response.statusCode() + "");
          Log.i("fcmIdentity", response.toString());
          Log.d("fcmIdentity", response.headers().toString());
          Log.i("fcmIdentity", response.body());


        } catch (Exception e) {
          e.printStackTrace();
          if (e instanceof IOException) {
            G.toast(getString(R.string.connection_error), true);
          }
        }
      }
    }).start();
  }

about SSLHelper it help to connect to HTTPS for more info check my topic https://answers.uncox.com/android/question/13003

Criss
  • 755
  • 7
  • 22
  • 1
    This is not related on the question as it is indicated on how to generate AWS Signature Authorization. You didn't generate any authorizations, and also Jsoup nor Okhttp doesn't make things easier, actually its the other way around. It makes parsing of the response complicated (you need to map it on a JSONObject, loop, etc) unlike Retrofit that will automatically the JSON response to object. – Tenten Ponce Apr 17 '19 at 06:32