Skip to main content

Oauth Google With Salesforce.com using apex

In this short blogpost lets explore oauth along with a classic example of connecting salesforce and google and fetching auth token from the google using on demand salesforce emerging platform language apex 

There are already excellent client libraries to help you started with oauth 2.0 

The above link is really nice one to explore more code in your faviourate language .Now coming to salesforce we have excellent blog post that teaches oauth in depth and i call it as sfdc oauth bible 


The above URL teaches lot of stuff in a very lucid and very unique style .Thanks to the author .

In this blog post we will connect sfdc and Google and obtain oauth token from the google .This oauth token can be used to fetch personalized  data from Google using the Google API's(I will love to do one more blog post where an example of data fetched from Google will be shown)


Using OAuth 2.0 to Access Google APIs


The API's provided by the Google can be accessed easily once we authorize the app created in google with sfdc.

There are various different types of apps that we can create which includes web server,client app,native,installed apps .


Reading the above document is really helpful as very steps are highlighted that we will do before moving into sfdc end 

1)Register your application

To register an application google provides a console name cloud console 


Here in the above URL we will need to do very important steps .One is to create a project and then register a web app .
Creation of project is just a button click on new project and providing a name and saving it.

To register an app go to Project>API's>Register APPs

Here create a new web app .You can register any of the type of app and note the mechanism for each is different and discussed in the Guide
For this demo we will create a web server application.

2)Authenticating the User

User authentication is our next step.Once we register our web app of sfdc in google we obtain the clientid and client secret which are used to fetch the oauth token from google .

Please note a very important parameter is necessary that's redirect url for the web app .In force.com we will create a small visualforce page and the url this visualforce page has ,to be provided as the redirect url.Also its important to fill the web orgin in cloud console  so as per your salesforce request url please provide as appropriate(example in my case its developer org and hence its https://ap1.salesforce.com).

If my visualforce page is named as GoogleAuth then my redirect url would be https://ap1.salesforce.com/apex/GoogleAuth

We are ready to see some code that is written in visualforce and apex language to fetch the oauth token from the google completing the oauth dance



Let me explain the code above to my best of ability of explaining

1)The controller consists of two major methods

Connect method()
Showtoken()

Connect method redirects to url to authorize the app .First step is to get "code " url parameter from the google .This will be got only once we authorize the app .Typically web server oauth flow you will see in video

Show token method is called once we have the "code" parameter.

string codeparam=apexpages.currentpage().getparameters().get('code');

Using the code parameter we will make a request body and make a http request from sfdc.



The body formation is the key and all parameters are clearly explained in the google documet https://developers.google.com/accounts/docs/OAuth2

We are printing the oauth token obtained and using this oauth token we can fetch the personalised user data like contacts,calendar events ,access Map API's,etc




Here is the video link

http://screencast-o-matic.com/watch/cI6Y2hV7dS

There is force.com toolkit for the google found in the link below which is very easy to install and use

http://wiki.developerforce.com/page/Google_Data_API_Toolkit

But this blogpost my intention was to demonstrate oauth and its concepts for force.com lovers .Hope you enjoyed this blog!Happy coding !

There is lot of to do's after fetching oauth token but this is the starting point to get going .Infact an  oauth token is short lived and hence a refresh a token is necessary to fetch and left as an exercise to reader.





Popular posts from this blog

TLS 1.0 has been disabled For Sandbox - Salesforce

Salesforce has finally disabled TLS 1.0 in sandboxes .This is in preparation for disablement later in 2017 for PRODUCTION instances .

Now this would have not impacted your integrations in PRODUCTION org but if your integration is broken in your sandbox ,this means you have only few months to sort this and fix before this affects your integration for PROD live users .
Before we deep drive on how to possibly fix this and work with your external systems to figure solution ,lets first understand what is TLS and why did SFDC moved to 1.x and had to disable TLS 1.0
TLS 1.0 Explained























TLS 1.1 Improvements 
Added protection against cipher-block chaining (CBC) attacks. Support for IANA registration of parameters.

Clearly TLS 1.1 is more secure(Compared to 1.0) and protects salesforce resources against CBC attacks .
Identify if this change broke anything .The things that can be affected in your instances are as below Web requests to Salesforce URLs that require authenticationWeb requests to the login pag…

Opening Modal Using Lightning Component Framework of SFDC

One of my friend from India threw a challenge .The challenge was to open a modal by using latest and greatest lightning components framework and modals design from SLDS .For the love of community I thought of sharing the entire code base that I did .

So here we start ..

Business Use Case - Need a handy SalesLeader board component that can be used to display the Sales revenue generated by each sales rep for current year in the order of decreasing total revenue .On click of the tile ,we will show detail opportunity list aggregating the revenue .

The component can be dragged in lightning design experience or in App builder lightning Page .

Video Demonstration-



SalesLeaderBoard from Mohith Kumar Shrivastava on Vimeo.








Frameworks Used -
Lightning Design Systems (SLDS) for CSSLightning Component Framework for client side logicApex aura enabled class for backend logicApproach
The component hierarchy is very important to imagine or mindmap before we dig deeper- SalesLeaderMain
    -SalesLeaderBoard
    …

Customizing Napili Template Of SFDC For Communities - Community Cloud

Recently I have been busy working with salesforce community cloud platform and I am impressed by the new added features of Spring 16 .

You can watch the Release Readiness webinar for more details on what are added features for community cloud 

Lets talk about something which most of client (Either SI or ISV ) would love to have with the Napili template .The first question that clients ask with Napili is its flexibility to customize and tailor to their specific needs .

The very straight forward questions that people familiar with Napili template ask are as follows 

Can I change the CSS and look and feel ?  

Well yes and No ....The new branding editor you can sparingly use CSS and change look and   feel   and community editor can also help you to some extent to change labels and the color and you can really get closer to what you want .Just note that efforts will be high if we want to change the entire template design.

Will i be able to populate data from custom objects and standard objects a…