Android Question Google Drive API Uploaded File Names

SackMcNuts

Member
Licensed User
Longtime User
Hello,

I am using the Google Drive API to upload photos to my Drive account.

I have successful OAuth clearance and I'm able to POST the files using HttpUtils2.
I have changed the MIME type in the Request so the file acts appropriately one on Drive. Awesome, so far.

However, the File Name always reverts to "Untitled." When I use GetString upon Job Success, the "title" field is in fact "untitled."

I've tried GetRequest.SetHeader to hopefully edit the MetaData but nothing... In fact when I view the Request header in HttpService using Log(Result.GetHeaders) , the header appears unchanged. I can't seem to change or add values to the header.

Any advice would be HUGE!!

Thank you!
 

SackMcNuts

Member
Licensed User
Longtime User
I've solved the mystery behind the Google Drive Multi-Part post... It's very picky about the syntax but I've gotten this to work.
B4X:
'The Directory and File Name of the File To be uploaded.
   
    Dim Dir As String = "FILE DIRECTORY"
    Dim FN As String = "FILE NAME"
   
    Dim In As InputStream = File.OpenInput(Dir, FN)
    Dim out2 As OutputStream
    out2.InitializeToBytesArray(1000)
   

'A Map of properties To be set on the File once uploaded.

    Dim m As Map
    m.Initialize
    m.Put("title", “DESIRED NAME”)
    m.Put("parents", “ID # OF FOLDER To UPLOAD To”) 'remove this line To upload directly To Main Drive Folder.   


'The properties must be In JSON.

    Dim J As JSONGenerator
    J.Initialize(m)   
   

'I use this To determine the proper mimeType For the File being uploaded.  I only have 3 mimeTypes below, more can be added.

    Dim FileName As String = FN
    FileName = FileName.SubString(FileName.LastIndexOf(".")+1)
    Dim ContentType As String
    Dim Suffix As Object = FileName   
    Select Suffix
        Case "jpg"
            ContentType = "image/jpeg"
        Case "xls"
            ContentType = "application/vnd.ms-excel"
        Case "csv"
            ContentType = "text/csv"
    End Select


'This Is where the Syntax has To be pretty much perfect otherwise you’ll get a Bad Request response.
   
    Dim data() As Byte
'The first half of the POST contains the JSON data AND the Content Type from above.
    data = ("--bound" & CRLF & "Content-Type: application/json; charset=UTF-8" & CRLF & CRLF & J.ToString & CRLF & CRLF & "--bound" & CRLF & "Content-Type: " & ContentType & CRLF & CRLF).GetBytes("UTF8")
'Write it To the Stream.
    out2.WriteBytes(data, 0, data.Length)
'Add the File itself To the Stream.   
    File.Copy2(In, out2)
'Add the final boundary to the POST
    data = (CRLF & CRLF & "--bound--").GetBytes("UTF8")
'Write it To the Stream Then make the Bytes the complete Stream.
    out2.WriteBytes(data, 0, data.Length)
    data = out2.ToBytesArray

'POST the Bytes and change the Content Type.
'The boundary can be any word you choose.   
    FileUpload.PostBytes(DriveUploadLink, data)
    FileUpload.GetRequest.SetContentType("multipart/related; boundary=bound")
    FileUpload.GetRequest.SetHeader("Authorization", "Bearer " & AccessToken)
 
Upvote 0

Woinowski

Active Member
Licensed User
Longtime User
Hello,

I've tried to incorporate that into the Googledrivetest from somewhere along the line.

I come up with:

B4X:
Sub Button3_Click
   ProgressDialogShow("Uploading File...")
  
   Dim Dir, FN, title, parent As String
   Dir = File.DirInternal
   FN = "Test.jpg"
   title = "EINE TESTDATEI"
   parent = ""
  
   Dim boundary As String
   boundary = "bound"
  
   Dim In As InputStream = File.OpenInput(Dir, FN)
  Dim out2 As OutputStream
  out2.InitializeToBytesArray(1000)
  

   'A Map of properties To be set on the File once uploaded.

  Dim m As Map
  m.Initialize
  m.Put("title", title)
   If parent <> "" Then
    m.Put("parents", parent) '“ID # OF FOLDER To Upload To” remove this line To upload directly To Main Drive Folder.  
   End If

   'The properties must be In JSON.

  Dim J As JSONGenerator
  J.Initialize(m)  
  

   'I use this To determine the proper mimeType For the File being uploaded.  I only have 3 mimeTypes below, more can be added.

  Dim FileName As String = FN
  FileName = FileName.SubString(FileName.LastIndexOf(".")+1)
  Dim ContentType As String
  Dim Suffix As Object = FileName  
  Select Suffix
  Case "jpg"
  ContentType = "image/jpeg"
  Case "xls"
  ContentType = "application/vnd.ms-excel"
  Case "csv"
  ContentType = "text/csv"
     Case Else
       ContentType = "application/octet-stream"
  End Select


   'This Is where the Syntax has To be pretty much perfect otherwise you’ll get a Bad Request response.
  
  Dim data() As Byte
   'The first half of the POST contains the JSON data AND the Content Type from above.
  data = ("--" & boundary & CRLF & "Content-Type: application/json; charset=UTF-8" & CRLF & CRLF & J.ToString & CRLF & CRLF & "--" & boundary & CRLF & "Content-Type: " & ContentType & CRLF & CRLF).GetBytes("UTF8")
   'Write it To the Stream.
  out2.WriteBytes(data, 0, data.Length)
   'Add the File itself To the Stream.  
  File.Copy2(In, out2)
   'Add the final boundary to the POST
  data = (CRLF & CRLF & "--" & boundary & "--").GetBytes("UTF8")
   'Write it To the Stream Then make the Bytes the complete Stream.
  out2.WriteBytes(data, 0, data.Length)
  data = out2.ToBytesArray
  
  
  
   'POST the Bytes and change the Content Type.
   'The boundary can be any word you choose.  
   Upload.Initialize ("FileUpload", Me)  
  Upload.PostBytes(DriveUploadLink, data)
  Upload.GetRequest.SetContentType("multipart/related; boundary=bound")
  Upload.GetRequest.SetHeader("Authorization", "Bearer " & AccessToken)
End Sub

As a result I get "Invalid multipart request with 0 mime types". Can you tell what I got wrong?

Regards,
Jens
 
Upvote 0
Top