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
Jack123Jack123 

salesforce to google drive integration

So my requirement is I want all the salesforce attachment backup in google drive also I can syncronise the salesforce with google drive so that in future if I will upload any attachment in salesforce it will automaticallu added to the google drive

Can anyone help me how we can do this using third party app or any other way we can intergrate salesforce with google drive 
NagendraNagendra (Salesforce Developers) 
Hi Rohit,

You can use the new feature provided by Salesforce for the same which is known as Files Connect.
Please refer to the documentation for the same. Hope this helps.

Please mark this as solved if it's resolved.

Thanks,
Nagendra
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Rohit Kumar,

May I request you please refer the below link for reference on Salesforce to google drive integration. Hope it will be helpful.

Please mark it as best answer if the information is informative.so that question is removed from an unanswered question and appear as a proper solution.

Thanks
Rahul Kumar
Azhar Jamil 5Azhar Jamil 5
Hi Rohit,

You need to first authenticate the credentials from the link https://console.developers.google.com/project
Follow the link for the step to create the project and credentials -- credentials https://help.salesforce.com/articleView?id=000239696&type=1

The second Step you need to create one visual force page, I have added the VF page 
<apex:page controller="GoogleDriveController">
    <style>
    .error {
    font-style: italic;
    font-size: 15px;
    font-weight: bold;
    text-align: center;
    color: green;
    }
    
    .myClass {
    color: black !important;
    background: #87ceeb !important;
    width: 300px;
    font-size: 20px !important;
    height: 35px;
    }
    </style>
    <center>
        <apex:form style="margin-top:5%;margin-left:5%;width:60%">
        <apex:pageblock >
            <apex:commandbutton styleClass="myClass" onclick="this.value = 'Authenticating....'" action="{!DriveAuth}" value="Google Drive Authentication">
            </apex:commandbutton>
            <br/>
            <br/>
            <br/>
            <br/>
            <apex:inputfile value="{!file}" contentType="{!filetype}" filename="{!filename}" />
            <br/>
            <br/>
            <br/>
            <br/>
            <apex:commandButton styleClass="myClass" onclick="this.value = 'Uploading...'" value="Upload file" action="{!UploadFile}" />
            <br/>
            <br/>
            <apex:messages styleClass="error" />
            <br/>
            </apex:pageblock>
        </apex:form>
    </center>
</apex:page>


The third step is you need to create the apex-class and assign the client id and client secret which you will get from google API credentials,
I have attached the apex-class also after following this step you will be authenticated and your file will be uploaded from salesforce to google drive.
public class GoogleDriveController {
    private String code;
    public boolean val {get;set;}
    public blob file { get;set;}
    public String filetype { get;set;}
    public String filename {get;set;}
    private string key = 'Client_id';
    private string secret = 'Client_secret';
    private string redirect_uri = 'https://teqtrailhead-dev-ed--c.ap5.visual.force.com/apex/GoogleDrivePage';
    private String accesstoken;
    private Integer expiresIn;
    private String tokentype;
    public GoogleDriveController() {
        code = ApexPages.currentPage().getParameters().get('code');
        //Get the access token once we have code
        if (code != '' && code != null) {
            System.debug(' code__ ' + code);
            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/v2/auth?'+
        'scope=https://www.googleapis.com/auth/drive&'+
        'state=security_token%3D138r5719ru3e1%26url%3Dhttps://oa2cb.example.com/myHome&'+
        'redirect_uri=' +uri+ 
        '&response_type=code&'+
        'client_id='+key+
        '&access_type=offline';       
        return authuri;
    }
    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()));
        system.debug('body.length()'+body.length());
        system.debug('body>>'+body);
        req.setBody(body);
        req.setMethod('POST');
        req.setTimeout(60 * 1000);
        HttpResponse resp = http.send(req);
        system.debug('resp'+resp);
        file = null;
        filetype = '';
        filename = '';
        
    
    }

    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);
        system.debug('resp>>>'+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);
    }
}

Feel free to ask if you need further clarification.

Mark, it as best answers if you like it.

Thanks! 
Boris FrankBoris Frank
Hi! You can integrate Salesforce and Google Drive via Skyvia (https://skyvia.com/data-integration/integrate-salesforce-google-drive).Skyvia is an integration platform with an intuitive interface. You need only configure your import/export in convenient GUI wizards and use flexible scheduling settings to automate data operation