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
Chirag_JoshiChirag_Joshi 

How to get access to google basic profile information with access_token

I have a force.com site where I am using google authentication
The authentication is working fine. I am getting the acess_tokne in json

After this i want to get the basic profile information about the user from google like his name, email, etc

How to use access_token to get this and what url to send request at

Here is what i am using in force.com site

Site URL:
http://bodaciousithub01-developer-edition.ap5.force.com/GoogleAuth

VF Page:
<apex:page controller="GoogleAuthController" showheader="false" sidebar="false">
    <apex:form >
        <apex:pageblock title="Google Authentication">
            <apex:commandButton value="Connect To Google" action="{!connect}"/>
            <apex:commandButton value="Fetch Auth Token" action="{!showtoken}"/>
            <br/><br/>
            {!bodyprint}
        </apex:pageblock>
    </apex:form>
</apex:page>

Apex Controller:
public class GoogleAuthController {
    
    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='ZpH3EjBufPYchj1t97yerzaA'; //Fill as per your registered app settings in google console
    public static final string CLIENT_ID='472650385442-r0gt7rpvg4f54ao65hiq5m2so4afrs3p.apps.googleusercontent.com'; //Fill as per your registered app settings in google console
    public static final string REDIRECT_URL='https://bodaciousithub01-developer-edition.ap5.force.com/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;
        System.debug(x);
        pagereference p=new pagereference(x);
        return p;
        
    }
    
    public pagereference showtoken(){
        
        String codeparam=apexpages.currentpage().getparameters().get('code');
        
        System.debug('codeparam: ' + codeparam);
        
        // 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;
        
    }
}

 
Best Answer chosen by Chirag_Joshi
Chirag_JoshiChirag_Joshi
Alright, I got the endpoint URL to get the basic profile information of user from Google:
Here it is:

https://www.googleapis.com/oauth2/v1/userinfo?access_token='+access_token

I gave my access_token here and i got the response