Wednesday, 29 October 2014

Google Drive Authentication In Salesforce

Now where comes part 2 of authentication. This time it's with Google Drive.

In this blog Authentication with Google is explained so developers can get started.

Step 1 : Create  account on google (hope you already have), then go to developer console to create a project. Now click on project which is newly created then click on APIs and auth > APIs and make Drive API "on".

Step 2 : Now "Create New Client ID " which will look something like this :


Now we will use this info in salesforce to authenticate.

Step 3 : Create this apex class in Salesforce

  1. public class GoogleDriveController  
  2. {  
  3.     //Fetched from URL  
  4.     private String code ;  
  5.     private string key = '134427681112-ld4vp2l1jut3aj2fktip776081nhn8l3.apps.googleusercontent.com' ;  
  6.     private string secret = 'hdHNk4GjNtkR4nNL1SqCfRk_' ;  
  7.     private string redirect_uri = 'https://c.ap1.visual.force.com/apex/GoogleDrivePage' ;  
  8.       
  9.     public GoogleDriveController()  
  10.     {  
  11.         code = ApexPages.currentPage().getParameters().get('code') ;  
  12.         //Get the access token once we have code  
  13.         if(code != '' && code != null)  
  14.         {  
  15.             AccessToken() ;  
  16.         }  
  17.     }  
  18.       
  19.     public PageReference DriveAuth()  
  20.     {  
  21.         //Authenticating  
  22.         PageReference pg = new PageReference(GoogleDriveAuthUri (key , redirect_uri)) ;  
  23.         return pg ;  
  24.     }  
  25.       
  26.     public String GoogleDriveAuthUri(String Clientkey,String redirect_uri)  
  27.     {  
  28.         String key = EncodingUtil.urlEncode(Clientkey,'UTF-8');  
  29.         String uri = EncodingUtil.urlEncode(redirect_uri,'UTF-8');  
  30.         String authuri = '';  
  31.         authuri = 'https://accounts.google.com/o/oauth2/auth?'+  
  32.         'client_id='+key+  
  33.         '&response_type=code'+  
  34.         '&scope=https://www.googleapis.com/auth/drive'+  
  35.         '&redirect_uri='+uri+  
  36.         '&state=security_token%3D138r5719ru3e1%26url%3Dhttps://oa2cb.example.com/myHome&'+  
  37.         '&login_hint=jsmith@example.com&'+  
  38.         'access_type=offline';  
  39.         return authuri;  
  40.     }  
  41.       
  42.       
  43.     public void AccessToken()  
  44.     {  
  45.         //Getting access token from google  
  46.         HttpRequest req = new HttpRequest();  
  47.         req.setMethod('POST');  
  48.         req.setEndpoint('https://accounts.google.com/o/oauth2/token');  
  49.         req.setHeader('content-type''application/x-www-form-urlencoded');  
  50.         String messageBody = 'code='+code+'&client_id='+key+'&client_secret='+secret+'&redirect_uri='+redirect_uri+'&grant_type=authorization_code';  
  51.         req.setHeader('Content-length', String.valueOf(messageBody.length()));  
  52.         req.setBody(messageBody);  
  53.         req.setTimeout(60*1000);  
  54.   
  55.         Http h = new Http();  
  56.         String resp;  
  57.         HttpResponse res = h.send(req);  
  58.         resp = res.getBody();  
  59.           
  60.         System.debug(' You can parse the response to get the access token ::: ' + resp);  
  61.    }  
  62. }  


Step 4 : Create this visualforce page (GoogleDrivePage) in Salesforce

  1. <apex:page controller="GoogleDriveController">  
  2. <apex:form>  
  3.     <apex:pageblock>  
  4.         <apex:commandbutton action="{!DriveAuth}" value="Google Drive Authentication">  
  5.     </apex:commandbutton></apex:pageblock>  
  6. </apex:form>  
  7. </apex:page>  


Step 5 : Replace the code :

You can replace these three variables according to your settings (created in step 2)

  1. private string key = '134427681112-ld4vp2l1jut3aj2fktip776081nhn8l3.apps.googleusercontent.com' ;  
  2. private string secret = 'hdHNk4GjNtkR4nNL1SqCfRk_' ;  
  3. private string redirect_uri = 'https://c.ap1.visual.force.com/apex/GoogleDrivePage' ;  

Step 6 : Don't forget to create remote site setting :


Step 7 : All set to go, now hit the page "https:// ..... /apex/GoogleDrivePage", make sure your debug is on. Once authenticated with google debug will print the access token.


Now you can use this access token for further requests.

Please note, code is not beautified as this is just to explain how you can authenticate google with salesforce. A lot more creativity can be applied here.

No comments:

Post a Comment