2

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

2

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

           ...
}
| improve this answer | |
  • Thank you @jun-liu - this workaround works good. I believe salesforce should update documentation according the changes in Graph API v2.4 – Yury Bondarau Sep 16 '15 at 7:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.