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
Jun LiuJun Liu 

Could not get Access token using Auth.AuthToken.getAccessToken in custom Registration Handler

Hi,

Recently I follow https://help.salesforce.com/HTViewHelpDoc?id=sso_provider_facebook.htm&language=en_US to set up a facebook auth.provider, and put the callback URL in my facebook app. and the provider is using a custom handler as below.

after the user login using facebook credentials, and in the handler I always get null value for access token using Auth.AuthToken.getAccessToken('my auth.provider id in org', 'Facebook');

I even tried using Salesforce facebook app instead of mine, but still no luck. Can some one tell me if there is something specific i need to pay attention to to get access token? 

 

public class FacebookHandler implements Auth.RegistrationHandler{
    
    public User createUser(Id portalId, Auth.UserData data){
        
        
        User[] users = [SELECT Id FROM User WHERE Email = :data.email];

        if (users.size() == 0) {

          String accessToken = Auth.AuthToken.getAccessToken('0SO28000000XXXXXX', 'Facebook');
        
           System.debug('access token: ' + accessToken);
   
           return null;

        }
        else if (users.size() == 1)
        {
          
           User u = users[0];
            
           System.debug('User Id: ' + u.Id);

           String accessToken = Auth.AuthToken.getAccessToken('0SO28000000XXXXXX', 'Facebook');
        
           System.debug('access token: ' + accessToken);

           //return the user to login as
           return u;
        }

Many thanks in advance,

Jun Liu

        return null;    
        
    }
Jun LiuJun Liu
Here is a workaround:

Edit userinfo endpoint in your facabook Auth.provider to retrieve specific user attributes. Something like this:

https://graph.facebook.com/me?fields=picture,name,id,email

And modify the reg. handler to extract the attributes from the attributeMap and create/update the user as follow:

for(string key : data.attributeMap.keySet())
{
          data.attributeMap.get(key));
          if(key == 'name')
             u.name = data.attributeMap.get('name');
  
           ...
}