1

we are using apex to upload a file to google drive's folder and gave folder id and sending the request. after debugging the http request, we are able to see request is good and folder id taken is correct. but file is not uploaded inside the folder but outside the folder and inside the drive else where. Please help us know the mistake. Not able to understand where is the issue.

Here is the code. `

           String boundary = '----------9889464542212';
           String delimiter = '\r\n--' + boundary +'\r\n';
           String close_delim = '\r\n--' + boundary + '--';
           String bodyEncoded = EncodingUtil.base64Encode(att.Body);
           String    FolderId= '1U0vuP53dpIAM_WlmbJfvwvMCK0XZREOo';  

       String body=delimiter+'Content-Type: application/pdf\r\n\r\n'+'{ "title" : "'+ filename+'",'+' "mimeType" : "' + att.ContentType+ '",'+ '"parents":[{"id":"'+ FolderId +'"}] }' + delimiter+'Content-Type: ' + att.ContentType +'\nContent-Transfer-Encoding: base64\r\n'+'\r\n'+ '\r\n'+bodyEncoded+close_delim;

   `

Here is the request : `------------9889464542212 Content-Type: application/pdf

{ "title" : "tester 83_Resume", "mimeType" : "application/pdf","parents":[{"id":"1U0vuP53dpIAM_WlmbJfvwvMCK0XZREOo"}] } ------------9889464542212 Content-Type: application/pdf Content-Transfer-Encoding: base64 `

Here is the response with different parent id compared to request : "createdDate": "2018-01-17T11:05:15.699Z", "modifiedDate": "2018-01-17T11:05:15.699Z", "modifiedByMeDate": "2018-01-17T11:05:15.699Z", "lastViewedByMeDate": "2018-01-17T11:05:15.699Z", "markedViewedByMeDate": "1970-01-01T00:00:00.000Z", "version": "2", "parents": [ { "kind": "drive#parentReference", "id": "0ABNzkfZayN4oUk9PVA", "selfLink": "https://www.googleapis.com/drive/v2/files/1SBy8FSshEm9FgHdX_9cP9ZY0bbRiw9Ni/parents/0ABNzkfZayN4oUk9PVA", "parentLink": "https://www.googleapis.com/drive/v2/files/0ABNzkfZayN4oUk9PVA", "isRoot": true } ],

vardhan
  • 53
  • 7

1 Answers1

0

Got about this same issue. It was uploaded to the root drive directory, instead of the parent you supplied. It looks like your parents content json is allright. Looks like you are using an multipart upload, there could be an problem with that. The base request below generated with okhttp3 on Android seems to be working. This is an plain base request, without any library used.

Working request:

POST  https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart
Accept: application/json
Content-Type: multipart/related
Authorization: Bearer ya29.c.El__BV-q0dMeaVNil2kcVYUkpZmeEW85rjV6hRdgsoIeXbl-pFeEAaJuqcUSwok63JcnKg
body: --hkj_multipart_data
Content-Disposition: form-data; name="metadata"
Content-Type: application/json; charset=utf-8
Content-Length: 298

{"title":"3628360894d8f16bc-08c9-4021-b417-d18f693a6ee2_20180707033608.JPG","mimeType":"image/jpeg","description":"AndroidTest uploaded 3628360894d8f16bc-08c9-4021-b417-d18f693a6ee2_20180707033608.JPG","parents":[{"id":"1w8APlCLvop42O3DiddkITKc63zGk0Yt4A"}]}
--hkj_multipart_data
Content-Disposition: form-data; name="file"
Content-Transfer-Encoding: base64
Content-Type: image/jpeg; charset=utf-8
Content-Length: 5339083

[BASE64 ENCODED STRING]/9j/4TUMRXhpZgAASUkqAAgAAAAMAAABBAABAAAAwBQAAAEBBAABAAAArAsAAA8BAgAIAAAAngAA
ABABAgAJAAAApgAAABIBAwABAAAABgAAABoBBQABAAAA0gAAABsBBQABAAAA2gAAACgBAwABAAAA
AgAAADEBAgAOAAAAsAAAADIBAgAUAAAAvgAAABMCAwABAAAAAQAAAGmHBAABAAAA4gAAAB4DAABz
YW1zdW5nAFNNLUc5MzBGAABHOTIwRlhYVTVFUUU3ADIwMTg6MDc6MDcgMTU6MzY6MDgASAAAAAEA
AABIAAAAAQAAABo

Response:

{
 "kind": "drive#file",
 "id": "1PTU_N4JEd8EIpj31bsNDMOx5et15VJW",
 "etag": "\"Kk_83xxobVILlgYUB8xd9E4tSOE/ZCHdcOb2Zhti8IgKqfYv\"",
 "selfLink": "https://www.googleapis.com/drive/v2/files/1PTU_N4JEd8EIpj31bsNDMOx5et15VJW",
 "webContentLink": "https://drive.google.com/uc?id=1PTU_N4JEd8EIpj31bsNDMOx5et15VJW&export=download",
 "alternateLink": "https://drive.google.com/file/d/1PTU_N4JEd8EIpj31bsNDMOx5et15VJW/view?usp=drivesdk",
 "embedLink": "https://drive.google.com/file/d/1PTU_N4JEd8EIpj31bsNDMOx5et15VJW/preview?usp=drivesdk",
 "iconLink": "https://drive-thirdparty.googleusercontent.com/16/type/image/jpeg",
 "thumbnailLink": "https://lh3.googleusercontent.com/7Zd1aZsFxdVackbhnAqaJrc5geBSrqQOaJUXaywQMRjRr9i6ml56k7u0Cbow_Y6FAHpWmLdYvrKSo=s220",
 "title": "3628360894d8f16bc-08c9-4021-b417-d18f693a6ee2_20180707033608.JPG",
 "mimeType": "image/jpeg",
 "description": "AndroidTest uploaded 3628360894d8f16bc-08c9-4021-b417-d18f693a6ee2_20180707033608.JPG",
 "copyRequiresWriterPermission": false,
 "createdDate": "2018-07-21T08:47:13.955Z",
 "modifiedDate": "2018-07-21T08:47:13.955Z",
 "version": "2",
 "parents": [
  {
   "kind": "drive#parentReference",
   "id": "1w8APlCLvop42O3DiddkITKc63zGk0Yt4A",
   "selfLink": "https://www.googleapis.com/drive/v2/files/1PTU_N4JEd8EIpj31bsNDMOx5et15VJW/parents/1w8APlCLvop42O3DiddkITKc63zGk0Yt4A",
   "parentLink": "https://www.googleapis.com/drive/v2/files/1w8APlCLvop42O3DiddkITKc63zGk0Yt4A",
   "isRoot": false
  }
 ],
 "downloadUrl": "https://doc-0g-84-docs.googleusercontent.com/docs/securesc/bfl87khmp0ji0o8234fefb9j8tg2e8eq/0afho4f4lpjh2fc86pamub3m932ml45e/1532160000000/3453454378456732326/3453454378456732326/1PTU_N4JEd8EIpj31bsNDMOx5et15VJW?e=download&gd=true",
 "userPermission": {
  "kind": "drive#permission",
  "etag": "\"Kk_83xxobVILlgYUB8xd9E4tSOE/t3z1cT1lHlrhFSOpyQWJMD-ZlW8\"",
  "id": "me",
  "selfLink": "https://www.googleapis.com/drive/v2/files/1PTU_N4JEd8EIpj31bsNDMOx5et15VJW/permissions/me",
  "role": "owner",
  "type": "user"
 },
 "originalFilename": "3628360894d8f16bc-08c9-4021-b417-d18f693a6ee2_20180707033608.JPG",
 "fileExtension": "JPG",
 "md5Checksum": "67c38d5121b983b9be4bad3d291f1ec5",
 "fileSize": "3952306",
 "quotaBytesUsed": "3952306",
 "ownerNames": [
  "nameoftheserviceaccount@norme-naming-space.iam.gserviceaccount.com"
 ],
 "owners": [
  {
   "kind": "drive#user",
   "displayName": "nameoftheserviceaccount@norme-naming-space.iam.gserviceaccount.com",
   "isAuthenticatedUser": true,
   "permissionId": "3453454378456732326",
   "emailAddress": "nameoftheserviceaccount@norme-naming-space.iam.gserviceaccount.com"
  }
 ],
 "lastModifyingUserName": "nameoftheserviceaccount@norme-naming-space.iam.gserviceaccount.com",
 "lastModifyingUser": {
  "kind": "drive#user",
  "displayName": "nameoftheserviceaccount@norme-naming-space.iam.gserviceaccount.com",
  "isAuthenticatedUser": true,
  "permissionId": "3453454378456732326",
  "emailAddress": "nameoftheserviceaccount@norme-naming-space.iam.gserviceaccount.com"
 },
 "capabilities": {
  "canCopy": true,
  "canEdit": true
 },
 "editable": true,
 "copyable": true,
 "writersCanShare": true,
 "shared": true,
 "explicitlyTrashed": false,
 "appDataContents": false,
 "headRevisionId": "0B277NX55uQ91SkY4NVRxMSs5cWNDQWdPckwwMTlEWVFwNExBPQ",
 "imageMediaMetadata": {
  "width": 5312,
  "height": 2988,
  "rotation": 1,
  "date": "2018:07:07 15:36:08",
  "cameraMake": "samsung",
  "cameraModel": "SM-G930F",
  "exposureTime": 0.02,
  "aperture": 1.9,
  "flashUsed": false,
  "focalLength": 4.3,
  "isoSpeed": 200,
  "meteringMode": "CenterWeightedAverage",
  "exposureMode": "Auto",
  "colorSpace": "sRGB",
  "whiteBalance": "Auto",
  "exposureBias": 0.0,
  "maxApertureValue": 1.85
 },
 "spaces": [
  "drive"
 ]
}

Java android code (okhttp 3.10.0) to generate the request:

String fileMimeType = "image/jpeg";
String metaDataFile = "{\"title\":\"" + filename + "\"," +
                    "\"mimeType\":\"" + fileMimeType + "\"," +
                    "\"description\":\"" +account.getName() + " uploaded " + filename + "\"," +
                    "\"parents\":[{\"id\":\"" + account.getUploadFolderID() + "\"}]" +
                    "}";
RequestBody requestBodyMetaData = RequestBody.create(JSON, metaDataFile);
RequestBody requestBodyFile = RequestBody.create(fileMimeType, BitmapUtils.getBase64(sourceFile));
MultipartBody.Part requestBodyFilePart = MultipartBody.Part.create(Headers.of("Content-Disposition", "form-data; name=\"file\"", "Content-Transfer-Encoding", "base64"), requestBodyFile);

RequestBody requestBodyMultipart = new MultipartBody.Builder("hkj_multipart_data")
        .setType(MultipartBody.FORM)
        .addFormDataPart("metadata", null, requestBodyMetaData)
        .addPart(requestBodyFilePart)
        .build();
request = new Request.Builder()
        .url(HKJConstants.GOOGLE_UPLOAD_API_SERVER_BASE_URL + "files?uploadType=multipart")
        .header("Accept", "application/json")
        .header("Content-Type", "multipart/related")
        .addHeader("Authorization", "Bearer " + accessToken)
        .post(requestBodyMultipart)
        .build();
Call call = okHttpClient.newCall(request);

Response response = call.execute();

if (response.isSuccessful()) {
    GoogleDriveFileList googleDriveFileListResult = new Gson().fromJson(response.body().charStream(), GoogleDriveFileList.class);
    if(googleDriveFileListResult.IsFilled()){
        return true;
    }
}
BramscoChill
  • 383
  • 4
  • 17