You need to sign in to do that
Don't have an account?
Sathik
How to intergrate Google calender API in Salesforce using JWT to retrive Oauth 2.0 token
I have tried with following code but Am getting following error {"error" : "invalid_grant"}.
if any one have solutions,pls let me know
public class Calender {
public void Conn() {
http h = new Http();
Httprequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
req.setEndpoint('https://accounts.google.com/o/oauth2/token');
req.setMethod('GET');
req.setHeader('ContentType','application/x-www-form-urlencoded');
String Header = '{"alg":"RS256","typ":"JWT"}';
String Header_Encode = EncodingUtil.base64Encode(blob.valueof(Header));
// system.debug('Response ='+Header_Encode);
// https://www.googleapis.com/auth/calender.readonly
String claim_set = '{"iss":"119812430492-1olmin1inmkav7ek4tbmkk7cmc3htf41@developer.gserviceaccount.com"';
claim_set += ',"scope":"https://www.googleapis.com/auth/calender.readonly"';
claim_set += ',"aud":"https://accounts.google.com/o/oauth2/token"';
claim_set += ',"exp":1328554385';
claim_set += ',"iat":1328550785}';
//String repChar = base64(blob.valueof(claim_set));
// claim_set += ',"exp":"'+datetime.now().addHours(1).getTime()/1000+'"';
// claim_set += '","iat":"'+datetime.now().getTime()/1000+'"}';
// system.debug('Response ='+claim_set);
// String UFTSTR = EncodingUtil.urlEncode(claim_set,'UTF-8');EncodingUtil.base64Encode
String claim_set_Encode = base64(blob.valueof(claim_set));
// system.debug('Response ='+claim_set_Encode);
String Singature_Encode = Header_Encode+'.'+claim_set_Encode;
// system.debug('Singature_Input ='+Singature_Encode);
String key = 'MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAL0Gla03iExJR6iI\n/b9KRtQoUR8ta8uhxOF3JVaiW7BVU5+3u5iIi52T7SQyxhci9lFZaVJLzdWA83wM\nn99Wvbk640KS0D5YR3F87FCovBbv/rbBJIzxoIrgS5iC1MYP+T1PUrLH2JIe+tCb\nYJ8gDixmKhSXPrqrWbmNoPH8QENlAgMBAAECgYAk0gmJ6k1UftnEjJrRDkjpvsi+\nh/x9eA9/09OkIpA1swlNDV8vgTmhAlWpPXK0wwy0H/SSIU4BDtNfEJ57xMvD2Uol\nqpjeui+5f6RfqA1U5kLzOruRlTYuEfG2XNC5pSuMci+u4J0GEB1uRVxG3YnlEXNV\nOmd78/VBazxY8QNXqQJBAOkoEKQYKCnArmNiic1J9X55sh41rQB/i4CMlrl/C+rY\nch4kk0kEdezRjZS3Ckhte+es317+CI975PpTXwsNnBcCQQDPi6ZOhdjB6Ksg9eDB\n6xKm/BCDrJKFSYUBqRpH7ewUa3rS7V6oQrXcgPeR3HjaBNLCGcUq18UXJlsAraAm\nad3jAkB3Imi2YD2NcA3rbHs7MVJGaMpxkz2t5n4SlkOhzt/5BNXyvv+fLK9Q7ZZp\nacnQTlkkfPm54RXgsw7CNf13eWHLAkEAl71HMwPCzUq5yxhZYg3nDjUeMdb7LP3q\ng6NJ9qrpvqgyHYK0gRp88iFMX9UsemGqYYUL1V352AoP4LgQbeVa9wJABz81deGp\nhBkpuJkooNODSSeLy9cXYCxEZc+o8kQRsuEkuGZda2kXgDvumWjD35en26hbjag3\nMSMW6kFgoa/nfA\u003d\u003d\n';
blob privateKey = EncodingUtil.base64Decode(key);
// system.debug('Singature_Input ='+privateKey);
Singature_Encode = Singature_Encode.replaceAll('=','');
String Singature_Encode_Url = EncodingUtil.urlEncode(Singature_Encode,'UTF-8');
blob Signature_b = blob.valueof(Singature_Encode_Url);
String Sinatute_blob = base64(Crypto.sign('RSA-SHA256', Signature_b , privateKey));
String JWT = Singature_Encode+'.'+Sinatute_blob;
JWT = JWT.replaceAll('=','');
// system.debug('JWT ='+JWT);
String grt = 'urn:ietf:params:oauth:grant-type:jwt-bearer';
req.setBody('grant_type='+grt+'&assertion='+JWT);
res = h.send(req);
String Response = res.getBody() +' '+ res.getStatusCode();
system.debug('Response ='+Response);
}
private String base64(Blob b) {
String ret = EncodingUtil.base64Encode(b);
ret = ret.replaceAll('\\+', '-');
ret = ret.replaceAll('/', '_');
ret = ret.replaceAll('=', '');
return ret;
}
}
Json Private key:"-----BEGIN PRIVATE KEY-----\nMIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAL0Gla03iExJR6iI\n/b9KRtQoUR8ta8uhxOF3JVaiW7BVU5+3u5iIi52T7SQyxhci9lFZaVJLzdWA83wM\nn99Wvbk640KS0D5YR3F87FCovBbv/rbBJIzxoIrgS5iC1MYP+T1PUrLH2JIe+tCb\nYJ8gDixmKhSXPrqrWbmNoPH8QENlAgMBAAECgYAk0gmJ6k1UftnEjJrRDkjpvsi+\nh/x9eA9/09OkIpA1swlNDV8vgTmhAlWpPXK0wwy0H/SSIU4BDtNfEJ57xMvD2Uol\nqpjeui+5f6RfqA1U5kLzOruRlTYuEfG2XNC5pSuMci+u4J0GEB1uRVxG3YnlEXNV\nOmd78/VBazxY8QNXqQJBAOkoEKQYKCnArmNiic1J9X55sh41rQB/i4CMlrl/C+rY\nch4kk0kEdezRjZS3Ckhte+es317+CI975PpTXwsNnBcCQQDPi6ZOhdjB6Ksg9eDB\n6xKm/BCDrJKFSYUBqRpH7ewUa3rS7V6oQrXcgPeR3HjaBNLCGcUq18UXJlsAraAm\nad3jAkB3Imi2YD2NcA3rbHs7MVJGaMpxkz2t5n4SlkOhzt/5BNXyvv+fLK9Q7ZZp\nacnQTlkkfPm54RXgsw7CNf13eWHLAkEAl71HMwPCzUq5yxhZYg3nDjUeMdb7LP3q\ng6NJ9qrpvqgyHYK0gRp88iFMX9UsemGqYYUL1V352AoP4LgQbeVa9wJABz81deGp\nhBkpuJkooNODSSeLy9cXYCxEZc+o8kQRsuEkuGZda2kXgDvumWjD35en26hbjag3\nMSMW6kFgoa/nfA\u003d\u003d\n-----END PRIVATE KEY-----\n"
if any one have solutions,pls let me know
public class Calender {
public void Conn() {
http h = new Http();
Httprequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
req.setEndpoint('https://accounts.google.com/o/oauth2/token');
req.setMethod('GET');
req.setHeader('ContentType','application/x-www-form-urlencoded');
String Header = '{"alg":"RS256","typ":"JWT"}';
String Header_Encode = EncodingUtil.base64Encode(blob.valueof(Header));
// system.debug('Response ='+Header_Encode);
// https://www.googleapis.com/auth/calender.readonly
String claim_set = '{"iss":"119812430492-1olmin1inmkav7ek4tbmkk7cmc3htf41@developer.gserviceaccount.com"';
claim_set += ',"scope":"https://www.googleapis.com/auth/calender.readonly"';
claim_set += ',"aud":"https://accounts.google.com/o/oauth2/token"';
claim_set += ',"exp":1328554385';
claim_set += ',"iat":1328550785}';
//String repChar = base64(blob.valueof(claim_set));
// claim_set += ',"exp":"'+datetime.now().addHours(1).getTime()/1000+'"';
// claim_set += '","iat":"'+datetime.now().getTime()/1000+'"}';
// system.debug('Response ='+claim_set);
// String UFTSTR = EncodingUtil.urlEncode(claim_set,'UTF-8');EncodingUtil.base64Encode
String claim_set_Encode = base64(blob.valueof(claim_set));
// system.debug('Response ='+claim_set_Encode);
String Singature_Encode = Header_Encode+'.'+claim_set_Encode;
// system.debug('Singature_Input ='+Singature_Encode);
String key = 'MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAL0Gla03iExJR6iI\n/b9KRtQoUR8ta8uhxOF3JVaiW7BVU5+3u5iIi52T7SQyxhci9lFZaVJLzdWA83wM\nn99Wvbk640KS0D5YR3F87FCovBbv/rbBJIzxoIrgS5iC1MYP+T1PUrLH2JIe+tCb\nYJ8gDixmKhSXPrqrWbmNoPH8QENlAgMBAAECgYAk0gmJ6k1UftnEjJrRDkjpvsi+\nh/x9eA9/09OkIpA1swlNDV8vgTmhAlWpPXK0wwy0H/SSIU4BDtNfEJ57xMvD2Uol\nqpjeui+5f6RfqA1U5kLzOruRlTYuEfG2XNC5pSuMci+u4J0GEB1uRVxG3YnlEXNV\nOmd78/VBazxY8QNXqQJBAOkoEKQYKCnArmNiic1J9X55sh41rQB/i4CMlrl/C+rY\nch4kk0kEdezRjZS3Ckhte+es317+CI975PpTXwsNnBcCQQDPi6ZOhdjB6Ksg9eDB\n6xKm/BCDrJKFSYUBqRpH7ewUa3rS7V6oQrXcgPeR3HjaBNLCGcUq18UXJlsAraAm\nad3jAkB3Imi2YD2NcA3rbHs7MVJGaMpxkz2t5n4SlkOhzt/5BNXyvv+fLK9Q7ZZp\nacnQTlkkfPm54RXgsw7CNf13eWHLAkEAl71HMwPCzUq5yxhZYg3nDjUeMdb7LP3q\ng6NJ9qrpvqgyHYK0gRp88iFMX9UsemGqYYUL1V352AoP4LgQbeVa9wJABz81deGp\nhBkpuJkooNODSSeLy9cXYCxEZc+o8kQRsuEkuGZda2kXgDvumWjD35en26hbjag3\nMSMW6kFgoa/nfA\u003d\u003d\n';
blob privateKey = EncodingUtil.base64Decode(key);
// system.debug('Singature_Input ='+privateKey);
Singature_Encode = Singature_Encode.replaceAll('=','');
String Singature_Encode_Url = EncodingUtil.urlEncode(Singature_Encode,'UTF-8');
blob Signature_b = blob.valueof(Singature_Encode_Url);
String Sinatute_blob = base64(Crypto.sign('RSA-SHA256', Signature_b , privateKey));
String JWT = Singature_Encode+'.'+Sinatute_blob;
JWT = JWT.replaceAll('=','');
// system.debug('JWT ='+JWT);
String grt = 'urn:ietf:params:oauth:grant-type:jwt-bearer';
req.setBody('grant_type='+grt+'&assertion='+JWT);
res = h.send(req);
String Response = res.getBody() +' '+ res.getStatusCode();
system.debug('Response ='+Response);
}
private String base64(Blob b) {
String ret = EncodingUtil.base64Encode(b);
ret = ret.replaceAll('\\+', '-');
ret = ret.replaceAll('/', '_');
ret = ret.replaceAll('=', '');
return ret;
}
}
Json Private key:"-----BEGIN PRIVATE KEY-----\nMIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAL0Gla03iExJR6iI\n/b9KRtQoUR8ta8uhxOF3JVaiW7BVU5+3u5iIi52T7SQyxhci9lFZaVJLzdWA83wM\nn99Wvbk640KS0D5YR3F87FCovBbv/rbBJIzxoIrgS5iC1MYP+T1PUrLH2JIe+tCb\nYJ8gDixmKhSXPrqrWbmNoPH8QENlAgMBAAECgYAk0gmJ6k1UftnEjJrRDkjpvsi+\nh/x9eA9/09OkIpA1swlNDV8vgTmhAlWpPXK0wwy0H/SSIU4BDtNfEJ57xMvD2Uol\nqpjeui+5f6RfqA1U5kLzOruRlTYuEfG2XNC5pSuMci+u4J0GEB1uRVxG3YnlEXNV\nOmd78/VBazxY8QNXqQJBAOkoEKQYKCnArmNiic1J9X55sh41rQB/i4CMlrl/C+rY\nch4kk0kEdezRjZS3Ckhte+es317+CI975PpTXwsNnBcCQQDPi6ZOhdjB6Ksg9eDB\n6xKm/BCDrJKFSYUBqRpH7ewUa3rS7V6oQrXcgPeR3HjaBNLCGcUq18UXJlsAraAm\nad3jAkB3Imi2YD2NcA3rbHs7MVJGaMpxkz2t5n4SlkOhzt/5BNXyvv+fLK9Q7ZZp\nacnQTlkkfPm54RXgsw7CNf13eWHLAkEAl71HMwPCzUq5yxhZYg3nDjUeMdb7LP3q\ng6NJ9qrpvqgyHYK0gRp88iFMX9UsemGqYYUL1V352AoP4LgQbeVa9wJABz81deGp\nhBkpuJkooNODSSeLy9cXYCxEZc+o8kQRsuEkuGZda2kXgDvumWjD35en26hbjag3\nMSMW6kFgoa/nfA\u003d\u003d\n-----END PRIVATE KEY-----\n"
You must also have got this as resolved from your followed question: https://developer.salesforce.com/forums/ForumsMain?id=906F000000093E4
Thank you so much.I have made coneection