1

I'm trying to get a get request to the db cosmos using android, using REST API! The azure cosmos db has a specific heading, but I think everything is correct

However, when I use Http REST API I get the error Unauthorized 401, can you help me?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    n = findViewById(R.id.teste11);
    Button buttonParse = findViewById(R.id.button_parse);
    buttonParse.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                getwebservice();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    client = new OkHttpClient();


}

Getwebservices method

 private void getwebservice() throws Exception {

    String headerDate = getDateString();

    String gen = generateAuthHeader("GET", "docs", "dbs/tempdb/colls/tempcoll/docs/WakefieldFamily",
            headerDate, PRIMARY_KEY);
    final Request request = new Request.Builder()
            .url("https://***.documents.azure.com:443/dbs/***/colls/***/docs/WakefieldFamily")
            .get()
            .addHeader("Accept", "application/json")
            .addHeader("x-ms-version", "2017-02-22")
            .addHeader("Authorization", gen)
            .addHeader("x-ms-date", headerDate)
            .addHeader("cache-control", "no-cache")
            .build();

    okhttp3.Response response = null;
    client.newCall(request).enqueue(new Callback() {
        Headers g = request.headers();
        @Override
        public void onFailure(okhttp3.Call call, IOException e) {
             MainActivity.this.runOnUiThread(new Runnable() {
                 @Override
                 public void run() {
                     String g = "oi";

                 }
             });
        }

        @Override
        public void onResponse(okhttp3.Call call, okhttp3.Response response) throws IOException {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {

                    n.setText(String.valueOf(response.code()));
                }
            });
        }
    });

    }

These are the auxiliary methods for signature and header date

 public String getDateString() {
    SimpleDateFormat formatter =
            new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
    formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
    return formatter.format(new Date()).toLowerCase();

}

public String generateAuthHeader(String verb, String resourceType, String resourceId, String date, String masterKeyBase64) throws Exception {

    byte[] masterKeyBytes = Base64.decode(masterKeyBase64, Base64.NO_WRAP);
    Mac mac = Mac.getInstance("HMACSHA256");
    mac.init(new SecretKeySpec(masterKeyBytes, "HMACSHA256"));

    //Build the unsigned auth string.
    String stringToSign = verb.toLowerCase() + "\n"
            + resourceType.toLowerCase() + "\n"
            + resourceId + "\n"
            + date.toLowerCase() + "\n"
            + "\n";

    //Sign and encode the auth string.
    String signature = Base64.encodeToString(mac.doFinal(stringToSign.toLowerCase().getBytes("UTF-8")), Base64.NO_WRAP);

    //Generate the auth header.
    String authHeader = URLEncoder.encode("type=master&ver=1.0&sig=" + signature, "UTF-8");

    return authHeader;
}

I'm still trying to figure out how this works.

1 Answer 1

0

I think the problem is that you are converting the stringToSign variable to lower case. And the resourceId has some capital letters which are converted in the process.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.