使用AWS签名授权的Android Retrofit请求

时间:2018-05-15 14:20:48

标签: android amazon-web-services authentication retrofit aws-sdk

我正在尝试从需要处理aws身份验证的API中获取数据,我的问题是如何生成授权和X-Amz-Date? 我必须传递3个参数作为标题: Content-Type 授权 X-Amz-Date 。 如图所示:enter image description here

这是生成授权字符串的函数:

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 是“application / x-www-form-urlencoded” 并生成 X-Amz-Date 的内容为:“201805138T120046Z”

然后通过改进方法传递它们:

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

结果返回null并且我确定该问题与授权有关,因为它之前运行良好。

感谢您的帮助:)

1 个答案:

答案 0 :(得分:-1)

我总是对我的朋友说,为什么你会使用改造或谷,如果它对你来说似乎很复杂!

相反你可以使用JSOUP或OKHTTP它更容易,我真的很喜欢JSOUP

您可以连接并向您发送数据的示例:

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();
  }

关于 SSLHelper 它有助于连接到HTTPS 有关详细信息,请查看我的主题https://answers.uncox.com/android/question/13003