2

I am able to create a file in Google drive from salesforce with the below code:

Http http = new Http();
HttpRequest req = new HttpRequest();
req.setMethod('POST');
req.setEndpoint('https://www.googleapis.com/upload/drive/v2/files?uploadType=media');
req.setHeader('content-type', 'text/plain');
req.setHeader('Authorization','Bearer '+accessToken);
String messageBody = 'Hi, This message is from Salesforce';
req.setBody(messageBody);  
req.setTimeout(60*1000);
HttpResponse resp = http.send(req); 

But now i want to create a file inside a folder in Google drive. I am using end point as:

req.setEndpoint('https://www.googleapis.com/upload/drive/v2/files/0B602YDdndVQ6b3RnR2NYQXo5TXM/children?uploadType=media');

where 0B602YDdndVQ6b3RnR2NYQXo5TXM is the folder id.

Can someone please tell me what other changes i have to make in the above code in order to create the file inside a folder

  • This question is with respect to Salesforce only as i am trying to create the file in Google drive from Salesforce. – Srilakshmi B S Aug 25 '14 at 12:03
  • This isn't really a good fit for SFSE and should be on StackOverflow as in this case the fact that the call is being made from Salesforce has no bearing on the question at all. However, because it's had a high voted answer and I believe this will be of use to others at some point I'm going to repoen it. – Matt Lacey Aug 26 '14 at 1:27
10

This is not correct. As per Google Drive API, you should be setting the parents parameter in the POST request.

As an example, this POST request will upload the file inside the folder whose id is 0Bz0bd074

POST https://www.googleapis.com/drive/v2/files
Authorization: Bearer {ACCESS_TOKEN}
Content-Type: application/json
...
{
  "title" : "cat.jpg",
  "mimeType" : "image/jpeg",
  "parents": [{
    "kind": "drive#fileLink",
    "id": "0Bz0bd074"
  }]
}
  • So i have to set the body like this right? req.setBody('Title' , 'File1'); req.setBody('mimeType' , 'text/plain'); – Srilakshmi B S Aug 25 '14 at 11:44
  • No, this is not how you set the params in a POST request body. At the bottom of this link - there's a Try It section which will help you understand how to set the various params including parent - developers.google.com/drive/v2/reference/files/insert – Gaurav Kheterpal Aug 25 '14 at 11:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.