Q:

Set dynamic keys/secret for python-social-auth, before every request used for social auth

I am using inside django:
social-auth-app-django and python-social-auth[django].
In settings.py I have declared the following variables:
SOCIAL_AUTH_FACEBOOK_KEY = ''
SOCIAL_AUTH_FACEBOOK_SECRET = ''
SOCIAL_AUTH_FACEBOOK_SCOPE = ['email']
SOCIAL_AUTH_FACEBOOK_PROFILE_EXTRA_PARAMS = {'fields': 'name, email'}

Everything works ok. But I want to set the SOCIAL_AUTH_FACEBOOK_KEY and the SOCIAL_AUTH_FACEBOOK_SECRET dynamic before every request for social auth, in an middleware or decorator. Why I want this is, because my  django app is serving data for multiple sites, and every site has is own keys, and his own domain.
I can't find a way to overwrite the variables in settings, or to send the keys to social_auth.

A:

To accomplish this you have two options:

Define your own strategy that overrides get_setting() and returns the dynamic values as desired (the current request is available at self.request).
Then point to it with SOCIAL_AUTH_STRATEGY setting.

Extend Facebook backend and override get_key_and_secret(), and return the custom values. Current request is at self.strategy.request.
Then point to it in AUTHENTICATION_BACKENDS setting (remove the built-int one).

Edit: typography

