Android Question Google Drive Example: Stuck with file update

Woinowski

Active Member
Licensed User
Longtime User
Hi,

I'm stuck with my example for the Google Drive API (attached). Based on http://www.b4x.com/android/forum/threads/google-drive-api-uploaded-file-names.36387/#post-214064 I could manage:

  • Authorization (OAuth 2.0)
  • File Upload
  • File Download
  • (Paged) File Listing
  • Get File Metadata
Updating a file (esp. "title" and the file itself) does not work. I tried various approaches, in the attached file you find the latest try. Google Drive API reference does not help...

I always get a 404 NOT FOUND error. Which is strange, because when trying with the Google Drive API explorer the file ids always work fine, and an update of the title is also possible.

Maybe somebody can help. Here is a part of the code, the full example is attached (needs Client ID and Secret to work):

B4X:
' UNTIL HERE, basically upload code that works already.
  If FileId = "" Then
     ' Upload as new without file id in the URL
    Upload.PostBytes(DriveUploadLink & "?uploadType=multipart", data)
    Upload.GetRequest.SetContentType("multipart/related; boundary=" & boundary)
    Upload.GetRequest.SetHeader("Authorization", "Bearer " & AccessToken)
   Else
     ' FILE UPDATE DOES NOT WORK
     '
     ' HELP, I NEED SOMEBODY.
     ' HELP :-)
     '
     Msgbox(FileId, "File ID")
     ' Update requires a file id
    Upload.PostBytes(DriveUploadLink & "/" & FileId & "?uploadType=multipart", data)
    Upload.GetRequest.SetContentType("multipart/related; boundary=" & boundary)
     Upload.GetRequest.SetHeader("Authorization", "Bearer " & AccessToken)
     ' For this I always get a 404 NOT FOUND error
     ' It is the same when I do not use the full metadata from the upload but only the title
     ' I have veryfied the FileID with the Google API explorer. There it works and I can udpate the title
     ' or do whatever I want to do (e.g. delete the file)
   End If

Thanks in advance!
Jens
 

Attachments

  • Google Drive Example.zip
    12.1 KB · Views: 517

bus1963

Member
Licensed User
Longtime User
Hello,
I know this conversation is a few days old ... but ...

I have found after a long search for this dialog.
I am faced with the problem that I want to use your code changed a little.
I have the file paths changed a little ... yet he always gives me out an error message - Error sending !!
Did you already get help, or found only one solution to the problem?

I want to use for a database solution Google Drive. This is to 3 mobile phones and PC 4 comunicate.
The database file download is already be viable, only the upload a text file just does not work. !!

Please help or advice

Thanks - Sven
 
Upvote 0

Woinowski

Active Member
Licensed User
Longtime User
Hello,

changing local file paths should not be a problem, as long as you so not try to write to protected folders.

Changing the Google drive (URI) paths will surely not work if you do not know what exactly you are doing. I think I am no expert to help for that.

My update problem seems to be that for update you need put and not post requests. Could not verify that yet. I think Httpjob/Httputils2 needs changes as well.

Regards,
Jens
 
Upvote 0

bus1963

Member
Licensed User
Longtime User
Hello Jens,

Thanks for the quick reply.

I pushed in between to check in code MsgBoxes to the individual entries, but it all seems to be correct.
He begins to send, but after a while comes up -> Failed to send ...
Can not> Upload.PostFiles <directly easily upload only one file ??

Question!
If you just want to upload a file without an update .... it works for you .. ??

That can not be so complicated ..?

Greetings from me and if you still can find a solution ... please post.
 
Upvote 0

Woinowski

Active Member
Licensed User
Longtime User
Hello Sven,

the code I uploaded works for me. You just need to add the API key and secret.

Jens
 
Upvote 0

davfla

Member
Licensed User
Longtime User
Hi,

This is an old thread, I know. But I had found the solution for this problem after some hours of searching.

B4X:
    If FileId = "" Then
        ' Upload as new without file id in the URL
        Upload.PostBytes(DriveUploadLink & "?uploadType=multipart", data)
        Upload.GetRequest.SetContentType("multipart/related; boundary=" & boundary)
        Upload.GetRequest.SetHeader("Authorization", "Bearer " & AccessToken)
    Else
        'Create a new stream
        Dim OutUpdate As OutputStream                           
        Dim InUpate As InputStream = File.OpenInput(Dir, FN)

        'Initialize the stream   
        OutUpdate.InitializeToBytesArray(1000)

        'Copy the filestream in the output stream
        File.Copy2(InUpate, OutUpdate)

        'Fill export data array
        data = OutUpdate.ToBytesArray

        'Here is the difference. An update must make with Put, not Post
        Upload.PutBytes(DriveUploadLink & "/" & FileId, data)
        Upload.GetRequest.SetContentType("boundary=" & boundary)
        Upload.GetRequest.SetHeader("Authorization", "Bearer " & AccessToken)
    End If

The new Put Function must be insert in the HttpJob Module
B4X:
'Sends a PUT request with the given string as the post data
Public Sub PutBytes(Link As String, Data() As Byte)
    req.InitializePut2(Link, Data)
    CallSubDelayed2(HttpUtils2Service, "SubmitJob", Me)
End Sub

David
 
Last edited:
Upvote 0
Top