3

Recently, I followed 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. The provider is using a custom handler shown below.

After the user logs in using facebook credentials, I always get a null value for access token using the following in the handler:

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;
        }



        return null;    

    }

Many thanks in advance,

Jun Liu

0

1 Answer 1

3

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');

           ...
}
1
  • Thank you @jun-liu - this workaround works good. I believe salesforce should update documentation according the changes in Graph API v2.4 Commented Sep 16, 2015 at 7:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .