Skip to content

Commit 43c4a06

Browse files
Added Basic Auth in track SDK
1 parent 93bc747 commit 43c4a06

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,19 @@ Install `interakt-track-python` using pip
77

88
pip install interakt-track-python
99

10+
## Authentication
1011
Inside your app, you’ll want to **set your** `write_key` before making any track calls:
12+
```
13+
import track
14+
15+
track.write_key = "YOUR_WRITE_KEY"
16+
```
17+
Interakt Track APIs uses HTTP Basic Auth, which involves a `‘username:password’` that is **base64 encoded** and prepended with the string `‘Basic ‘`.
18+
19+
Your **write_key** is your `username` and `password` is empty. Which means if your **write_key** is `'abcd123'`, a colon is added to it, and then the password field is left empty.
20+
21+
After base64 encoding `'abcd123:'` becomes `'YWJjZDEyMzo='`; and this is passed in the authorization header like so: `'Authorization: Basic YWJjZDEyMzo='`
1122

12-
import track
13-
14-
track.write_key = "YOUR_WRITE_KEY"
1523

1624

1725
## Development Settings
@@ -85,4 +93,4 @@ The `event` call has the following fields:
8593
|--|--|--|
8694
|user_id|str or int|The ID for the user in your database.|
8795
|event|str|Name of the event you want to track, For eg: "Product Added".|
88-
|traits|dict|dictionary of properties for the event. If the event was **Product Added**, it might have properties like `price` or `product_name`|
96+
|traits|dict|dictionary of properties for the event. If the event was **Product Added**, it might have properties like `price` or `product_name`.|

track/request.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@
1313

1414
def post(write_key, host=None, path=None, body=None, timeout=10):
1515
"""Post the msg to the API"""
16+
auth = HTTPBasicAuth(username=write_key, password="")
1617
headers = {
1718
'Content-Type': 'application/json',
18-
'User-Agent': f'interakt-track-python/{VERSION}',
19-
'Authorization': write_key
19+
'User-Agent': f'interakt-track-python/{VERSION}'
2020
}
2121
url = remove_trailing_slash(host or DEFAULT_HOST) + path
2222
logger.debug(f'Making request: {body}')
2323
response = _session.post(url=url, headers=headers,
24-
json=body, timeout=timeout)
24+
auth=auth, json=body, timeout=timeout)
2525
if response.status_code == 200:
2626
logger.debug("Data uploaded successfully")
2727
return response

0 commit comments

Comments
 (0)