Android Question Upload to OneDrive

stu14t

Active Member
Licensed User
Longtime User
I've spent the best part of two days looking at this code and can't seem to get the file to upload to OneDrive.

The Endpoint seems to be formed correctly and the token works as I can successfully create folders and sub-folders. If anyone has any ideas, I'd be really grateful for some idea into where I'm going wrong.

The MS Graph api keeps saying "Not found" which I know the folder ID exits

B4X:
Sub SyncFolder(localFolderPath As String, parentFolderId As String) As ResumableSub
    ' Get a list of all files in the local folder
    Dim localFiles As List = File.ListFiles(localFolderPath)
    Log("Parent Folder ID: " & parentFolderId)
    ' For each local file, check if it exists on OneDrive and upload it if necessary
    For Each localFileName As String In localFiles
        ' Check if the file exists on OneDrive
        Dim job As HttpJob
        job.Initialize("", Me)
        job.Download($"https://graph.microsoft.com/v1.0/me/drive/items/${parentFolderId}:/${localFileName}:"$)
        job.GetRequest.SetHeader("Authorization", "Bearer " & token)
        Wait For (job) JobDone(job As HttpJob)
        If job.Success Then
            ' The file exists on OneDrive, check if the local file has been modified since it was last uploaded
            Dim parser As JSONParser
            parser.Initialize(job.GetString)
            Dim oneDriveFile As Map = parser.NextObject
            Dim oneDriveFileLastModified As Long = oneDriveFile.Get("lastModifiedDateTime")
            Dim localFileLastModified As Long = File.LastModified(localFolderPath, localFileName)
            If localFileLastModified > oneDriveFileLastModified Then
                ' The local file has been modified since it was last uploaded, upload it again
                UploadFile(localFolderPath, localFileName, parentFolderId)
            End If
        Else
            ' The file does not exist on OneDrive, upload it
            UploadFile(localFolderPath, localFileName, parentFolderId)
        End If
        job.Release
    Next
End Sub

Sub UploadFile(localFolderPath As String, localFileName As String, parentFolderId As String) As ResumableSub
    ' Initialize OkHttpClient
    Dim hc As OkHttpClient
    hc.Initialize("hc")

    ' URL encode the file name
    Dim su As StringUtils
    Dim encodedFileName As String = su.EncodeUrl(localFileName, "UTF8")
    encodedFileName = encodedFileName.Replace("+", "%20")

    ' Get the file's MIME type
    Dim extension As String = localFileName.SubString(localFileName.LastIndexOf(".") + 1)
    Dim mimeType As String = GetMimeType(extension)

    ' Log MIME Type and File Extension
    Log("File Extension: " & extension)
    Log("MIME Type: " & mimeType)

    ' Open the file as an InputStream
    Dim InStream As InputStream
    InStream = File.OpenInput(localFolderPath, localFileName)
    
    ' Log the InputStream
    Log("Stream = " & InStream)

    ' Get the file length
    Dim FileLength As Long
    FileLength = File.Size(localFolderPath, localFileName)

    ' Log the File Length
    Log("Length = " & FileLength)

    ' Initialize the POST request with InputStream
    Dim endpoint As String = $"https://graph.microsoft.com/v1.0/me/drive/items/${parentFolderId}/${encodedFileName}:/content"$
    Dim req As OkHttpRequest
    req.InitializePost(endpoint, InStream, FileLength)

    ' Log the Endpoint
    Log("Endpoint = " & endpoint)

    ' Set the request's Content-Type header
    req.SetContentType(mimeType)

    ' Set the request's Authorization header
    req.SetHeader("Authorization", "Bearer " & token)

    ' Log the Authorization Header
    Log("Authorization Header: " & "Bearer " & token)

    ' Execute the request
    hc.Execute(req, 1)
    Wait For hc_ResponseSuccess(Response As OkHttpResponse, TaskId As Int)
    If TaskId = 1 Then
        If Response.StatusCode = 201 Then
            Log($"Uploaded ${localFileName} to OneDrive"$)
        Else
            Log($"Failed to upload ${localFileName} to OneDrive: ${Response.StatusCode}"$)
        End If
    End If
    InStream.Close ' Close the InputStream
End Sub
 
Top