def mendeley(): ## load configs from file conf = open('config.yml', 'r+w') config = yaml.load( conf ) mendeley = Mendeley(config['clientId'], config['clientSecret'], config['path'] ) ## interactive OAuth flow if 'token' not in config: auth = mendeley.start_authorization_code_flow() state = auth.state auth = mendeley.start_authorization_code_flow( state = state ) print auth.get_login_url() ## auth = mendeley.start_implicit_grant_flow() # After logging in, the user will be redirected to a URL, auth_response. session = auth.authenticate( raw_input() ) print session.token config['token'] = session.token ## clean file conf.write('') yaml.dump( config, conf, default_flow_style=False ) print 'New infos stored' ## update access tokens ## use new access token session = MendeleySession( mendeley, config['token'] ) return session
def mendeley(): ## load configs from file conf = open('config.yml', 'r+w') config = yaml.load(conf) mendeley = Mendeley(config['clientId'], config['clientSecret'], config['path']) ## interactive OAuth flow if 'token' not in config: auth = mendeley.start_authorization_code_flow() state = auth.state auth = mendeley.start_authorization_code_flow(state=state) print auth.get_login_url() ## auth = mendeley.start_implicit_grant_flow() # After logging in, the user will be redirected to a URL, auth_response. session = auth.authenticate(raw_input()) print session.token config['token'] = session.token ## clean file conf.write('') yaml.dump(config, conf, default_flow_style=False) print 'New infos stored' ## update access tokens ## use new access token session = MendeleySession(mendeley, config['token']) return session
class MendeleyStructure(): def authorizeMendeley(self): self.mendeley_obj = Mendeley(CLIENT_ID, client_secret=CLIENT_SECRET, redirect_uri=REDIRECT_URI) self.auth = self.mendeley_obj.start_authorization_code_flow() login_url = self.auth.get_login_url() return login_url def getMendeleyObject(self): return self.mendeley_obj def getAuthObject(self): return self.auth def setSessionState(self,state): self.state = state def getSessionState(self): return self.state def setToken(self,token): self.token = token def getToken(self): return self.token
def get_session(self, user): # Get social instance social = user.social_auth.get(provider=self.name) client_id, client_secret = self.get_key_and_secret() tokens = { 'access_token': social.access_token, 'refresh_token': social.extra_data['refresh_token'] } # start authorization flow mendeley = Mendeley(client_id, client_secret=client_secret, redirect_uri=self.redirect_uri) auth = mendeley.start_authorization_code_flow() refresher = MendeleyAuthorizationCodeTokenRefresher(auth) session = MendeleySession( mendeley, tokens, client=auth.client, refresher=MendeleyAuthorizationCodeTokenRefresher(auth)) # test token expiration expires_at = social.extra_data['expires_at'] if (expires_at or 0) < time(): # renew refresher.refresh(session) # Store new tokens for key in session.token.keys(): social.extra_data[key] = session.token[key] social.save(update_fields=('extra_data', )) return session
def test_should_get_auth_code_login_url(): mendeley = Mendeley('id', 'secret', 'https://example.com', state_generator=DummyStateGenerator()) auth = mendeley.start_authorization_code_flow() assert auth.get_login_url() == 'https://api.mendeley.com/oauth/authorize?' \ 'response_type=code&' \ 'client_id=id&' \ 'redirect_uri=https%3A%2F%2Fexample.com&' \ 'scope=all&' \ 'state=state1234'
def test_should_get_auth_code_login_url(): mendeley = Mendeley('id', 'secret', 'https://example.com', state_generator=DummyStateGenerator()) auth = mendeley.start_authorization_code_flow() assert auth.get_login_url() == 'https://api.mendeley.com/oauth/authorize?' \ 'response_type=code&' \ 'client_id=id&' \ 'redirect_uri=https%3A%2F%2Fexample.com&' \ 'scope=all&' \ 'state=state1234'
def perform_action(self, request, *args, **kwargs): mendeley = Mendeley(settings.MENDELEY_ID, settings.MENDELEY_SECRET, settings.MENDELEY_REDIRECT) self.auth = mendeley.start_authorization_code_flow( request.session.get('state', '')) if 'state' not in request.session: request.session['state'] = self.auth.state print(request.session['state']) try: if not settings.MENDELEY_SSL_VERIFY: os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1' mendeley_session = self.auth.authenticate(request.get_full_path()) request.session['token'] = mendeley_session.token except Exception as e: print(self.auth.get_login_url()) raise e
def perform_action(self, request, *args, **kwargs): mendeley = Mendeley(settings.MENDELEY_ID, settings.MENDELEY_SECRET, settings.MENDELEY_REDIRECT) self.auth = mendeley.start_authorization_code_flow(request.session.get('state', '')) if 'state' not in request.session: request.session['state'] = self.auth.state print(request.session['state']) try: if not settings.MENDELEY_SSL_VERIFY: os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1' mendeley_session = self.auth.authenticate(request.get_full_path()) request.session['token'] = mendeley_session.token except Exception as e: print(self.auth.get_login_url()) raise e
def __init__(self, authinfo, group_id): """ Authenticate the Mendeley client """ mendeley = Mendeley( client_id=authinfo.client_id, client_secret=authinfo.client_secret, redirect_uri=authinfo.redirect_uri, ) auth = mendeley.start_authorization_code_flow() login_url = auth.get_login_url() response = requests.post( login_url, allow_redirects=False, data={ "username": authinfo.user, "password": authinfo.password }, ) redirect_url = response.headers["Location"] redirect_url = redirect_url.replace("http://", "https://") self.session = auth.authenticate(redirect_url) self.group = self.session.groups.get(group_id)
from mendeley import Mendeley import yaml import codecs with open('config.yml') as f: config = yaml.load(f) mendeley = Mendeley(config['clientId'], config['clientSecret'],"http://localhost:5000/oauth") auth = mendeley.start_authorization_code_flow() # mendeley_object = Mendeley(config['clientId'], config['clientSecret']) # auth = mendeley_object.start_client_credentials_flow() # mendeley = auth.authenticate() # page = mendeley.catalog.search('"gaia theory" "gaia hypothesis" geophysiology geofysiology daisyworld').list(100) page = mendeley.catalog.search('"gaia theory" "gaia hypothesis" geophysiology geofysiology daisyworld "daisy world"') with codecs.open('results.tsv', 'w', 'utf-8') as f: for doc in page.iter(20): f.write("%s\t%s\t%s\t%s\t%s\t%s\n" % (unicode(doc.title).replace("\n", " "), unicode(doc.year).replace("\n", " "), unicode(doc.abstract).replace("\n", " "), unicode(doc.source).replace("\n", " "), doc.identifiers, doc.link)) # The user needs to visit this URL, and log in to Mendeley. login_url = auth.get_login_url() print "Visit the login url: %s " % login_url x = raw_input("Insert the whole URL to authenticate: ") session = auth.authenticate(x) # doi = raw_input('Enter a DOI: ')
MENDELEY_REDIRECT = os.environ["MENDELEY_REDIRECT"] # Slack client for Web API requests slack_client = SlackClient(SLACK_BOT_TOKEN) # This will keep track of which pdfs to tag PDF_TAGS = {} # this will be used for the mendeley session mendeley_token = {} # this will keep track of which pdfs have been processed to avoid double posts processed_tokens = [] # Flask webserver for incoming traffic from Slack app = Flask(__name__) # Start mendeley client and Auth workflow men = Mendeley(MENDELEY_CLIENTID, MENDELEY_CLIENTSECRET, MENDELEY_REDIRECT) auth = men.start_authorization_code_flow() # message user to authorise mendeley api response = slack_client.api_call( "chat.postMessage", as_user=True, channel=SLACK_MENDELEY_LOGIN, text="Please go to this URL and login with your Mendeley account. After the successful login you will get to an error page. Copy the url of this error page and past it into the interactive dialog created by this bot in ~20 seconds: %s" % auth.get_login_url(), attachments=[] ) # give the user 20 seconds to retrieve the token (3 second timeout on the # interactive message) time.sleep(20) token_dm = slack_client.api_call( "chat.postMessage", as_user=True, channel=SLACK_MENDELEY_LOGIN,