Skip to content

Commit 8221c49

Browse files
committed
Merge remote-tracking branch 'origin/ian-UID2-970-publisher-sdk' into ian-UID2-970-publisher-sdk
# Conflicts: # examples/sample_token_generate_refresh.py
2 parents ae36179 + 8c63dba commit 8221c49

File tree

3 files changed

+116
-1
lines changed

3 files changed

+116
-1
lines changed

README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,57 @@ decrypted = client.decrypt(uid_token)
6969
```
7070
2. If decryption was successful, use the token `decrypted.uid2`.
7171

72+
## Usage for Publishers
73+
74+
1. Create an instance of Uid2PublisherClient
75+
76+
`client = Uid2PublisherClient(UID2_BASE_URL, UID2_API_KEY, UID2_SECRET_KEY)`
77+
78+
2. Call a function that takes the user's email address or phone number as input and generates a `TokenGenerateResponse` object. The following example uses an email address:
79+
80+
`token_generate_response = client.generate_token(TokenGenerateInput.from_email(emailAddress).do_not_generate_tokens_for_opted_out())`
81+
82+
>IMPORTANT: Be sure to call this function only when you have obtained legal basis to convert the user’s [directly identifying information (DII)](https://unifiedid.com/docs/ref-info/glossary-uid#gl-dii) to UID2 tokens for targeted advertising.
83+
84+
>`do_not_generate_tokens_for_opted_out()` applies `policy=1` in the [/token/generate](https://unifiedid.com/docs/endpoints/post-token-generate#token-generation-policy) call. Without this, `policy` is omitted to maintain backwards compatibility.
85+
86+
### Standard Integration
87+
88+
If you're using standard integration (client and server) (see [UID2 SDK for JavaScript Integration Guide](https://unifiedid.com/docs/guides/publisher-client-side)), follow this step:
89+
90+
* Send this identity as a JSON string back to the client (to use in the [identity field](https://unifiedid.com/docs/sdks/client-side-identity#initopts-object-void)) using the following:
91+
92+
`token_generate_response.get_identity_json_string()` //Note: this method returns `None` if the user has opted out, so be sure to handle that case.
93+
94+
### Server-Only Integration
95+
96+
If you're using server-only integration (see [Publisher Integration Guide, Server-Only](https://unifiedid.com/docs/guides/custom-publisher-integration)):
97+
98+
1. Store this identity as a JSON string in the user's session, using the `token_generate_response.get_identity_json_string()` function. This method returns `None` if the user has opted out, so be sure to handle that case.
99+
2. To retrieve the user's UID2 token, use:
100+
101+
```
102+
identity = token_generate_response.get_identity()
103+
if identity:
104+
advertising_token = identity.get_advertising_token()
105+
```
106+
4. When the user accesses another page, or on a timer, determine whether a refresh is needed:
107+
1. Retrieve the identity JSON string from the user's session, and then call the following function that takes the identity information as input and generates an `IdentityTokens` object:
108+
109+
`identity = IdentityTokens.from_json_string(identityJsonString)`
110+
2. Determine if the identity can be refreshed (that is, the refresh token hasn't expired):
111+
112+
` if not identity or not identity.is_refreshable(): # we must no longer use this identity (for example, remove this identity from the user's session) `
113+
3. Determine if a refresh is needed:
114+
115+
`if identity.is_due_for_refresh()):`
116+
5. If needed, refresh the token and associated values:
117+
118+
`token_refresh_response = client.refresh_token(identity)`
119+
120+
6. Store `token_refresh_response.get_identity_json_string()` in the user's session. If the user has opted out, this method returns `None`, indicating that the user's identity should be removed from the session. To confirm optout, you can use the `token_refresh_response.is_optout()` function.
121+
122+
72123
## Development
73124

74125
First, build the Docker image with Python 3.6 and all dev dependencies. This is required for all subsequent commands. Run the following:
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import sys
2+
3+
from uid2_client import Uid2PublisherClient
4+
from uid2_client import TokenGenerateResponse
5+
from uid2_client import TokenGenerateInput
6+
7+
8+
def _usage():
9+
print('Usage: python3 sample_token_generate_refresh.py <base_url> <auth_key> <secret_key>', file=sys.stderr)
10+
sys.exit(1)
11+
12+
13+
if len(sys.argv) <= 3:
14+
_usage()
15+
16+
base_url = sys.argv[1]
17+
auth_key = sys.argv[2]
18+
secret_key = sys.argv[3]
19+
20+
21+
publisher_client = Uid2PublisherClient(base_url, auth_key, secret_key)
22+
print("Generating Token")
23+
token_generate_response = publisher_client.generate_token(TokenGenerateInput.from_email("[email protected]").do_not_generate_tokens_for_opted_out())
24+
25+
status = token_generate_response.status
26+
tokens = token_generate_response.get_identity()
27+
28+
advertising_token = tokens.get_advertising_token()
29+
refresh_token = tokens.get_refresh_token()
30+
refresh_response_key = tokens.get_refresh_response_key()
31+
refresh_from = tokens.get_refresh_from()
32+
refresh_expires = tokens.get_refresh_expires()
33+
identity_expires = tokens.get_identity_expires()
34+
json_string = tokens.get_json_string()
35+
36+
print('Status =', status)
37+
print('Advertising Token =', advertising_token)
38+
print('Refresh Token =', refresh_token)
39+
print('Refresh Response Key =', refresh_response_key)
40+
print('Refresh From =', refresh_from)
41+
print('Refresh Expires =', refresh_expires)
42+
print('Identity Expires =', identity_expires)
43+
print('As Json String =', json_string, "\n")
44+
45+
print("Refreshing Token")
46+
token_refresh_response = publisher_client.refresh_token(tokens)
47+
status = token_refresh_response.status
48+
tokens = token_refresh_response.get_identity()
49+
advertising_token = tokens.get_advertising_token()
50+
refresh_token = tokens.get_refresh_token()
51+
refresh_response_key = tokens.get_refresh_response_key()
52+
refresh_from = tokens.get_refresh_from()
53+
refresh_expires = tokens.get_refresh_expires()
54+
identity_expires = tokens.get_identity_expires()
55+
json_string = tokens.get_json_string()
56+
57+
print('Status =', status)
58+
print('Advertising Token =', advertising_token)
59+
print('Refresh Token =', refresh_token)
60+
print('Refresh Response Key =', refresh_response_key)
61+
print('Refresh From =', refresh_from)
62+
print('Refresh Expires =', refresh_expires)
63+
print('Identity Expires =', identity_expires)
64+
print('As Json String =', json_string)

uid2_client/encryption.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ def encrypt_data(data, identity_scope, **kwargs):
267267
268268
The keyword arguments key, keys, site_id and advertising_token can only be used in
269269
the following combinations:
270-
- key: use the specied key
270+
- key: use the specified key
271271
- keys and site_id: find the key for the specified site_id
272272
- keys and advertising_token: extract site_id from the token and find a key for it
273273
"""

0 commit comments

Comments
 (0)