I need to write Apex code to connect with Google Drive and create a Spreadsheet and write data into it.

I am done with google Drive Authentication but now i am not getting how to create Spreadsheet and what scope should i give in Auth request so that i can create/ modify Spreadsheet. Below is my code. Can anyone please help -

public class GoogleDriveController {
    private String code ;
    private string key = '548018328029-3l8hn2ikongosaeudp87he1r6s9rv3jhskl.apps.googleusercontent.com' ;
    private string secret = 'PF1yzvAlhlY71cmUFSSQvPoXjA' ;
    private string redirect_uri = 'https://c.ap2.visual.force.com/apex/GoogleDrivePage' ;

    public GoogleDriveController()
        code = ApexPages.currentPage().getParameters().get('code') ;
        System.debug('codesdd:::' + code);

        if(code != '' && code != null)
            System.debug('code:::' + code);
            AccessToken() ;

    public PageReference DriveAuth()
        PageReference pg = new PageReference(GoogleDriveAuthUri (key , redirect_uri)) ;
        return pg ;

    public String GoogleDriveAuthUri(String Clientkey,String redirect_uri)
        String key = EncodingUtil.urlEncode(Clientkey,'UTF-8');
        String uri = EncodingUtil.urlEncode(redirect_uri,'UTF-8');
        String authuri = '';
        authuri = 'https://accounts.google.com/o/oauth2/v2/auth?'+

        'redirect_uri=' +uri+ 
        return authuri;

    public void AccessToken()
        //Getting access token from google
        HttpRequest req = new HttpRequest();
        req.setHeader('content-type', 'application/x-www-form-urlencoded');
        String messageBody = 'code='+code+'&client_id='+key+'&client_secret='+secret+'&redirect_uri='+redirect_uri+'&grant_type=authorization_code';
        req.setHeader('Content-length', String.valueOf(messageBody.length()));

        Http h = new Http();
        String resp;
        HttpResponse res = h.send(req);
        resp = res.getBody();

        String str = resp;
        List<String> lstStr = str.split(',');
        List<String> lstStr1 = lstStr[0].split(':');
        String st = lstStr1[1].remove('"').trim();
                SpreadsheetService service = new SpreadsheetService();  
        GoogleData sheets = service.getSpreadsheets(); 

    } }
  • 9
    Please tell me that's not really your secret...
    – sfdcfox
    Commented Feb 27, 2016 at 6:03
  • I am pretty sure that this is not SFDC question. Thats google spreadsheet api question. developers.google.com/google-apps/spreadsheets and stackoverflow.com/questions/19361814/…
    – kurunve
    Commented Feb 27, 2016 at 8:02
  • @sfdc I didn't get you... Do u want Secret key? Commented Feb 27, 2016 at 8:03
  • 4
    @PramodKumar No, it's a best practice to remove any passwords/keys/certificates from code that you post publicly.
    – sfdcfox
    Commented Feb 27, 2016 at 8:48
  • @sfdc yes you are right. Actually those are fake one.. Well you for pointing that out Commented Feb 28, 2016 at 11:56

2 Answers 2


If you have access token , you can upload the file on google drive using this apex code

    Http http = new Http();
    req = new HttpRequest();


    req.setHeader('content-type', 'text/csv');
    req.setHeader('Authorization','Bearer '+st);

    String messageBodies = 'Last,First,Mid,Gender,Nationality,DOB,Passport No,Email,Phone';

    HttpResponse respp = http.send(req);

Help from - http://yoforce.blogspot.in/

Where st is your access token Mark it if helpful


No need to do this anymore google and SFDC teamed up to do the work for us.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .