Android Question Google API Patch

cdsincfl

Member
Licensed User
Longtime User
I have cheched the "GoogleDrive via REST API V3 - Small Testproject" post by Fredo.
Also checked the "Working With Google Drive" post by davfla.

Both work great and many thanks to Fredo and davfla for the code.

Does anyone have an example for the following:

- Renaming an existing file on Google Drive
- Moving an existing file on Google Drive to another Folder on Google Drive

I have searched and could not find any articles on the Patch or Update request.
Checked the Google API documentation and all of the examples are in Java or Python which I don't know either.

Any help appreciated.
 

cdsincfl

Member
Licensed User
Longtime User
I have tried the following code:
MoveFile:
Sub MoveFile(FileID As String, NewFolderID As String) As ResumableSub
    
    Dim h_patch As HttpJob
    Dim m As Map : m.Initialize

    h_patch.Initialize("",Me)
    Dim jg As JSONGenerator
    Dim data() As Byte
    m.Put("access_token", myAccessToken)
    m.Put("corpora", "user")
    m.Put("addParents=",  Array As String(NewFolderID))
    m.Put("removeParents=", "")

    jg.Initialize(m)                        'Map zu JSON wandeln
    data = (jg.ToString).GetBytes("UTF-8")    'in Datenstream wandeln
    
    h_patch.PatchBytes("https://www.googleapis.com/upload/v3/files" & "/" & FileID, data)
    
    Wait For (h_patch) JobDone(h_patch As HttpJob)
                        
    If h_patch.Success Then
        'Log("#-  x264, h_cf.GetString=" & h_cf.GetString)
        Dim j As JSONParser
        Dim Map1 As Map
        J.Initialize(h_patch.GetString)
        Map1 = J.NextObject
        CallSub2(evModule, evName & "_PatchCompleted", Map1.Get("id"))
    Else
        'Log("#-  x272, h_cf.ErrorMessage=" & h_cf.ErrorMessage)
        CallSub2(evModule, evName & "_PatchCompleted", "")
    End If
'   
    h_patch.Release
    Return Null
End Sub

I never get JobDone it just waits and stops there.
Any suggestions on what is wrong are appreciated.
 
Upvote 0

cdsincfl

Member
Licensed User
Longtime User
I have solved moving a file to another folder with this code:

MoveFile:
Sub MoveFile(FileID As String, AddFolderID As String, RemoveFolderID As String) As ResumableSub
    
    Dim h_patch As HttpJob
    Dim m As Map : m.Initialize

    h_patch.Initialize("",Me)
    Dim jg As JSONGenerator
    Dim data() As Byte
    m.Put("title", "Renamed11.txt")

    jg.Initialize(m)                        'Map zu JSON wandeln
    data = (jg.ToString).GetBytes("UTF-8")    'in Datenstream wandeln
    
    Log(BytesToString(data, 0, data.Length, "UTF-8"))
    h_patch.PatchBytes($"https://www.googleapis.com/upload/drive/v3/files/${FileID}?addParents=${AddFolderID}&removeParents=${RemoveFolderID}&key=${Main.sToken}"$, data)
    h_patch.GetRequest.SetHeader("Authorization", "Bearer " & myAccessToken)
    h_patch.GetRequest.SetHeader("Accept", "application/json ")
    h_patch.GetRequest.SetHeader("Content-Type", "application/json ")
    'Log("PatchRequest")
    
    Wait For (h_patch) JobDone(h_patch As HttpJob)
                        
    If h_patch.Success Then
        Dim j As JSONParser
        Dim Map1 As Map
        J.Initialize(h_patch.GetString)
        Map1 = J.NextObject
        'Log("PatchCompleted: " & Map1.Get("id"))
        CallSub2(evModule, evName & "_PatchCompleted", Map1.Get("id"))
    Else
        'Log("PatchCompleted: {ERR}")
        CallSub2(evModule, evName & "_PatchCompleted", "{ERR}")
    End If
    '
    h_patch.Release
    Return Null
End Sub

The code moves the file by FileID to another folder without error. I can't seem to get the file name to change. It has no effect for anything entered for the title. It does add/remove parents from the file. I have tried the same code in the Google API test API and it does renames the file.

What a I missing?
 
Upvote 0

cdsincfl

Member
Licensed User
Longtime User
Upon further testing the code posted does not solve the problem.
It will move a file from the Google Drive [root] folder to another folder and back to the [root] folder.
BUT:
-- The file is corrupted after the move
-- The file rename does not work

There are no errors returned and I have used the Google API to test and it works fine without error. I have duplicated the Patch exactly as the one used in the API test and it does not work from my B4A app.

Not getting much feedback on this question during the Holidays.
Hope someone has some suggestions.
 
Upvote 0
Top