Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions v2/python/adsense_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Utility class to handle account and credential management."""

from google_auth_oauthlib.flow import InstalledAppFlow
Expand All @@ -37,6 +36,7 @@
# This access scope grants read-only access to the authenticated user's account.
SCOPES = ['https://www.googleapis.com/auth/adsense.readonly']


def get_account_id(service):
"""Gets the AdSense account id, letting the user choose if multiple exist.

Expand All @@ -54,11 +54,11 @@ def get_account_id(service):
print('Multiple accounts were found. Please choose:')
for i, account in enumerate(response['accounts']):
print(' %d) %s (%s)' % (i + 1, account['displayName'], account['name']))
selection = (input('Please choose number 1-%d>'
% (len(response['accounts']))))
selection = (input('Please choose number 1-%d>' % (len(response['accounts']))))
account_id = response['accounts'][int(selection) - 1]['name']
return account_id


def get_adsense_credentials(overwrite_existing_credentials=False):
"""Gets AdSense credentials (locally cached or by running the OAuth flow).

Expand All @@ -70,12 +70,11 @@ def get_adsense_credentials(overwrite_existing_credentials=False):
A credential object required for making authenticated API requests.
"""
credentials = None
if (os.path.isfile(CREDENTIALS_FILE) and not overwrite_existing_credentials
and not ALWAYS_REQUIRE_AUTHENTICATION ):
if (os.path.isfile(CREDENTIALS_FILE) and not overwrite_existing_credentials and not ALWAYS_REQUIRE_AUTHENTICATION):
credentials = Credentials.from_authorized_user_file(CREDENTIALS_FILE)
else:
flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES)
credentials = flow.run_console()
credentials = flow.run_local_server()
with open(CREDENTIALS_FILE, 'w') as credentials_file:
credentials_json = credentials.to_json()
if isinstance(credentials_json, str):
Expand Down