msgraph-sdk-java: Unable to upload a file

I’m able to get the list of all files in a OneDrive folder and also download a file from a OneDrive Folder to my android device. But I am unable to upload a file to OneDrive. It silently crash when I call the post() function. I use MSAL to connect to OneDrive. My scopes are: “User.Read”, “Files.ReadWrite.All” I use microsoft-graph:2.0.0

Here is a simplified code with minimal error handling inspired from the sample app found here:

Note: I can debug step all the way to buildRequest() but it crashes when I try to step thru .post() The requestUrl seams ok: https://graph.microsoft.com/v1.0/me/drive/root:/TestFolder/Test File.sco:/microsoft.graph.createUploadSession (Crashes at the line that is commented // ************* Silently craches here ***************** )

The runtime exception cause is: com.microsoft.graph.core.ClientException: Error during http request

   private void oneDrive_UploadFile(final IAuthenticationResult authenticationResult, final String strLocalFilePath)
    {
        // Create a callback used by the upload provider
        IProgressCallback<DriveItem> callback = new IProgressCallback<DriveItem>()
        {
            @Override
            // Called after each slice of the file is uploaded
            public void progress(final long current, final long max)
            {
                System.out.println(
                        String.format("Uploaded %d bytes of %d total bytes", current, max)
                );
            }

            @Override
            public void success(final DriveItem result)
            {
                System.out.println(
                        String.format("Uploaded file with ID: %s", result.id)
                );
                executeOneDriveTask(authenticationResult);
            }

            public void failure(final ClientException ex)
            {
                System.out.println(
                        String.format("Error uploading file: %s", ex.getMessage())
                );
            }
        };

        // Get an input stream for the file
        File file = new File(strLocalFilePath);
        if (file.exists())
        {
            final String accessToken = authenticationResult.getAccessToken();

            IGraphServiceClient graphClient =
                    GraphServiceClient
                            .builder()
                            .authenticationProvider(new IAuthenticationProvider()
                            {
                                @Override
                                public void authenticateRequest(IHttpRequest request)
                                {
                                    Log.d(TAG, "Authenticating request," + request.getRequestUrl());
                                    request.addHeader("Authorization", "Bearer " + accessToken);
                                }
                            })
                            .buildClient();

            InputStream fileStream = null;
            try
            {
                fileStream = new FileInputStream(file);
            }
            catch (FileNotFoundException e)
            {
                e.printStackTrace();
            }
            long streamSize = file.length();

            // Create an upload session
            UploadSession uploadSession = graphClient
                    .me()
                    .drive()
                    .root()
                    // itemPath like "/Folder/file.txt"
                    // does not need to be a path to an existing item
                    .itemWithPath(AppPrefs.getInstance().getOneDriveFolder() + '/' + file.getName())
                    .createUploadSession(new DriveItemUploadableProperties())
                    .buildRequest()
                    .post();   // ************* Silently craches here   *****************  

            ChunkedUploadProvider<DriveItem> chunkedUploadProvider = new ChunkedUploadProvider<DriveItem>(
                    uploadSession,
                    graphClient,
                    fileStream,
                    streamSize,
                    DriveItem.class);

            // Config parameter is an array of integers
            // customConfig[0] indicates the max slice size
            // Max slice size must be a multiple of 320 KiB
            int[] customConfig = {2 * 320 * 1024};

            // Do the upload
            try
            {
                chunkedUploadProvider.upload(callback, customConfig);
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }

Any clue ?

Thanks

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 34 (12 by maintainers)

Most upvoted comments

Do you have an estimated release date for V2.2 ?