我有一个使用分块上传会话将图片文件上传到共享点文件夹的 android 应用程序。通常这进展顺利,但突然(突然)我收到 403 禁止消息。这可能发生在尝试的中间,其中 1-5 是可以的,1 是 403,后续上传是可以的。文件没有任何差异(每个大约 400 kb),相同的文件夹,每次上传都会获得新的 accessToken(如果存在)。
其他任何人都有相同的经验,或者 MsGraph 中是否有任何(已知/未知)错误?
使用微软依赖:
//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'
代码片段 (kotlin)
上传会话:
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)
}
访问令牌功能:
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
}
}
...或者我是否以错误的方式使用该函数(奇怪,因为它在 85% 的尝试中都有效)。
RG