Package: python-socialite
python-socialiteThe easy way to retrieve OAuth 2.0 Tokens from any providerSimple and convenient way for fetching OAuth 2.0 tokens from any provider. Out of the box support for Facebook, Google, GitHub, Microsoft, Bitbucket and more coming... Inspired byLaravel SocialiteThis package handles all required boilerplate code for implementing OAuth based social authentication in Python and allows you to integrate OAuth login easily in any app built with Python irrespective of your python framework or platform.FeaturesSupports multiple common providersSupports any oAuth 2 compliant providers (You can provide a custom driver)Straighforward unopinionated authenticationCan be implemented in any python frameworkOut of the box OAuth 2.0 Social Login ProvidersLogin with FacebookLogin with GoogleLogin with MicrofostLogin with GithubLogin with BitbucketAny custom OAuth 2.0 driverUsageInstallationpipinstallpython-socialiteGenerate redirect urifrompython_socialiteimportOAuthProviderconfig={"google":{"client_id":"","client_secret":"","redirect_url":""},"microsoft":{"client_id":"","client_secret":"","redirect_url":"","tenant":"common","scopes":["email","user"],}}# Authorize Googleprovider=OAuthProvider("google",config)redirect_url=provider.get_auth_url()# Authorize Microsoftprovider=OAuthProvider("microsoft",config)redirect_url=provider.get_auth_url()# redirect user to the redirect_url using your frameworks supported redirectRetrieving Access Token and Usercode=""# OAuth provider will redirect back to your redirect_url with a code in the urlprovider=OAuthProvider("google",config)token=provider.get_token(code)user=provider.get_user(token["access_token"])This package does not provide opinion on how you use the returned token or user profile. Add that to your application's business logic. Examples include hooking up to your authentication logic, fetching data associated with the returned access token e.t.cToken TemplateNB:Token attributes might vary between providers. Here's a sample returned by Google oAuth{"access_token":"ya29.***","expires_in":3599,"scope":"https://www.googleapis.com/auth/userinfo.profile openid","token_type":"Bearer","id_token":"***jwt***"}User Templateuser={"id":"","name":"","email":"","avatar":"","raw":"","provider":""}Therawattribute contains all user data as returned by the oAuth provider. Fields in this attribute can be different across different oAuth providersRequesting ScopesBy default the following scopes are requestedopenid,email,profileYou can override requested scopes by adding them to the provider config or usingset_scopesmethodprovider=OAuthProvider("google",config)auth_url=provider.set_scopes(["openid","email","profile"]).get_auth_url()NB:If no scopes are set in the config or in code the default scopes will be usedConfig OptionsThe config must be a dict containing keys of any of the supported providers# each provider key must have client_id, client_secret and redirect_url. It's advised to ensure your client_secret is properly securedconfig={"google":{"client_id":"","client_secret":"","redirect_url":"","scopes":[]# optional},"facebook":{},"github":{},"microsoft":{},}Points to noteFacebook now requires an access token to load user profile picture. If token is not supplied a placeholder will be returned.Github does not always return users emails depending on user's privacy settings. In that case an@users.noreply.github.comemail will be returned.Microsoft does not return a picture in the users profile. You can use the returned access token to fetch one from Microsoft Open GraphSupport for adding a custom driver to any OAuth provider of your choice is planned. If you urgently need this open an issueThe Bitbucket driver will make two calls in order to fetch the user's email addressesThe Bitbucket driver does not fetch the user's profile picBuilding custom OAuth 2.0 login driver in pythonYou can build your own custom OAuth 2.0 driver for any of your preferred services by extendingAbstractDriverTIP:copy code from one of the other drivers on the src directory and modify where necessaryfrompython_socialite.drivers.abstract_driverimportAbstractDriverclassAcmeProvider(AbstractDriver):def__init__(self,config):"""Initialize Acme provider."""super().__init__(config)self.scopes=config.get("scopes",["account","email"])@staticmethoddefprovider_name():return"acme"# config keyHave a look at the exiting drivers for methods that you must implmentCalling your custom driverCreate your configconfig={"acme":{"client_id":"xxxxxxxxxxxxxxxx","client_secret":"xxxxxxxxxxxx","redirect_url":"http://localhost"},}provider=OAuthProvider(AcmeProvider,config)NB:Call all other methods as ussual