Thanks Alex, this advise helped me get on the right track with this.
The critical bit was to use the credentials with PortalItem.fetchItem to establish a new token.
Portal portal = new Portal(url, uc);
PortalItem item = PortalItem.fetchItem(portal, itemID);
If anyone ever runs into issues trying to download from AGOL via HttpURLConnection using token authentication below is the code that worked.....
try {
// Fetch the portal item by ID, and the data, passing in a Portal with an
// authenticated user with access to the item.
String url = "https://www.arcgis.com";
UserCredentials uc = new UserCredentials();
uc = mValidLoginCredentials;
Portal portal = new Portal(url, uc);
PortalItem item = PortalItem.fetchItem(portal, itemID);
String requestUrl = portal.getUrl() + "/sharing/rest/content/items/" + itemID + "/data";
if(mValidLoginCredentials!=null) {
String token = portal.getCredentials().getToken();
requestUrl = portal.getUrl() + "/sharing/rest/content/items/" + itemID + "/data?token=" + token;
}
URL url2 = new URL(requestUrl);
HttpURLConnection urlConnection = (HttpURLConnection) url2.openConnection();
filePath = dataWorkingDirectory + "/Offline_Base_Maps/" + item.getName();
TPKPath = filePath;
int len = 0;
//Check if file exists already
File baseMapFile = new File(filePath);
int length = 0;
if (baseMapFile.exists()) {
length = (int) baseMapFile.length();
if (length==0) {
lastModified = urlConnection.getHeaderField("Last-Modified");
inputStream = new BufferedInputStream(urlConnection.getInputStream(), 16384);
file_size = urlConnection.getContentLength();
}else{
urlConnection.setRequestProperty("Range", "bytes=" + (baseMapFile.length()) + "-");
urlConnection.setRequestProperty("If-Range", lastModified);
inputStream = new BufferedInputStream(urlConnection.getInputStream(), 16384);
}
}else{
lastModified = urlConnection.getHeaderField("Last-Modified");
inputStream = new BufferedInputStream(urlConnection.getInputStream(), 16384);
file_size = urlConnection.getContentLength();
}
long fileLength = Long.valueOf(file_size);
int latestPercentDone;
int lastPercentDone = 0;
// Save the data to a file.
outputStream =(length==0)? new FileOutputStream(filePath): new FileOutputStream(filePath,true);
byte[] buf = new byte[16384];
int size = 0;
while ((len = inputStream.read(buf)) != -1) {
outputStream.write(buf, 0, len);
length += len;
latestPercentDone = (int) Math.round(length * 100.0 / fileLength);
if (lastPercentDone != latestPercentDone) {
publishProgress(latestPercentDone);
}
lastPercentDone = latestPercentDone;
}
mNotificationHelper.completed();
outputStream.flush();
outputStream.close();
inputStream.close();
error = "done";
} catch (Exception e) {
e.printStackTrace();
}