# HG changeset patch # User Peter Sanchez # Date 1445047077 25200 # Fri Oct 16 18:57:57 2015 -0700 # Node ID 957f448be8052dd6cdb29fa8ef5044d2b5397de6 # Parent a00cb277fb8ef123dfa3cb376d55124230e6fdd0 Added the ability to save FB token session diff --git a/tinder/utils.py b/tinder/utils.py --- a/tinder/utils.py +++ b/tinder/utils.py @@ -4,6 +4,7 @@ import json import datetime import requests +import cookielib from . import constants from .facebook import FacebookAuthRequest @@ -51,11 +52,21 @@ return api.API(auth_handler=auth) -def get_tinder_access_token(username, password): +def get_tinder_access_token(username=None, password=None, cookie_file=None): ''' Helper to login to your FB account and authorize the Tinder App to get an access token that will work with the Tinder API. ''' + if cookie_file is not None: + use_cookies = cookielib.LWPCookieJar( + filename=os.path.expanduser(cookie_file), + ) + try: + use_cookies.load() + except IOError: + # Cookie file doesn't exist yet. + pass + fb_url = ('https://www.facebook.com/dialog/oauth?client_id=464891386855067' '&redirect_uri=https://www.facebook.com/connect/login_success.' 'html&scope=basic_info,email,public_profile,user_about_me,' @@ -63,6 +74,9 @@ 'user_friends,user_interests,user_likes,user_location,' 'user_photos,user_relationship_details&response_type=token') req = FacebookAuthRequest(username=username, password=password) + if cookie_file is not None: + req.session.cookies = use_cookies + response = req.authorized_request(url=fb_url) if response.status_code != 200: raise ValueError('Error logging in {0}'.format(response.content)) @@ -71,4 +85,7 @@ res = re.search(r'.*access_token=(\w+)\&.*', location) if not res: raise ValueError('Unable to get access key {0}'.format(location)) + + if cookie_file is not None: + req.session.cookies.save() return res.groups()[0]