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
It should work by simply adding to the request body. The code I'm using is:

Dim DriveUploadLink As String : DriveUploadLink = "https://www.googleapis.com/upload/drive/v2"
Upload.PostFile(DriveUploadLink & "/files" & "?access_token=" & AccessToken, File.DirRootExternal, "TestFile.jpg")

When I add "?title=TestFile.jpg" after the AccessToken I get an authorization error. If I change it to "&?title=TestFile.jpg" I get authorization but no change to the file title.

https://developers.google.com/drive/v2/reference/files/insert
 
Upvote 0

SackMcNuts

Member
Licensed User
Longtime User
Yeah probably, I've tried

Upload.PostFile(DriveUploadLink & "/files" & "?access_token=" & AccessToken & "&title=TestFile.jpg", File.DirRootExternal, "TestFile.jpg")

with no success.
 
Upvote 0

SackMcNuts

Member
Licensed User
Longtime User
I tried...

Dim j As JSONGenerator
Dim m As Map
m.Initialize
m.Put("title", "TestFile.jpg")
j.Initialize(m)
Log ("JSON " & j.ToString)

ProgressDialogShow("Uploading File...")
Upload.PostString(DriveLink & "/files" & "?access_token=" & AccessToken, j.ToString)

Upload.PostFile(DriveUploadLink & "/files" & "?access_token=" & AccessToken, File.DirRootExternal, "TestFile.jpg")

And got...

{

"error": {

"errors": [

{

"domain": "global",

"reason": "parseError",

"message": "This API does not support parsing form-encoded input."

}

],

"code": 400,

"message": "This API does not support parsing form-encoded input."

}

}

ERROR Bad Request
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User

Attachments

  • Aufnahme1.jpg
    Aufnahme1.jpg
    127.9 KB · Views: 376
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
It should work by simply adding to the request body.

As result on the fileupload you should get return a files-object-resource
with this informations it should be easy to update the metadata with the url for updating metadata!? I didnt tried that, just read what i posted here after asking aunt google ;-)
 
Upvote 0

SackMcNuts

Member
Licensed User
Longtime User


I've tried multipart with the linked tutorial however I get "Unsupported Media Type" as the returned error.

B4X:
    hc.Initialize ("hc")
    Dim files As List
    files.Initialize
    Dim FD As FileData
    FD.Initialize
    FD.Dir = File.DirRootExternal
    FD.FileName = "TestFile.jpg"
    FD.KeyName = "upfile2"
    FD.ContentType = "image/jpeg"
    files.Add(FD)

    Dim NV As Map
    NV.Initialize
    NV.Put("title", "Test.jpg")
    Dim req As HttpRequest
    req = MultipartPost.CreatePostRequest(DriveUploadLink & "/files" & "?access_token=" & AccessToken, NV, files)
    hc.Execute(req, 1)

I've tried leaving the FD.ContentType as "multipart/related; boundary=" & boundary but got the same error. I've tried changing the Content Type in the MultiPart Post Module. Nothing takes.

As result on the fileupload you should get return a files-object-resource
with this informations it should be easy to update the metadata with the url for updating metadata!?

That was my last ditch idea but, I can't even accomplish a metadata update without receiving a 'bad request' error.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I i understand the docs (and the b4a-example erel posted) correctly then you have to create a jsonstring with all metadata and put a new file "as attachment". Maybe you first write the jsondata to root.external + "jsondata.json" and then you add a new file-object

B4X:
Dim fd As FileData
    fd.Initialize
    fd.Dir = File.DirRootExternal
    fd.FileName = "jsondata.json"
    fd.KeyName = "upfilejson"
    fd.ContentType = "application/json"
    files.Add(fd)

POST /upload/drive/v2/files?uploadType=multipart HTTP/1.1
Host: www.googleapis.com
Authorization: your_auth_token
Content-Type: multipart/related; boundary="foo_bar_baz"
Content-Length: number_of_bytes_in_entire_request_body

--foo_bar_baz
Content-Type: application/json; charset=UTF-8

{
"title": "My File"
}

--foo_bar_baz
Content-Type: image/jpeg

JPEG data

--foo_bar_baz--


BUT: That all i THINK how it must be. I don´t know. That´s just my thoughts after reading the docs. i NEVER tried to work with google-apis...

Maybe you have a short example (exported project as zip) which can upload a file to google-drive and upload them; i´ll have a look tomorrow at it and try to help with that problem. I need such a function in my apps soon so i´m looking for samples to learn :)
 
Last edited:
Upvote 0

SackMcNuts

Member
Licensed User
Longtime User
That would be great!! Here is the test program that I've been trying with. It's set up using the Credentials from the GMailContactList Example. It will ask once for your Google account credentials to access your Drive.

Button1 (Auth) is for getting OAuth clearance. Trying to transfer will result in an authorization error if not pushed first.

Button2 (Single Transfer) is a single request that semi-works. As long as the Content Type is "image/jpeg" the file reacts properly on Drive (can preview, etc.) but no MetaData is altered. I can't figure out what to add here to edit MetaData. Maybe JSON as you suggested. I kind of tried it but I could have gotten it wrong too. Adding anything before the Access Token results in an authorization error. For instance, in the API Documentation it suggests making it "/files?uploadtype=multipart" or "/files?uploadtype=media."

Button3 (Multi Transfer) is the multipart request that yields the error: Unsupported Media Type

Thank you for your help!!
 

Attachments

  • GoogleDriveTest.zip
    11.6 KB · Views: 334
Upvote 0

SackMcNuts

Member
Licensed User
Longtime User
Success!!... well kind of... more like, Semi-Success!!!

B4X:
Dim hc As HttpClient
    Dim BC As ByteConverter
    Dim j As JSONGenerator
    Dim m As Map
    m.Initialize
    m.Put ("title", "NewTitle.jpg")
    j.Initialize (m)
    Dim b () As Byte
    BC.LittleEndian = False
    b = BC.StringToBytes (j.ToString,"UTF8")

    Dim r As HttpRequest
    r.InitializePut2(DriveLink & "/files/<FILE ID>" & "?access_token=" & AccessToken, b)
    r.SetContentType("application/json")

    hc.Execute(r,1)

If I have the File Id from the file upload Response.GetString, I can change the file properties including Title using the above code.

It would still be nice to handle it all in one request.

I still get "Unsupported Media Type" when trying a MultiPart Post.

Hey, it's a step in a great direction!
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
If I have the File Id from the file upload Response.GetString, I can change the file properties including Title using the above code.

It would still be nice to handle it all in one request.

I still get "Unsupported Media Type" when trying a MultiPart Post.

*thumbs up*
All my tryings yesterday ends in "Unsupported Media Type" too :-/
 
Upvote 0

aviario

Active Member
Licensed User
Longtime User
Hi, is installed as googledrivetest.zip upload a picture called uno.jpg'll google driver folder and upload the file calls it "Untitled" instead of sie uno.jpg the "Untitled" file is renamed me shows well that this problem can occur?

thank
 
Upvote 0
Top