Learning Salesforce Einstein
上QQ阅读APP看书,第一时间看更新

Setting authentication for calling API from SFDC

To simplify the authentication, we will use a named credential in Salesforce and auth settings to keep it configurable. Note that this is not scalable, but it's a quick way to integrate Salesforce with Google for the purposes of demonstration only. You can use a service account or set up a full OAuth 2.0 if you are considering a scalable approach.

The steps to authorize Salesforce to access the Google Prediction API are as follows:

  1. Generate a client ID and client secret via the Google Auth screen. To generate this, navigate to the console URL (https://console.developers.google.com/apis/credentials). Choose the subtab Oauth consent screen and fill in the Product name shown to users as Salesforce and save the OAuth consent screen form.

    The following screenshot shows the OAuth consent screen and details one has to fill in to generate the Consumer Secret and Consumer Key:

Once an OAuth consent screen is created, create a web application and note the Consumer Secret and Consumer Key. This will be used in the Salesforce Auth. Provider screen:

  1. Use Auth in Salesforce to set up authorization, as shown in the following screenshot. The path for it is SETUP | Security Controls | Auth. Providers | New 

Select the Provider Type as Google. The following screenshot shows the form. Note that we use the Consumer Key  and Consumer Secret from step 1 as input to the Auth. Provider form:

The following screenshot shows the saved Auth. Provider record. Note the Callback URL as it will be fed back to the Google OAuth consent screen:

  1. Note down the Callback URL from the Auth. Provider screen. The Callback URL needs to be added back to the Google Auth screen. The following screenshot shows the redirect URL configuration in the Google OAuth screen:
  1. Use Named Credentials so that we avoid passing the authorization token. Carefully note the scope defined in the SETUP. The path for it in Classic is: Setup | Security Controls | Named Credentials:
  1. Testing out the results is the final step to test our model and use it with a real-time record creation screen in Salesforce. Create an opportunity record and indicate the type and the amount of opportunity. Once a Salesforce record is inserted, a trigger fires, calling the Prediction API with a new Dataset, and the result is stored back in the custom probability field.
Note that if you get an authentication error in the logs, go to Named Credentials to authorize the app again.

The API that the Apex trigger code hits is as follows:

https://www.googleapis.com/prediction/v1.6/projects/learned-maker-155103/trainedmodels/opportunity-predictor/predict

To test, create the Salesforce new record and add fields such as type and amount and monitor the custom probability field on the Salesforce opportunity record.

Note that we are using the custom field for probability. Also note that we have switched our Salesforce organization to use new Lightning Experience. Salesforce offers Classic and Lightning Experience. Classic is an older user interface, while Lightning Experience is a modern interface. To enable Lightning Experience in your developer org under the Setup menu you will find a menu item named Switch To Lightning Experience. You can always toggle between Lightning and Classic in your developer org.

To switch to the Salesforce Lightning experience, consider the following screenshot:

The following screenshot shows the results obtained in Lightning Experience, and, clearly, the Predicted_Probability field is populated: