Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get long-lived Facebook token on connect or login #413

Open
dadoeyad opened this issue Feb 28, 2018 · 0 comments
Open

Get long-lived Facebook token on connect or login #413

dadoeyad opened this issue Feb 28, 2018 · 0 comments

Comments

@dadoeyad
Copy link

This is an improvement request/suggestion more than an issue.
django-allauth allows exchanging the short-lived Facebook access token for a long-lived one by setting "EXCHANGE_TOKEN": True in the Facebook provider settings.

Code in django-allauth:

https://github.com/pennersr/django-allauth/blob/25fe632acf12571ae2ac9e692e8890019d5a6e7b/allauth/socialaccount/providers/facebook/views.py#L97-L108

I needed django-rest-auth to do the same and did it by overriding the get_social_login method in my FacebookSocialConnectSerializer

def get_social_login(self, *args, **kwargs):
        social_login = super(FacebookSocialConnectSerializer, self).get_social_login(*args, **kwargs)
        # Get long-lived token
        request = self._get_request()

        provider = providers.registry.by_id(
            FacebookProvider.id, request
        )
        app = provider.get_app(request)

        resp = requests.get(
            GRAPH_API_URL + '/oauth/access_token',
            params={'grant_type': 'fb_exchange_token',
                    'client_id': app.client_id,
                    'client_secret': app.secret,
                    'fb_exchange_token': social_login.token}).json()
        access_token = resp['access_token']
        expires_at = None
        expires_in = resp.get('expires_in')
        if expires_in:
            expires_at = timezone.now() + timedelta(
                seconds=int(expires_in))

        token = SocialToken(app=app,
                            token=access_token,
                            expires_at=expires_at)
        social_login.token = token
        return social_login

If you think django-rest-auth would benefit from this, I'll be happy to open a pull request :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant