def resolve_authorization_url(oauth_library: OAuth2Session, provider_instance, **kwargs): additional_params = {} if "path" in kwargs: provider_instance["authorization_url_path"] = kwargs["path"] if "prompt" in kwargs: if provider_instance["client"] == "zoho": provider_instance["prompt"] = kwargs["prompt"] if "approval_prompt" in kwargs: if provider_instance["client"] == "gmail": provider_instance["approval_prompt"] = kwargs["approval_prompt"] if "access_type" in kwargs: provider_instance["access_type"] = kwargs["access_type"] if provider_instance.get("approval_prompt"): additional_params["approval_prompt"] = provider_instance[ "approval_prompt"] if provider_instance.get("prompt"): additional_params["prompt"] = provider_instance["prompt"] if provider_instance.get("access_type"): additional_params["access_type"] = provider_instance["access_type"] authorization_url, state = oauth_library.authorization_url( f"{provider_instance['base_url']}{provider_instance['authorization_url_path']}", **additional_params, ) return authorization_url
def _authenticate( sesh: OAuth2Session, client_secret: str, api_url: str, redirect_uri: str, state: Optional[str], token_path: str, authorize_path: str, authorize_params: QueryParams, ) -> str: """Perform OAuth2 Flow and get a new token Note: Implicitly updates the OAuth2Session """ authorization_url, state = sesh.authorization_url( urljoin(api_url, authorize_path), state=state, kwargs=authorize_params, ) print( 'Opening browser to visit:\n\n' f'{Fore.BLUE}{Style.BRIGHT}{authorization_url}{Style.RESET_ALL}\n\n' 'Sign in and go through the DSA, then copy the url at the end.\n' ) sleep(DRAMATIC_PAUSE) try: webbrowser.open(authorization_url) except NotADirectoryError: # If permissions error print_exception( title='I couldn\'t open your browser', message=( 'Go ahead and copy/paste the url into your browser\n' 'Then sign in and go through the DSA.' ), ) sleep(DRAMATIC_PAUSE) authorization_response = _get_authorization_response_url() print() environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1' # allow non-http redirect_uri token = sesh.fetch_token( urljoin(api_url, token_path), authorization_response=authorization_response, client_secret=client_secret, include_client_id=True, ) return str(token['access_token'])
def authorization_url(self): return OAuth2Session.authorization_url(self, _AUTHORIZATION_BASE_URI)