AnsweredAssumed Answered

Error 403 using Upload API (Java)

Question asked by Holmer Jordan on Apr 7, 2017
Latest reply on Apr 19, 2017 by Holmer Jordan

I am working through the Canvas documentation for doing file uploads. My institution is interested in using this to automatically place our generated PDF report cards into user's files. I am testing by trying to upload a file called test.txt into my own files. Step 1 from the documentation works perfectly. However in Step 2 I keep getting a 403 error. Here is my code. The error happens at the last line of this snippet. Any help is greatly appreciated.


URL url = new URL(urlString);
Map<String,Object> parameters = new LinkedHashMap<>();

for(int i = 0; i < params.length; i++) {
parameters.put(paramNames[i], params[i]);
parameters.put("file", new FileReader("U:/Canvas/test.txt"));

StringBuilder postData = new StringBuilder();
for (Map.Entry<String,Object> param : parameters.entrySet()) {
if (postData.length() != 0) postData.append('&');
postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
byte[] postDataBytes = postData.toString().getBytes("UTF-8");

HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));

Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));