function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
reatlimecoreatlimeco 

Google Doc integration using Apex class to create authenication

Is there a way to use the Google Doc authentication that a Salesforce org already has to store/retrieve documents from an organization's exisitng Google Doc account?

 

If not, the example of authenication shows a visualforce page to capture and generate the authenication token.   Is it possible to do the authenication instead in an apex class where the account name and password would come from the "Configuration" object instead.  If so, can someone post an example of how this is done.

Ron HessRon Hess

I don't think salesforce stores google docs auth, rather it's a sign on process.  Do you have a google my domain ?

 

the toolkit has an example of using a visualforce page to capture and store an authSub token, but you may want to re-write that using oauth.   the only examples i have are out on code share.

 

 

Ron HessRon Hess

Here is the auth sub page with no google domain 

 

<apex:page controller="AuthSubController">
	<apex:form >
		<!--  if we are missing a token, ask for one -->

		<apex:outputPanel rendered="{!requestToken}">
	<script> 
	var scope = 'http://www.google.com/calendar/feeds/'; // change this to match the API you will use
	 
	var forceReDirect = '/_ui/core/google/GoogleAuthSubCallback?url=' ;
    var googleRedir = 'https://www.google.com/accounts/AuthSubRequest?next=';
 					
	// destination (relateive path) that will capture the token and store it for your app	
	// you can pass your own query string parms
	var myApp = encodeURIComponent( 
				window.location.pathname + "?id={!$CurrentPage.parameters.id}" ) ;
	
    var nextUrl = window.location.protocol + '//' + window.location.host + 
    			forceReDirect + myApp ;  
      
    var tokenRequestUrl  = googleRedir + 
		        encodeURIComponent( nextUrl ) + 
		        "&session=1&secure=0&scope="+ scope;

	// go there now
    window.location.href = tokenRequestUrl;
</script>
		</apex:outputPanel>
		<apex:outputPanel rendered="{!$CurrentPage.parameters.token != null}"> 
The one time use token is :<b>{!$CurrentPage.parameters.token}</b>
			<br />
Record to update is {!$CurrentPage.parameters.id}
<apex:commandbutton action="{!exchangeRequestToken}"
				oncomplete="window.close();" value="Request Session token" />
		</apex:outputPanel>
	</apex:form>
</apex:page>

 

 

Ron HessRon Hess

here is the auth sub page with a google domain

 

 

<apex:page controller="AuthSubController">
    
	<apex:form >
		<!--  if we are missing a token, ask for one -->

		<apex:outputPanel rendered="{!requestToken}">
	<script> 
	var scope = 'http://www.google.com/calendar/feeds/'; // change this to match the API you will use
	 
	var forceReDirect = '/_ui/core/google/GoogleAuthSubCallback?url=' ;
    var googleRedir = 'https://www.google.com/accounts/AuthSubRequest?next=';
 					
	// destination (relateive path) that will capture the token and store it for your app	
	// you can pass your own query string parms
	var myApp = encodeURIComponent( 
				window.location.pathname + "?id={!$CurrentPage.parameters.id}" ) ;
	
    var nextUrl = window.location.protocol + '//' + window.location.host + 
    			forceReDirect + myApp ;  
      
    var tokenRequestUrl  = googleRedir + 
		        encodeURIComponent( nextUrl ) + 
		        "&hd=yourgoogledomain.com&session=1&secure=0&scope="+ scope;

	// go there now
    window.location.href = tokenRequestUrl;
</script>
		</apex:outputPanel>
	
		<apex:outputPanel rendered="{!$CurrentPage.parameters.token == null && $CurrentPage.parameters.id == null}" >
		Missing id parameter on URL
		</apex:outputPanel>
		<apex:outputPanel rendered="{!$CurrentPage.parameters.token != null}"> 
The one time use token is :<b>{!$CurrentPage.parameters.token}</b>
			<br />
Record to update is {!$CurrentPage.parameters.id}
<apex:commandbutton action="{!exchangeRequestToken}"
				oncomplete="window.close();" value="Request Session token" />
		</apex:outputPanel>
	</apex:form>
</apex:page>