function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Srilakshmi B SSrilakshmi B S 

How to create a file inside a folder in google drive?

I am able to create a file in Google drive 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 do in the above code in order to create the file inside a folder?
James LoghryJames Loghry
This is more of a question for the Google Drive API, however, looking at their documentation it looks like your close with your original request.   You'll need to specify the "parents" key in your request body as shown in the example here: https://developers.google.com/drive/web/folder
Prakhar KPrakhar K
This is how I did it in Apex.Hope this helps to anyone who  is in need !!! (creates a CSV file in a folder in google drive).

public  void CreateFile(){
       String boundary = '----------9889464542212';
       String delimiter = '\r\n--' + boundary +'\r\n';
       String close_delim = '\r\n--' + boundary + '--';
       ApexPages.PageReference report = new ApexPages.PageReference('https://cs2.salesforce.com/00OR0000001FMn2?export=0&enc=UTF-8&xf=csv');
       
       String before='';      
       //Get the folder ID having file
       Google_Drive_Info__c g=new Google_Drive_Info__c();
       g=[SELECT Folder_Id__c FROM Google_Drive_Info__c WHERE Name=:'main'];
       String FolderId=g.Folder_Id__c; 
       
       // create a blob from our parameter value before we send it as part of the url
       Blob beforeblob = Blob.valueOf(before);
       String bodyEncoded = EncodingUtil.base64Encode(beforeblob);
       String filename='Sample_fileNameWithoutExtension';
       String filetype='text/csv';
       String body=delimiter+'Content-Type: application/json\r\n\r\n'+'{ "title" : "'+ filename+'",'+ ' "mimeType" : "'+ filetype+ '",' + '"parents":[{"id":"'+ FolderId +'"}] }'+delimiter+'Content-Type: ' + filetype + '\r\n'+'Content-Transfer-Encoding: base64\r\n'+'\r\n'+bodyEncoded+close_delim;
       Http http = new Http();
       HttpRequest req = new HttpRequest();
       req.setEndpoint('https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart&convert=true');
       req.setHeader('Authorization', 'Bearer ' +access_token);
       req.setHeader('Content-Type', 'multipart/mixed; boundary="'+boundary+'"');
       req.setHeader('Content-length', String.valueOf(body.length()));
       req.setBody(body);
       req.setMethod('POST');
       req.setTimeout(60*1000);
       HttpResponse resp = http.send(req);
       filetype='';
       filename='';
   }


Give a like if this solves your purpose!!!
Manu Yadav 25Manu Yadav 25
Hi Prakar,

I want to delete the files which is we created in the google drive folder from the salesforce vf page. do you have any approaches...??

TIA
Vengatesh Pandiyan 7Vengatesh Pandiyan 7
Manu yadav i want to upload a file from sf to gdrive and able to access that file later in the record page i think you have done this before ,, can you share the code please
Vardhman Jain 18Vardhman Jain 18
When I am uploading a file on google drive through salesforce, it is being uploaded as 'UNTITLED'.
Can anyone tell me what should i do if i want it to upload it with the actual name.