Last active
December 26, 2015 01:09
-
-
Save msrivastav13/7069586 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<apex:page controller="googleauthclass"> | |
<apex:form > | |
<apex:commandButton value="Connect To Google" action="{!connect}"/> | |
<apex:commandButton value="Fetch Auth Token" action="{!showtoken}"/> | |
</apex:form> | |
{!bodyprint} | |
</apex:page> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class googleauthclass{ | |
public string authtoken{get;set;} | |
public string refereshtoken{get;set;} | |
public string bodyprint{get;set;} | |
//Settings needed on the google cloud console.One can store this securely in custom settings or an object. | |
public static final string CLIENT_SECRET='';//Fill as per your registered app settings in google console | |
public static final string CLIENT_ID='';//Fill as per your registered app settings in google console | |
public static final string REDIRECT_URL='https://ap1.salesforce.com/apex/GoogleAuth'; | |
public static final string OAUTH_TOKEN_URL='https://accounts.google.com/o/oauth2/token'; | |
public static final string OAUTH_CODE_END_POINT_URL='https://accounts.google.com/o/oauth2/auth'; | |
public static final string GRANT_TYPE='grant_type=authorization_code'; | |
//Scope URL as per oauth 2.0 guide of the google | |
public static final string SCOPE='https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile'; | |
public static final string STATE='/profile'; | |
//Approval Prompt Constant | |
public static final string APPROVAL_PROMPT='force'; | |
public pagereference connect(){ | |
String x=OAUTH_CODE_END_POINT_URL+'?scope='+EncodingUtil.urlEncode(SCOPE,'UTF-8')+'&state='+EncodingUtil.urlEncode(STATE,'UTF-8')+'&redirect_uri='+EncodingUtil.urlEncode(REDIRECT_URL,'UTF-8')+'&response_type=code&client_id='+CLIENT_ID+'&approval_prompt='+APPROVAL_PROMPT; | |
pagereference p=new pagereference(x); | |
return p; | |
} | |
public pagereference showtoken(){ | |
string codeparam=apexpages.currentpage().getparameters().get('code'); | |
// Instantiate a new http object | |
Http h = new Http(); | |
String body='code='+codeparam+'&client_id='+CLIENT_ID+'&client_secret='+CLIENT_SECRET+'&redirect_uri='+REDIRECT_URL+'&'+GRANT_TYPE; | |
// Instantiate a new HTTP request, specify the method (GET) as well as the endpoint | |
HttpRequest req = new HttpRequest(); | |
req.setEndpoint(OAUTH_TOKEN_URL); | |
req.setHeader('Content-Type','application/x-www-form-urlencoded'); | |
req.setMethod('POST'); | |
req.setBody(body); | |
system.debug('REQUEST BODY'+body); | |
// Send the request, and return a response | |
HttpResponse res = h.send(req); | |
system.debug('body'+res.getbody()); | |
bodyprint=res.getbody(); | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment