Search code examples
pythondjangopython-social-auth

How to get backend instance from python-social-auth


I am currently using python-social-auth and added a backend MyOAuth which BaseOAuth1, inside the BaseOAuth1 class there is a handy oauth_request instance method which I would like to make use of outside of the class.

https://github.com/omab/python-social-auth/blob/master/social/backends/oauth.py

I tried instantiate the class directly but looks like I am missing some context.

Is there a way reference that MyOAuth backend instance? I am expecting something like

request.user.social_auth.get(provider='MyOAuth').backend.oauth_request(...)


Solution

  • Sweet, posted on the github project and got an answer.

    https://github.com/omab/python-social-auth/issues/114

    For now:

    from social.apps.django_app.utils import load_strategy
    
    strategy = load_strategy(backend='MyOAuth')
    social = request.user.social_auth.get(provider='MyOAuth')
    backend = backend(strategy=strategy)
    backend = social.get_backend(strategy)
    backend.oauth_request(...)
    

    The author will release a helper to do this easily.

    social = request.user.social_auth.get(provider='MyOAuth')
    backend = social.get_backend_instance()