Last active
January 6, 2019 13:56
-
-
Save tushar30/c220d84a49a5a70e6629ce17aa851d37 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public String getAccessToken() { | |
// Ignore the File upload part and "jwt.pkcs" if you used a Salesforce certificate to sign up | |
// for an Einstein Platform account | |
ContentVersion base64Content = [SELECT Title, VersionData FROM ContentVersion where Title='einstein_platform' OR Title='predictive_services' ORDER BY Title LIMIT 1]; | |
String keyContents = base64Content.VersionData.tostring(); | |
keyContents = keyContents.replace('-----BEGIN RSA PRIVATE KEY-----', ''); | |
keyContents = keyContents.replace('-----END RSA PRIVATE KEY-----', ''); | |
keyContents = keyContents.replace('\n', ''); | |
// Get a new token | |
JWT jwt = new JWT('RS256'); | |
// jwt.cert = 'JWTCert'; // Uncomment this if you used a Salesforce certificate to sign up for an Einstein Platform account | |
jwt.pkcs8 = keyContents; // Comment this if you are using jwt.cert | |
jwt.iss = 'developer.force.com'; | |
jwt.sub = '<Your Email Address>'; | |
jwt.aud = 'https://api.einstein.ai/v2/oauth2/token'; | |
jwt.exp = '3600'; | |
String access_token = JWTBearerFlow.getAccessToken('https://api.einstein.ai/v2/oauth2/token', jwt); | |
return access_token; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment