def authenticate(user_email, user_password): try: # These values should match the ones supplied when registering your application. mendeley = Mendeley(CLIENT_ID, redirect_uri=REDIRECT_URI) auth = mendeley.start_implicit_grant_flow() # The user needs to visit this URL, and log in to Mendeley2. login_url = auth.get_login_url() import requests res = requests.post(login_url, allow_redirects=False, data={ 'username': user_email, 'password': user_password }) auth_response = res.headers['Location'] # After logging in, the user will be redirected to a URL, auth_response. global session session = auth.authenticate(auth_response) global ACCESS_TOKEN ACCESS_TOKEN = session.token['access_token'] except Exception as exc: raise exc
def establish_mendeley_session(config: dict) -> MendeleySession: """ Connect to Mendeley's RESTful server """ if len(config) == 0: raise ValueError( 'Configuration Error: Check the configuration yaml file!') try: mendeley = Mendeley(client_id=config.get('clientId'), client_secret=config.get('clientSecret'), redirect_uri=config.get('redirectURI')) authorization = mendeley.start_implicit_grant_flow() if authorization is not None: loginURL = authorization.get_login_url() if loginURL != "" or loginURL is not None: authHeader = { "username": config.get("username"), "password": config.get("password") } request = requests.post(loginURL, allow_redirects=False, data=authHeader) if request.status_code >= 400: raise ConnectionError( 'Error: Cannot connect to Mendeley Database: Status Code: {}' .format(request.status_code)) session = authorization.authenticate( request.headers['Location']) return session else: raise ValueError('Error: Cannot Retrieve the Login URL') else: raise ConnectionAbortedError('Error: Unauthorized User!') except Exception as exc: print('Error: Connecting to the Mendeley Database')
def test_should_get_authenticated_session(): mendeley = Mendeley('id', 'secret', 'https://example.com', state_generator=DummyStateGenerator()) auth = mendeley.start_implicit_grant_flow() session = auth.authenticate('https://example.com#state=state1234&access_token=token5678&token_type=bearer') assert session.token['access_token'] == 'token5678' assert session.host == 'https://api.mendeley.com'
def test_should_get_implicit_grant_login_url(): mendeley = Mendeley('id', 'secret', 'https://example.com', state_generator=DummyStateGenerator()) auth = mendeley.start_implicit_grant_flow() assert auth.get_login_url() == 'https://api.mendeley.com/oauth/authorize?' \ 'response_type=token&' \ 'client_id=id&' \ 'redirect_uri=https%3A%2F%2Fexample.com&' \ 'scope=all&' \ 'state=state1234'