def test_get_application_access_token_v23_plus(): mock_request.return_value.status_code = 200 mock_request.return_value.content = ( '{"access_token":"<application access token>","token_type":"bearer"}' ) access_token, expires_at = get_application_access_token( '<application id>', '<application secret key>', api_version='2.3' ) mock_request.assert_called_with( 'GET', 'https://graph.facebook.com/v2.3/oauth/access_token', allow_redirects=True, verify=True, timeout=None, params={ 'client_id': '<application id>', 'client_secret': '<application secret key>', 'grant_type': 'client_credentials' } ) assert_equal(access_token, '<application access token>')
def generate_app_access_token(app_id, app_secret): """ Get an extended OAuth access token. :param access_token: A string describing an OAuth access token. :param application_id: An icdsnteger describing the Facebook application's ID. :param application_secret_key: A string describing the Facebook application's secret key. Returns a tuple with a string describing the extended access token and a datetime instance describing when it expires. """ # access tokens default_access_token = facepy.get_application_access_token( application_id = app_id, application_secret_key = app_secret ) graph = facepy.GraphAPI(default_access_token) response = graph.get( path='oauth/access_token', client_id = app_id, client_secret = app_secret, grant_type = 'client_credentials' ) components = parse_qs(response) token = components['access_token'][0] return token
def get_app_graph(): """ Returns the Graph object for the current app """ global app_graph try: app_graph.get('%s/app_domains' % settings.FACEBOOK_APP_ID) except: APP_TOKEN = facepy.get_application_access_token(settings.FACEBOOK_APP_ID, settings.FACEBOOK_APP_SECRET) app_graph = facepy.GraphAPI(APP_TOKEN) return app_graph
def get_app_graph(): """ Returns the Graph object for the current app """ global app_graph try: app_graph.get('%s/app_domains' % settings.FACEBOOK_APP_ID) except: APP_TOKEN = facepy.get_application_access_token( settings.FACEBOOK_APP_ID, settings.FACEBOOK_APP_SECRET) app_graph = facepy.GraphAPI(APP_TOKEN) return app_graph
def generate_extended_access_token(config): """ Get an extended OAuth access token. :param access_token: A string describing an OAuth access token. :param application_id: An integer describing the Facebook application's ID. :param application_secret_key: A string describing the Facebook application's secret key. Returns a tuple with a string describing the extended access token and a datetime instance describing when it expires. """ tests = [ not config['facebook'].has_key('stable_access_token'), config['facebook']['stable_access_token'] is None ] if any(tests): # access tokens default_access_token = facepy.get_application_access_token( application_id = config['facebook']['app_id'], application_secret_key = config['facebook']['app_secret'] ) graph = facepy.GraphAPI(default_access_token) response = graph.get( path='oauth/access_token', client_id = config['facebook']['app_id'], client_secret = config['facebook']['app_secret'], grant_type = 'fb_exchange_token', fb_exchange_token = config['facebook']['temp_access_token'] ) components = parse_qs(response) token = components['access_token'][0] expires_at = datetime.now() + timedelta(seconds=int(components['expires'][0])) config['facebook'].pop('stable_access_token', token) config['facebook'].pop('stable_access_token_expires_at', int(expires_at.strftime("%s"))) print "INFO: This is your new stable facebook access token: %s" % token print "INFO: It will expire on %s" % expires_at.strftime("%Y-%m-%d") return config else: return config
def _generate_app_access_token(self, app_id, app_secret): """ Get an extended OAuth access token. :param application_id: An icdsnteger describing the Facebook application's ID. :param application_secret_key: A string describing the Facebook application's secret key. Returns a tuple with a string describing the extended access token and a datetime instance describing when it expires. """ # access tokens default_access_token = facepy.get_application_access_token( application_id=app_id, application_secret_key=app_secret) graph = facepy.GraphAPI(default_access_token) response = graph.get(path='oauth/access_token', client_id=app_id, client_secret=app_secret, grant_type='client_credentials') return url.get_query_param(response, 'access_token')
def generate_extended_access_token(conf="config.yml"): """ Get an extended OAuth access token. :param access_token: A string describing an OAuth access token. :param application_id: An integer describing the Facebook application's ID. :param application_secret_key: A string describing the Facebook application's secret key. Returns a tuple with a string describing the extended access token and a datetime instance describing when it expires. """ # Configuration c = yaml.safe_load(open(conf)) app_id = c["fb_app_id"] app_secret = c["fb_app_secret"] temp_access_token = c["fb_temp_access_token"] # access tokens default_access_token = facepy.get_application_access_token(application_id=app_id, application_secret_key=app_secret) graph = facepy.GraphAPI(default_access_token) response = graph.get( path="oauth/access_token", client_id=app_id, client_secret=app_secret, grant_type="fb_exchange_token", fb_exchange_token=temp_access_token, ) components = parse_qs(response) token = components["access_token"][0] expires_at = datetime.now() + timedelta(seconds=int(components["expires"][0])) c["fb_stable_access_token"] = token c["fb_stable_access_token_expires_at"] = expires_at.strftime("%Y-%m-%d %H:%M:%S") with open(conf, "wb") as f: f.write(yaml.dump(c)) print "HERE IS YOUR STABLE ACCESS TOKEN: %s" % token print "IT EXPIRES AT %s" % expires_at print "YOUR CONFIG FILE (%s) HAS BEEN UPDATED" % conf
def test_get_application_access_token(): mock_request.return_value.status_code = 200 mock_request.return_value.content = 'access_token=...' access_token = get_application_access_token('<application id>', '<application secret key>') mock_request.assert_called_with( 'GET', 'https://graph.facebook.com/oauth/access_token', allow_redirects=True, verify=True, timeout=None, params={ 'client_id': '<application id>', 'client_secret': '<application secret key>', 'grant_type': 'client_credentials' } ) assert_equal(access_token, '...')
def test_get_application_access_token(): mock_request.return_value.status_code = 200 mock_request.return_value.content = 'access_token=<application access token>' access_token = get_application_access_token('<application id>', '<application secret key>') mock_request.assert_called_with( 'GET', 'https://graph.facebook.com/oauth/access_token', allow_redirects=True, verify=True, timeout=None, params={ 'client_id': '<application id>', 'client_secret': '<application secret key>', 'grant_type': 'client_credentials' }) assert_equal(access_token, '<application access token>')
def test_get_application_access_token_v23_plus(): mock_request.return_value.status_code = 200 mock_request.return_value.content = ( '{"access_token":"<application access token>","token_type":"bearer"}') access_token, expires_at = get_application_access_token( '<application id>', '<application secret key>', api_version='2.3') mock_request.assert_called_with( 'GET', 'https://graph.facebook.com/v2.3/oauth/access_token', allow_redirects=True, verify=True, timeout=None, params={ 'client_id': '<application id>', 'client_secret': '<application secret key>', 'grant_type': 'client_credentials' }) assert_equal(access_token, '<application access token>')
#python libraries import sys, time from datetime import datetime #third party libraries import sqlite3 as sql import requests, simplejson from facepy import GraphAPI,get_application_access_token #GLOBAL VARIABLES #ACCESS_TOKEN =raw_input("Enter a valid Facebook access token: ") ACCESS_TOKEN = get_application_access_token(appId,appSecret) page_name = raw_input("Enter the Facebook page name :") DB_NAME = raw_input("Enter SQLite DB name: ") num_userlikes = 1 num_posts = 0 num_albumlikes = 0 list_commentids = [] def getDBConnection(DB_NAME): try: con = sql.connect(DB_NAME) cur = con.cursor() return cur,con except sql.Error, e: print "Error %s:" % e.args[0] sys.exit(1) def insertPhotoFromAlbum(DB_NAME,album_id,album_name,photo_id): global ACCESS_TOKEN global num_userlikes