Authentication with Einstein Vision and Language APIs
Authentication with Einstein APIs can be tricky, especially if you are using Apex for authentication and you’re not familiar with the OAuth 2.0 JWT(JSON Web Token) bearer flow.
Einstein Language and Vision APIs, both use OAuth 2.0 JWT bearer token flow for authorization.
Primarily, there are two ways you can authenticate yourself and get the access token to successfully authenticate your callouts:
Use the token page to upload your key file and generate a JWT token.
Write some Apex to setup the authentication.
The first approach is rather simple but the downside of it is every time your token is expired you will have to regenerated it from the token page.
The second option, i.e. using Apex, is not as simple as the first one. In order to use Apex for authentication, you should have a good understanding of JSON Web Token.
I have put together a GitHub repo that you can use to set up authentication with Einstein APIs. Source: https://github.com/forcePanda/EinsteinPlatformServices-Repo
I’m using Custom Metadata types(CMDT) to store the information required for authentication.

Here’s how you should setup the info in your CMDT record.
Algorithm: RS256
Issuer: developer.force.com
Subscriber: *your email where you received your RSA key after signup*
Audiences: https://api.einstein.ai/v2/oauth2/token
Token Duration: *Time in seconds*
Endpoint: https://api.einstein.ai/v2/oauth2/token
RSA Key: *Your RSA key*
After setting up CMDT, simply call the getAccessToken method of the EinsteinServicesAPIAuthenticationHandler class to get the token.
String token = EinsteinServicesAPIAuthenticationHandler.getAccessToken('CMDT_Record_DeveloperName');
Hope you find this one useful! More content to follow around Einstein OCR in future posts. See you in the next one! ✌ Thank you for being an awesome reader! Subscribe to this blog for receiving all the latest updates straight to your inbox. 🙂

.png)




Comments