401 Response with Canvas Data API

Jump to solution
msorrell1
Community Novice

I am building an Java application that will communicate with the Canvas Data API and receive the data. I have followed other tutorials and the documentation from the Canvas community. The current code I have receives a 401 HTTP error when I make the request. The current request URL is "https://portal.inshosteddata.com/api/account/self/dump ,but if I change the url to request /api/schema then the response is 200 and I receive data from the API. Here is my current code:

String time = getCurrentTime();
String signature = "GET\n" + getBaseUrlData() + "\n\n\n" + "/api/account/self/dump?after=45&limit=100/\n\n" + time + "\n" +    getSecret();
SecretKeySpec signingKey = new SecretKeySpec(getSecret().getBytes(), "HmacSHA256");
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(signingKey);
byte [] rawHmac = mac.doFinal(signature.getBytes());
String result = Base64.encode(rawHmac);

String authorizationHeader = "HMACAuth " + getApiKey() + ":" + result;
System.out.println("Authorization header: \n" + authorizationHeader);
java.net.URL url = new URL("https://" + getBaseUrlData()+"/api/account/self/dump");
System.out.println(url);
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("Authorization", authorizationHeader);
con.setRequestProperty("Date", time);
System.out.println(con.getResponseCode());

Labels (2)
1 Solution
msorrell1
Community Novice
Author

I solved my issue. My url path that I was placing in the signature for the request ended with a "/" character when it should not have. I didn't realize that there was a difference in /api/account/self/dump and /api/account/self/dump/

Thank you to everyone who replied to my question.

View solution in original post