AnsweredAssumed Answered

HttpURLConnection and expired tokens

Question asked by jaarons47 on Apr 13, 2017
Latest reply on Apr 14, 2017 by jaarons47

I am trying to access a TPK for download using HttpURLConnection.  The reason I am doing it this way versus 

InputStream is = item.fetchData();

is because i want to be able to resume download by setting a Range in Bytes to resume download when a network connection is available again.

Issue is this.  I can download a TPK right after logging into the application (before token expires) with the following code...

requestUrl = portal.getUrl() + "/sharing/rest/content/items/" + itemID + "/data?token=" + mValidLoginCredentials.getToken();
URL url2 = new URL(requestUrl);
HttpURLConnection urlConnection = (HttpURLConnection) url2.openConnection();
urlConnection.setRequestProperty("Range", "bytes=" + (baseMapFile.length()) + "-");
urlConnection.setRequestProperty("If-Range", lastModified);
inputStream = new BufferedInputStream(urlConnection.getInputStream(), 16384);

I am wondering how I can regenerate a token from credentials so that I can provide the new token into the requestURL variable.
I have tried a couple of things that do not work...

1.  Trying to setUserToken then use it does not work
portal.getCredentials().setUserToken(mValidLoginCredentials.getToken(), portal.getUrl());
requestUrl = portal.getUrl() + "/sharing/rest/content/items/" + itemID + "/data?token=" + portal.getCredentials().getToken();
2. Trying to generate token. Probably bad syntax here...
requestUrl = portal.getUrl() + "/sharing/rest/content/items/" + itemID + "/data?token=tokens/generateToken?username=mMouse&password=****;

I am pretty sure it is some sort of syntax issue here.  Any advice greatly appreciated.  Am using Runtime SDK Android 10.2.9.

Outcomes