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
Rodolfo NoviskiRodolfo Noviski 

Salesforce Integration with Google Drive

Hello , I 'm trying to accomplish the following request:

    request.setHeader('Authorization', 'Bearer {Token}');
            String scope =  'https://www.googleapis.com/auth/drive,'+
                   'https://www.googleapis.com/auth/drive.file'+
                   'https://www.googleapis.com/auth/drive.appdata'+
                   'https://www.googleapis.com/auth/drive.apps.readonly'+
                   'https://www.googleapis.com/auth/drive.metadata.readonly'+
                   'https://www.googleapis.com/auth/drive.readonly'+
                   'https://www.googleapis.com/auth/drive.scripts';
       
       
        request.setHeader('Content-Type','application/json');
        request.setHeader('User-Agent','salesforce-toolkit-googledata/21');
        request.setHeader('Scope', scope);
        String body = '{\'value\':\'drive.permissions.insert\',\'type\':\'anyone\',\'role\':\'writer\'}';
        request.setBody(body);
        System.debug('Set Endpoint');
        request.setEndpoint(endpoint);
        System.debug('Set Method');
        request.setMethod('POST');

But I do not know how to automatically get the token that is sought. Could anyone help me?

thank you
Sfdc CloudSfdc Cloud
Hi Rodolfo,
                  You need to have App key,App Secret and redirect Uri for sending the request to google drive from your salesforce org.After authorization will get successful you will get access token
//Fetched from URL  
    private String code ;  
    //App key of your App
    private string key = '189819852420-nash4i2joigvcmap3pqhm0jhun3619sh.apps.googleusercontent.com' ;  
    //App Secret of your App
    private string secret = 'zjr1679QYeFAw_SnqjRBSH1q' ;  
   //Url where u need to have acccess token
    private string redirect_uri = 'https://c.na15.visual.force.com/apex/GoogleDrivePage' ;  
      
    public GoogleDriveController()  
    {  
        code = ApexPages.currentPage().getParameters().get('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&'+  
        '&login_hint=jsmith@example.com&'+  
        'access_type=offline';  
        return authuri;  
    }  
      
      
    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();  
          
        System.debug(' You can parse the response to get the access token ::: ' + resp);  
   }
If this helps you out mark it as best ans to help other !!!