Experiencing arbitrary Unexpected 403 messages when uploading file with microsoft graph uploadSession

111 Views Asked by At

I have an android app which is uploading picture files to a sharepoint folder using Chunked upload session. Normally this is going well, but suddenly (out of the blue) i get 403 forbidden message. This may happen in the middle of attempts, where 1-5 are ok, 1 are 403, and subsequent uploads are ok. There aren't any differences of the files (approx 400 kb each), same folder, achieving new accessToken (if present) for each upload.

Anyone else having same experience, or is there any (known /unknown) bug in MsGraph ?

Using microsoft dependencies:

    //Microsoft
    //Identity
    implementation ('com.microsoft.identity.client:msal:2.0.2')
            {exclude group: 'com.microsoft.device.display'}

    //Graph
    implementation 'com.microsoft.graph:microsoft-graph:2.3.1'

Code snippets (kotlin)

Upload session:


                            val uploadSession = safeGraphServiceClient
                                .sites(CONST_SHAREPOINT_SITE_ID)
                                .drive()
                                .root()
                                .itemWithPath(itemWithPath)
                                .createUploadSession(DriveItemUploadableProperties())
                                .buildRequest()
                                .post()


                            val fileStream = FileInputStream(uri.path)

                            try {


                                ChunkedUploadProvider(
                                    uploadSession,
                                    safeGraphServiceClient,
                                    fileStream,
                                    fileStream.available().toLong(),
                                    DriveItem::class.java
                                ).upload(object : IProgressCallback<DriveItem?> {
                                    override fun success(result: DriveItem?) {
                                        Log.i(TAG, "success: upload success ${result.toString()}")
                                        Message.info("Picture successfully uploaded")

                                        //Delete file after upload
                                        uri.path?.let { safePath ->
                                            val fdelete = File(safePath)
                                            if (fdelete.exists()) {
                                                if (fdelete.exists()) {
                                                    if (fdelete.delete()) {
                                                        Log.i(TAG, "success: file $fdelete deleted from device")
                                                    } else {
                                                        Log.e(TAG, "success: could not delete file $fdelete transferred file from device")
                                                        Message.warning("Could not delete file after transfer")
                                                    }
                                                }
                                            }
                                        }
                                    }

                                    override fun failure(ex: ClientException?) {

                                        Log.e(TAG, "failure: upload failure", ex)
                                        Message.error("Error when uploading picture $itemWithPath")
                                    }

                                    override fun progress(current: Long, max: Long) {
                                        Log.i(TAG, "progress: upload progress $current of $max")
                                        Message.info("Upload progress $current of $max")
                                    }

                                }, *intArrayOf(320 * 1024))
                            } catch (exception: Exception) {
                                Log.e(TAG, "uploadPicture2: upload picture Exception !! should be possible to retry", exception)
                            }


Access token function:


    suspend fun generateAccessToken(microsoftAccountViewModel: MicrosoftAccountViewModel):String?
    {
        Log.i(TAG, "generateAccessToken: generateAccessToken")
        var accessToken: String?=null
        microsoftAccountViewModel.apply {
            singleAccountAppMLD.value?.let { singleAccountApp ->
                accessScopesMLD.value?.let { accessScopes ->
                    accessAuthorityMLD.value?.let { accessAuthority->
                        withContext(Dispatchers.IO){
                            val iAuthenticationResult
                                    = singleAccountApp.acquireTokenSilent(
                                    accessScopes,
                                    accessAuthority)
                            accessToken = iAuthenticationResult.accessToken
                            Log.i(TAG, "generateAccessToken: accessToken achieved = $accessToken")
                        }
                    }
                }
            }
        }
        return accessToken
    }
}

...or am I using the function in a wrong way (strange, since it works 85% of the attempts).

RG

0

There are 0 best solutions below