I am trying to implement the sentiment api but having issue while generating the access token.

I have uploaded einstein_platform.pem file in Files in Salesforce.

ContentVersion con = [SELECT  Title
        FROM    ContentVersion
        WHERE   Title = 'einstein_platform'
        OR      Title = 'predictive_services'
        ORDER BY Title
        LIMIT 1];

    String key = con.VersionData.tostring();
    key = key.replace( '-----BEGIN RSA PRIVATE KEY-----', '' );
    key = key.replace( '-----END RSA PRIVATE KEY-----', '' );
    key = key.replace( '\n', '' );
    JWT jwt = new JWT( 'RS256' );
    jwt.pkcs8 = key;
    jwt.iss = 'developer.force.com';
    jwt.sub = 'demo@gmail.com'; // Update with your own email ID
    jwt.aud = 'https://api.einstein.ai/v2/oauth2/token';
    jwt.exp = String.valueOf(3600);
    String access_token = JWTBearerFlow.getAccessToken( 'https://api.einstein.ai/v2/oauth2/token', jwt );
    String keyaccess = access_token;

Output: 18:34:11:987 USER_DEBUG [21]|DEBUG|******keyaccess**null


In addition to storing the einstein_platform.pem file you also need to update the following line with the email address you used when creating the Einstein account.

jwt.sub = 'demo@gmail.com';

Also, check that you have https://api.einstein.ai configured as a Remote Site so the callout will be allowed.

  • Yes, I did update my email id, here I just put it for demo purpose only. – Ashutosh Tripathi Jan 8 at 7:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.