1

I Used below code after accessing client id and secret key but i am not able to upload file on google drive.

Apex code

public class GoogleDriveController{
    //Fetched from URL
    private String code ;
    public blob file { get; set; }
   // public String type{get; set;}
    public String filetype{get;set;}
    public  String filename{get; set;}

    private string key = '377203344030-3ckrlt2adh2d99sneggu4tm2dk18hrvj.apps.googleusercontent.com' ;
    private string secret = 'rmvtApvecwuRoHiTW8mMZEoJ' ;
    private string redirect_uri = 'https://ap1.salesforce.com/apex/GoogleDrivePage' ;
    private String accesstoken ;
    private  Integer expiresIn ;
    private String tokentype;

    public GoogleDriveController()
    {
        code = ApexPages.currentPage().getParameters().get('code') ;
        System.debug('----------------------------------------------------------------'+code);
        //Get the access token once we have code
        if(code != '' && code != null)
        {
            AccessToken() ;
        }
    }

    public PageReference DriveAuth()
    {
        //Authenticating
        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/auth?'+
        'client_id='+key+
        '&response_type=code'+
        '&scope=https://www.googleapis.com/auth/drive'+
        '&redirect_uri='+uri+
        '&state=security_token%3D138r5719ru3e1%26url%3Dhttps://oa2cb.example.com/myHome&'+
        '&[email protected]&'+
        'access_type=offline';
        return authuri;
    }
    public  PageReference UploadFile()
    {


      System.debug('----------------fn---------------'+filename);
      System.debug('----------------ft---------------'+filetype);
      System.debug('------------------at-------------'+accessToken);
       //String parent_id = '1121945745';
        String boundary = '-------314159265358979323846';
        String delimiter = '\r\n--' + boundary +'\r\n';
        String close_delim = '\r\n--' + boundary + '--';
        String bodyEncoded = EncodingUtil.base64Encode(file);
       System.debug('--------------be-----------------'+bodyEncoded);

        String body=
        delimiter +
       'Content-Type: application/json\r\n\r\n' + 
       '{ "title" : "'+ filename+'",' +
        ' "mimeType" : "'+ filetype+ '" }'  + 
        delimiter +
       'Content-Type: ' + filetype + '\r\n' +
       'Content-Transfer-Encoding: base64\r\n' +
       '\r\n' +
        bodyEncoded +
        close_delim;

        System.debug('--------body is-------------'+body);




    HttpRequest req = new HttpRequest();
    req.setEndpoint('https://www.googleapis.com/upload/drive/v2/files');

    req.setHeader('Authorization', 'Bearer' +accessToken);
    req.setHeader('Content-Type', 'multipart/mixed; boundary="'+boundary+'"'); 
    req.setHeader('Content-length', String.valueOf(body.length()));
    req.setHeader('Content-Encoding','base64');
    req.setBody(body);
    req.setMethod('POST');
    req.setTimeout(60*1000);
    // Http http = new Http();
    // HTTPResponse res = http.send(req);
     HttpResponse res = null;
     http h= new Http();
     res= h.send(req);
     System.debug('---------------------'+res.getbody());
     return null;

    //string sUrl = 'https://accounts.google.com';
    //req.setHeader('Content-Type','multipart/form-data; boundary='+boundary);
    // req.setHeader('Content-Length',String.valueof(req.getBodyAsBlob().size()));
    // req.setHeader('Content-Length',String.valueof(body.length()));
    //req.setTimeout(60000); 
    //  req.setHeader('Content-Encoding','base64');
    }

    public void AccessToken()
    {
        //Getting access token from google
        HttpRequest req = new HttpRequest();
        req.setMethod('POST');
        req.setEndpoint('https://accounts.google.com/o/oauth2/token');
        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()));
        req.setBody(messageBody);
        req.setTimeout(60*1000);


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




        JSONParser parser = JSON.createParser(resp);
        while (parser.nextToken() != null) {
        if ((parser.getCurrentToken() == JSONToken.FIELD_NAME)){
        String fieldName = parser.getText();
        parser.nextToken();
        if(fieldName == 'access_token')
        {
            accesstoken = parser.getText();
        } 
        else if(fieldName == 'expires_in')
        {
            expiresIn = parser.getIntegerValue();
        }
        else if(fieldname=='token_type')
        {
            tokentype=parser.getText();
        }
        }
  }   
        System.debug(' You can parse the response to get the access token ::: ' + resp);
  }
}

Visualforce page:

<apex:page controller="GoogleDriveController">
<apex:form >
    <apex:pageblock >
        <apex:commandbutton action="{!DriveAuth}" value="Google Drive Authentication" >
             <!-- <apex:inputFile value="{!attachment.body}"  id="file"/>-->
    </apex:commandbutton></apex:pageblock>

    <apex:inputfile value="{!file}" contentType="{!filetype}"  filename="{!filename}"/>
    <apex:commandButton value="Upload fiel" action="{!UploadFile}"/>
</apex:form>
</apex:page>

What's wrong in above code?

1
  • what is the error you are getting ? any debug log to see ?
    – Avidev9
    Commented Jun 11, 2014 at 8:55

2 Answers 2

2


I have uploaded, use below code.

public  void UploadFile(){
       String boundary = '----------9889464542212';
       String delimiter = '\r\n--' + boundary +'\r\n';
       String close_delim = '\r\n--' + boundary + '--';
       String bodyEncoded = EncodingUtil.base64Encode(file);
       String body=delimiter+'Content-Type: application/json\r\n\r\n'+'{ "title" : "'+ filename+'",'+' "mimeType" : "'+ filetype+ '" }'+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');
       req.setHeader('Authorization', 'Bearer ' +accessToken);
       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);
       system.debug('@@@@@@@---'+resp.getbody());
       file =null;
       filetype='';
       filename='';
   } 


Thanks

1
  • Rajesh, could you also explain why this code works ? Commented Feb 15, 2016 at 7:25
0
req.setHeader('Authorization', 'Bearer' +accessToken);

in the above line of code put a space after 'Bearer', like below:

req.setHeader('Authorization', 'Bearer ' +accessToken);
1
  • looks like a fine answer to me ? Commented Feb 15, 2016 at 7:26

You must log in to answer this question.

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