Skip to content

Commit

Permalink
🐛 Source Bing Ads: wrap auth errors with the config error (#30834)
Browse files Browse the repository at this point in the history
  • Loading branch information
davydov-d authored Sep 28, 2023
1 parent de6652f commit 5934995
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 3 deletions.
2 changes: 1 addition & 1 deletion airbyte-integrations/connectors/source-bing-ads/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ RUN pip install .
ENV AIRBYTE_ENTRYPOINT "python /airbyte/integration_code/main.py"
ENTRYPOINT ["python", "/airbyte/integration_code/main.py"]

LABEL io.airbyte.version=0.2.2
LABEL io.airbyte.version=0.2.3
LABEL io.airbyte.name=airbyte/source-bing-ads
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ data:
connectorSubtype: api
connectorType: source
definitionId: 47f25999-dd5e-4636-8c39-e7cea2453331
dockerImageTag: 0.2.2
dockerImageTag: 0.2.3
dockerRepository: airbyte/source-bing-ads
githubIssueLabel: source-bing-ads
icon: bingads.svg
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
import backoff
import pendulum
from airbyte_cdk.logger import AirbyteLogger
from airbyte_cdk.models import FailureType
from airbyte_cdk.utils import AirbyteTracedException
from bingads.authorization import AuthorizationData, OAuthTokens, OAuthWebAuthCodeGrant
from bingads.exceptions import OAuthTokenRequestException
from bingads.service_client import ServiceClient
from bingads.util import errorcode_of_exception
from bingads.v13.reporting.exceptions import ReportingDownloadException
Expand Down Expand Up @@ -90,7 +93,16 @@ def _get_access_token(self) -> OAuthTokens:
# clear caches to be able to use new access token
self.get_service.cache_clear()
self._get_auth_data.cache_clear()
return self.authentication.request_oauth_tokens_by_refresh_token(self.refresh_token)
try:
tokens = self.authentication.request_oauth_tokens_by_refresh_token(self.refresh_token)
except OAuthTokenRequestException as e:
raise AirbyteTracedException(
message=str(e),
internal_message="Failed to get OAuth access token by refresh token. "
"The user could not be authenticated as the grant is expired. The user must sign in again.",
failure_type=FailureType.config_error,
)
return tokens

def is_token_expiring(self) -> bool:
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
from unittest import mock
from unittest.mock import patch

import pytest
import source_bing_ads.client
from airbyte_cdk.utils import AirbyteTracedException
from bingads.authorization import OAuthTokens
from bingads.v13.reporting.exceptions import ReportingDownloadException
from suds import sudsobject
Expand Down Expand Up @@ -106,3 +108,20 @@ def test_handling_ReportingDownloadException(patched_request_tokens):
client._download_timeout = 600000
client.should_give_up(ReportingDownloadException(message="test"))
assert client._download_timeout == 600000


def test_get_access_token(requests_mock):
requests_mock.post(
"https://login.microsoftonline.com/tenant_id/oauth2/v2.0/token",
status_code=400,
json={
"error": "invalid_grant",
"error_description": "AADSTS70000: The user could not be authenticated as the grant is expired. The user must sign in again.",
},
)
with pytest.raises(
AirbyteTracedException,
match="Failed to get OAuth access token by refresh token. The user could not be authenticated as the grant is expired. "
"The user must sign in again.",
):
source_bing_ads.client.Client("tenant_id", "2020-01-01", client_id="client_id", refresh_token="refresh_token")
1 change: 1 addition & 0 deletions docs/integrations/sources/bing-ads.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ The Bing Ads API limits the number of requests for all Microsoft Advertising cli

| Version | Date | Pull Request | Subject |
|:--------|:-----------|:---------------------------------------------------------------------------------------------------------------------------------|:---------------------------------------------------------------------------------------------------------------------------------------------|
| 0.2.3 | 2023-09-28 | [30834](https://github.com/airbytehq/airbyte/pull/30834) | Wrap auth error with the config error. |
| 0.2.2 | 2023-09-27 | [30791](https://github.com/airbytehq/airbyte/pull/30791) | Fix missing fields for geographic performance reports. |
| 0.2.1 | 2023-09-04 | [30128](https://github.com/airbytehq/airbyte/pull/30128) | Add increasing download timeout if ReportingDownloadException occurs |
| 0.2.0 | 2023-08-17 | [27619](https://github.com/airbytehq/airbyte/pull/27619) | Add Geographic Performance Report |
Expand Down

0 comments on commit 5934995

Please sign in to comment.