Hi #Ohana,
Welcome back, it has been a while. In this blog post, we will talk about what Google OCR & Vision APIs are and how to get access token using the Salesforce VF page and apex class.
Introduction to Google Cloud Vision API
GC ( google cloud ) provides the free API which you can use for image labeling, face, logo, and landmark detection, optical character recognition (OCR), and detection of explicit content, into applications.
You can read the complete API documentation from Here
Authentication Overview
All the features of Google Cloud required a valid Access Token to make the request and then return the proper response. To get the access token google uses OAuth 2.O which is a 2 step process.
You can read the process of authentication with Google API from the official document of google cloud. Here is the link for the same.
Step 1 – Create custom metadata types – We will create 2 custom metadata types for authentication purpose.
1.1 – Google Config – The custom metadata types to store the configuration like authentication URL, token URL, client id, client secret &, etc. Below the image of the CM with all the fields
Below are the values for all the fields –
Field | value |
access type | offline |
authorize uri | https://accounts.google.com/o/oauth2/v2/auth |
grant_type | authorization_code |
response_type | code |
prompt | consent |
token URL | https://www.googleapis.com/oauth2/v4/token |
Content-Type | application/x-www-form-urlencoded |
model | builtin/stable |
client secret | YOUR_CLIENT_SECRET |
client id | YOUR_CLIENT_ID |
scope | space separate scope values for example – https://www.googleapis.com/auth/cloud-platform https://www.googleapis.com/auth/cloud-vision Here is the list of all scope – link |
redirect uri | redirect URI is the return URL after authentication. you only need to put the VF page like apex/GoogleAuth and you need to put the complete URL in google app like – https://back-to-basic-dev-ed–c.visualforce.com/apex/GoogleAUTH |
Note: – You can get client Id, Client Secret from this page
1.2 – Google Token – This metadata is used to store the Google Access Token and Refresh Token. You only need to create the metadata and the record will be created by code after authentication.
Step 2 – Create Apex Class to create/update custom metadata
You can refer this link for the same
Step 3 – Create Apex Class “GoogleTokenUtility” – the class behind getting access token from Google and then creating the Custom Metadata Record.
Methods in Apex Class
Method Name | Description |
getInstance(String filter) | A Reusable Method is used for getting the access token details from metadata Google Token |
getConfig(String filter) | A Reusable method to get the google configuration settings from the metadata Google Config |
buildQueryAllString | This is the reusable method for the SOQL query with all the fields. |
getAuthCode | The method used to get the auth code from google and then this auth code will be used to get access token |
getAccessToken | The method used to get the access token for authorized accounts. |
doRefreshToken | The method used for refreshing the token if expired |
checkIfTokenIsValid | Checks if the token is valid or not. |
prepareRequest | Used to prepare the HttpRequest |
Step 4 – Create a VF Page “GoogleAUTH“
<apex:page controller="GoogleTokenUtility" lightningStylesheets="true">
<apex:form >
<apex:pageBlock title="Google Authentication">
<apex:pageblockButtons >
<apex:commandButton value="Authorize" action="{!getAuthCode}" />
</apex:pageblockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
Step 5 – Create a new VF Page “CompleteGoogleAuth“
<apex:page controller="GoogleTokenUtility" action="{!getAccessToken}" lightningStylesheets="true">
<apex:form >
<apex:pageBlock title="Google Authentication">
<apex:pagemessages ></apex:pagemessages>
</apex:pageBlock>
</apex:form>
</apex:page>
Step 6 – Test the flow
Open the “GoogleAUTH” VF page and click on Preview. Now, Click on the Authorize button.
Thanks for reading 🙂
#Salesforce #Integration