- Learning Salesforce Einstein
- Mohith Shrivastava
- 584字
- 2021-07-02 21:43:57
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:
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:

- 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:

- 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:

- 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:

- 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.
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.
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:

- Getting Started with Gulp(Second Edition)
- Node.js+Webpack開發實戰
- Spring 5.0 By Example
- JavaScript高效圖形編程
- GraphQL學習指南
- 程序員考試案例梳理、真題透解與強化訓練
- Bootstrap Essentials
- PHP+MySQL+Dreamweaver動態網站開發實例教程
- Learning Hunk
- 單片機應用與調試項目教程(C語言版)
- Haskell Data Analysis Cookbook
- Unity 2018 Shaders and Effects Cookbook
- C++反匯編與逆向分析技術揭秘(第2版)
- C#程序設計基礎入門教程
- 嵌入式C編程實戰