1

I am trying to use Microsoft REST API to upload a file to OneDrive. I am passing access token to the request which I have got from Azure AD service:

URL urlTest = new URL("https://graph.microsoft.com/v1.0/me/drive/root:/TestFolder/RaviPdf.pdf:/content");
LOGGER.info("url1  : "+urlTest );

HttpURLConnection httpConn = (HttpURLConnection) urlTest.openConnection();
LOGGER.info("conn1  : "+httpConn );
httpConn.setDoOutput(true);
httpConn.setRequestMethod("PUT");
httpConn.setRequestProperty("Authorization", "Bearer " + accessToken);
httpConn.setRequestProperty("Accept","application/json");
httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
httpConn.setRequestProperty("charset","UTF-8");
httpConn.setRequestProperty("Content-Length",Integer.toString(100));

OutputStream os = httpConn.getOutputStream();
os.write(bArray);
os.close();  //don't forget to close the OutputStream
httpConn.connect();
httpResponseCode = httpConn.getResponseCode();
LOGGER.info("httpResponseCode "+httpResponseCode );
//here just printing error stream result 
LOGGER.info("httpConn.getErrorStream())"+httpConn.getErrorStream());

In response I am getting this error:

{
  "error": {
    "code": "notAllowed",
    "message": "You do not have access to create this personal site or you do not have a valid license",
    "innerError": {
      "request-id": "8d09ab84-a922-4713-9643-bbc533af41f9",
      "date": "2019-05-10T11:09:59"
    }
  }
}
Marc LaFleur
  • 31,987
  • 4
  • 37
  • 63
  • 1
    Are you sure you have a OneDrive instance and are you able to access your OneDrive via a browser? – Marc LaFleur May 10 '19 at 16:04
  • 2
    Also, are you intending to hit OneDrive Consumer (https://onedrive.com) or OneDrive Business (https://xxx-my.sharepoint.com)? – Brad May 11 '19 at 01:37
  • @MarcLaFleur No I can't access it via UI, I can't see Onedrive when I login through Azure AD Service. – Ravina Somvanshi May 14 '19 at 07:07
  • @Brad Sorry but As I am new to my Azure AD integration development, I am not sure which type of onedrive account it is. This account is connected to Azure AD service and can be accessed by office 365. – Ravina Somvanshi May 14 '19 at 12:50
  • If you don't have a OneDrive assigned to the user, you can't access it via Graph. Graph is just an API for an existing drive, it can't operate against a drive that doesn't exist. – Marc LaFleur May 14 '19 at 13:50
  • I doubt this is causing the issue here but you're `Content-Type` is also incorrect (it should be `application/pdf` in this context) and you're `Content-Length` should be the size of `bArray`. – Marc LaFleur May 14 '19 at 19:41

0 Answers0