+ Start a Discussion
Karunat1Karunat1 

Facing issue to achieve Access Token in Google Drive Integration

Code:
public class GdriveUsingJWT {
    public static String Sinatute_blob;
    public static blob Signature_b;
    public static blob privateKey;
    public string iss;
    public string scope;
    public string aud;
    public Long exp;
    public Long iat;
    
    public static HttpResponse accessToken(){
        http h = new Http();
        Httprequest req = new HttpRequest();
        HttpResponse res = new HttpResponse();
        req.setEndpoint('https://accounts.google.com/o/oauth2/token');
        req.setMethod('POST');
        req.setHeader('ContentType','application/x-www-form-urlencoded');
        
        String Header = '{"alg":"RS256","typ":"JWT"}';
        String Header_Encode = EncodingUtil.base64Encode(blob.valueof(Header));
        
        String claim_set = '{"iss":"driveint@gd-proj-  211813.iam.gserviceaccount.com"';
        claim_set += ',"scope":"https://www.googleapis.com/auth/analytics.readonly"';
        claim_set += ',"aud":"https://accounts.google.com/o/oauth2/token"';
        claim_set += ',"exp":"'+datetime.now().addHours(1).getTime()/1000;
        claim_set += '","iat":"'+datetime.now().getTime()/1000+'"}';
        system.debug('Response ='+claim_set);        
        String claim_set_Encode = EncodingUtil.base64Encode(blob.valueof(claim_set));
        String Singature_Encode = Header_Encode+'.'+claim_set_Encode;
        String key = 'MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDFa951gVKwj+ygqY2JwWKqaKm1G/7lqUAW4e51OXh8Tf+5WXgSfhxXQQXnneHTcQQOp4gDNGmg1u7MGqnIOEHh+5wwul+t4wtlHiV/RWjPcOYAIk/D/9C65KGR3ZZ1ko7VPR3DH/pnXr7UlTIhgVhiT1iKlmhltoNTr9Fqc6io8DxvXvjdms8+YSBJ3doMsPD1YMfcExhbEJxPfXdH1IMALKfEFlUGRyLE38cLJS0bXwoCPCUFLpzVEoOOB3g+yMBBYaXRRjWEuSm+6Vo1N5Mjf2bZyOFOHc62gpenGF99nyKez/aQrvt9ZZA7qTvYio1X+LzWc3aZmA/4/5TmxwQXAgMBAAECggEAVlqdJ4PmYhHKXOpdXVzIJib62Jw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5bbEvR32Pc06iT0FrHQtJox3i1k1KU8LW213i1NWG926DVHQV5laMdVVS03dh8TZIyeLcuJDcHI8J+cVSKOuWdVvLn5NE7Y7BAPJgT8lbGTM0UPVi2GE+If0Z/I';
        privateKey = EncodingUtil.base64Decode(key);
        system.debug('Singature_Input ='+privateKey);
        Singature_Encode = Singature_Encode.replaceAll('=','');
        String Singature_Encode_Url = EncodingUtil.urlEncode(Singature_Encode,'UTF-8');
        Signature_b =   blob.valueof(Singature_Encode_Url);
        Sinatute_blob = base64(Crypto.sign('RSA-SHA256', Signature_b , privateKey));
        system.debug('Singature_blobinatute_blob'+Sinatute_blob);
        String JWT = Singature_Encode+'.'+Sinatute_blob;
        system.debug('JWT@@'+JWT);
        JWT = JWT.replaceAll('=','');
        
        String grt = 'urn:ietf:params:oauth:grant-type:jwt-bearer';
        req.setBody('grant_type='+grt+'&assertion='+JWT);
        res = h.send(req);
        if (res.getStatusCode() == 200) {
            String Response = res.getBody();
            system.debug('@@@response'+ Response);
            Integer stuscode = res.getStatusCode();
            system.debug('@@@statuccode'+ stuscode);
            Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(res.getBody());
            system.debug('result='+ results );
            List<Object> values = (List<Object>) results.get('values');
            System.debug('@@@values are:'+ values);
            //system.debug('result='+ results );
            //Map<String, String> accessMap= new Map<String, String>();
         }
        return res;
    }
    private static String base64(Blob b) {
        String ret = EncodingUtil.base64Encode(b);
        system.debug('@@@##blob@@'+ b);
        system.debug('@@@rett'+ret);
        ret = ret.replaceAll('\\+', '-');
        ret = ret.replaceAll('/', '_');
        ret = ret.replaceAll('=', '');
        system.debug('@@@rett'+ret);
        return ret;
    }
}
I want to create Folder in Google Drive







 
Khan AnasKhan Anas (Salesforce Developers) 
Hi Karuna,

Hope the day is treating you well.

Please refer to the below awesome blog by Shruti Sridharan which contains step by step procedure for Gdrive integration with Salesforce which might help you further with the above issue.

https://shrutisridharan.wordpress.com/2017/11/23/yet-another-gdrive-integration-for-salesforce/


I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in future.

Thanks and Regards,
Khan Anas