Skip to content

Commit

Permalink
Merge pull request #1 from MarletteFunding/DATA-347-microsoft-ads-exp…
Browse files Browse the repository at this point in the history
…ort-refresh-token-expires-after-90-days

Data 347 microsoft ads export refresh token expires after 90 days
  • Loading branch information
wmattern0 authored Nov 18, 2022
2 parents 55bffa3 + 54a1374 commit 2b6021d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ celerybeat-schedule
# virtualenv
venv/
ENV/
.venv

# Spyder project settings
.spyderproject
Expand All @@ -95,3 +96,12 @@ ENV/
# Singer files
*.config.json
*.catalog.json

# IDE
.idea

# Singer config
config.json
tap-*.json
target-*.json
*config.json
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

setup(
name='tap-bing-ads',
version="2.2.1",
version="2.3.0",
description='Singer.io tap for extracting data from the Bing Ads API',
author='Stitch',
url='http://singer.io',
Expand Down
23 changes: 20 additions & 3 deletions tap_bing_ads/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"oauth_client_secret",
"refresh_token",
"developer_token",
"refresh_token_ssm_param"
]

# objects that are at the root level, with selectable fields in the Stitch UI
Expand Down Expand Up @@ -164,20 +165,36 @@ def get_authentication():
authentication = OAuthWebAuthCodeGrant(
CONFIG['oauth_client_id'],
CONFIG['oauth_client_secret'],
'') ## redirect URL not needed for refresh token
'', ## redirect URL not needed for refresh token
token_refreshed_callback=save_refresh_token_to_ssm)
# Retrieves OAuth access and refresh tokens from the Microsoft Account authorization service.
authentication.request_oauth_tokens_by_refresh_token(CONFIG['refresh_token'])
return authentication
except bingads.exceptions.OAuthTokenRequestException:
authentication = OAuthWebAuthCodeGrant(
CONFIG['oauth_client_id'],
CONFIG['oauth_client_secret'],
'',
oauth_scope='bingads.manage') ## redirect URL not needed for refresh token
'',## redirect URL not needed for refresh token
oauth_scope='bingads.manage',
token_refreshed_callback=save_refresh_token_to_ssm)
# Retrieves OAuth access and refresh tokens from the Microsoft Account authorization service.
authentication.request_oauth_tokens_by_refresh_token(CONFIG['refresh_token'])
return authentication


def save_refresh_token_to_ssm(oauth_tokens):
try:
import boto3
LOGGER.info(f"saving refresh token to SSM now at: {CONFIG['refresh_token_ssm_param']}")

ssm = boto3.client("ssm")
put_param_output: dict = ssm.put_parameter(Name=CONFIG['refresh_token_ssm_param'], Value=oauth_tokens.refresh_token, Overwrite=True)

LOGGER.info(f"put_param_output: {str(put_param_output)}")
except ImportError as e:
LOGGER.warning("SSM param not saved to SSM due to error starting boto3. Eventually, your refresh token will expire.")


@bing_ads_error_handling
def create_sdk_client(service, account_id):
# Creates SOAP client with OAuth refresh credentials for services
Expand Down

0 comments on commit 2b6021d

Please sign in to comment.