From 70569a6e96cc08d67c4aade1c22b107072e76f10 Mon Sep 17 00:00:00 2001 From: Evan Chen Date: Thu, 6 Nov 2014 08:56:08 -0800 Subject: [PATCH] Made it easier to bootstrap and run tests + interactive by allowing user to specify access token in config Summary: Made it easier to bootstrap and run tests + interactive by allowing user to specify access token in config Test Plan: Define access_token in config --- README.md | 12 +++++------- config.json.example | 3 ++- facebookads/bootstrap.py | 15 ++++++++++----- facebookads/test/integration.py | 9 ++++++--- 4 files changed, 23 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 7a52c68..ae84860 100644 --- a/README.md +++ b/README.md @@ -82,16 +82,13 @@ The rest of the example code given will assume you have bootstrapped the api into your program like the following sample app: ```python -from facebookads.session import FacebookSession from facebookads.api import FacebookAdsApi from facebookads import objects -my_app_id = '' -my_app_secret = '' -my_access_token = '' -my_session = FacebookSession(my_app_id, my_app_secret, my_access_token) -my_api = FacebookAdsApi(my_session) -FacebookAdsApi.set_default_api(my_api) +my_app_id = '' +my_app_secret = '' +my_access_token = '' +FacebookAdsApi.init(my_app_id, my_app_secret, my_access_token) ``` **NOTE**: We shall use the objects module throughout the rest of the tutorial. @@ -322,6 +319,7 @@ details. ``` python -m facebookads.test.unit python -m facebookads.test.integration +# Access token not required if it's defined in config.json ``` ## Examples diff --git a/config.json.example b/config.json.example index 57c3302..c6ada88 100644 --- a/config.json.example +++ b/config.json.example @@ -2,5 +2,6 @@ "app_id": "", "app_secret": "", "act_id": "act_", - "page_id": "" + "page_id": "", + "access_token": "" } diff --git a/facebookads/bootstrap.py b/facebookads/bootstrap.py index caac6e8..7fe72e3 100644 --- a/facebookads/bootstrap.py +++ b/facebookads/bootstrap.py @@ -9,12 +9,14 @@ from facebookads.exceptions import FacebookError -def auth(access_token): - if not hasattr(main, '__file__'): - config_file = open('./config.json') - config = json.load(config_file) - config_file.close() +config_file = open('./config.json') +config = json.load(config_file) +config_file.close() + +def auth(access_token=None): + if sys.__stdin__.isatty(): + access_token = access_token or config['access_token'] FacebookAdsApi.init( config['app_id'], config['app_secret'], @@ -29,3 +31,6 @@ def auth(access_token): "## Or try using FacebookAdsApi.init()" "\n" ) + +if config['app_id'] and config['app_secret'] and config['access_token']: + auth() diff --git a/facebookads/test/integration.py b/facebookads/test/integration.py index 33333d5..3789431 100644 --- a/facebookads/test/integration.py +++ b/facebookads/test/integration.py @@ -429,10 +429,13 @@ def runTest(self): app_id = config['app_id'] app_secret = config['app_secret'] - if len(sys.argv) < 2: - raise TypeError("Please provide the access token as an argument") + if 'access_token' in config: + access_token = config['access_token'] + else: + if len(sys.argv) < 2: + raise TypeError("Please provide the access token as an argument") - access_token = sys.argv.pop() + access_token = sys.argv.pop() FacebookAdsTestCase.TEST_SESSION = session.FacebookSession( app_id,