Skip to content

Commit 9087d61

Browse files
committed
add option for oauth auth
1 parent 77b32bf commit 9087d61

File tree

3 files changed

+55
-17
lines changed

3 files changed

+55
-17
lines changed

tap_linkedin_ads/__main__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"""LinkedInAds entry point."""
2+
3+
from __future__ import annotations
4+
5+
from tap_linkedin_ads.tap import TapLinkedInAds
6+
7+
TapLinkedInAds.cli()

tap_linkedin_ads/client.py

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,47 @@
77
from datetime import datetime, timezone
88
from pathlib import Path
99

10-
from singer_sdk.authenticators import BearerTokenAuthenticator
10+
import requests
11+
from singer_sdk.authenticators import BearerTokenAuthenticator, OAuthAuthenticator, SingletonMeta
1112
from singer_sdk.streams import RESTStream
1213

13-
if t.TYPE_CHECKING:
14-
import requests
15-
1614
SCHEMAS_DIR = Path(__file__).parent / Path("./schemas")
1715
UTC = timezone.utc
1816

17+
_Auth = t.Callable[[requests.PreparedRequest], requests.PreparedRequest]
18+
19+
20+
class LinkedInAdsOAuthAuthenticator(OAuthAuthenticator, metaclass=SingletonMeta):
21+
"""Authenticator class for LinkedInAds."""
22+
23+
@property
24+
def oauth_request_body(self):
25+
return {
26+
"grant_type": "refresh_token",
27+
"client_id": self.config["oauth_credentials"]["client_id"],
28+
"client_secret": self.config["oauth_credentials"]["client_secret"],
29+
"refresh_token": self.config["oauth_credentials"]["refresh_token"],
30+
}
31+
1932

2033
class LinkedInAdsStream(RESTStream):
2134
"""LinkedInAds stream class."""
2235

2336
records_jsonpath = "$[*]" # Or override `parse_response`.
24-
next_page_token_jsonpath = (
25-
"$.paging.start" # Or override `get_next_page_token`. # noqa: S105
26-
)
37+
next_page_token_jsonpath = "$.paging.start" # Or override `get_next_page_token`. # noqa: S105
2738

2839
@property
29-
def authenticator(self) -> BearerTokenAuthenticator:
40+
def authenticator(self) -> _Auth:
3041
"""Return a new authenticator object.
3142
3243
Returns:
3344
An authenticator instance.
3445
"""
46+
if "oauth_credentials" in self.config:
47+
return LinkedInAdsOAuthAuthenticator(
48+
self,
49+
auth_endpoint="https://www.linkedin.com/oauth/v2/accessToken",
50+
)
3551
return BearerTokenAuthenticator.create_for_stream(
3652
self,
3753
token=self.config["access_token"],
@@ -143,11 +159,7 @@ def parse_response(
143159
columns["run_schedule_start"] = datetime.fromtimestamp( # noqa: DTZ006
144160
int(schedule_column) / 1000,
145161
).isoformat()
146-
yield from (
147-
resp_json["elements"]
148-
if resp_json.get("elements") is not None
149-
else [columns]
150-
)
162+
yield from (resp_json["elements"] if resp_json.get("elements") is not None else [columns])
151163

152164
def _to_id_column(
153165
self,
@@ -161,9 +173,7 @@ def _to_id_column(
161173

162174
def _add_datetime_columns(self, columns): # noqa: ANN202, ANN001
163175
created_time = columns.get("changeAuditStamps").get("created").get("time")
164-
last_modified_time = (
165-
columns.get("changeAuditStamps").get("lastModified").get("time")
166-
)
176+
last_modified_time = columns.get("changeAuditStamps").get("lastModified").get("time")
167177
columns["created_time"] = datetime.fromtimestamp(
168178
int(created_time) / 1000,
169179
tz=UTC,

tap_linkedin_ads/tap.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,30 @@ class TapLinkedInAds(Tap):
2525
th.Property(
2626
"access_token",
2727
th.StringType,
28-
required=True,
2928
description="The token to authenticate against the API service",
3029
),
30+
# OAuth
31+
th.Property(
32+
"oauth_credentials",
33+
th.ObjectType(
34+
th.Property(
35+
"refresh_token",
36+
th.StringType,
37+
description="LinkedIn Ads Refresh Token",
38+
),
39+
th.Property(
40+
"client_id",
41+
th.StringType,
42+
description="LinkedIn Ads Client ID",
43+
),
44+
th.Property(
45+
"client_secret",
46+
th.StringType,
47+
description="LinkedIn Ads Client Secret",
48+
),
49+
),
50+
description="LinkedIn Ads OAuth Credentials",
51+
),
3152
th.Property(
3253
"start_date",
3354
th.DateTimeType,

0 commit comments

Comments
 (0)