Skip to content

Commit 91b3f97

Browse files
committed
Updates example.py to be more explicit and readable
Updated example.py to be more explicit about initial credentials. Updated comments to be more readable. Updated commented call to requiredFields to show use of action parameter with the 'create' action. Updated order creation example to use descriptive variables instead of reusing same 'order' variable.
1 parent 8e95572 commit 91b3f97

1 file changed

Lines changed: 30 additions & 25 deletions

File tree

example.py

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,80 @@
11
import datetime
22
import ox3apiclient
3-
import urllib
43

5-
usr = ''
6-
pwd = ''
4+
# User credentials
5+
email_address = ''
6+
password = ''
77

8+
# OAuth credentials. These will be supplied by your Account Manager or Support.
89
domain = ''
910
realm = ''
1011
consumer_key = ''
1112
consumer_secret = ''
1213

14+
1315
ox = ox3apiclient.OX3APIClient(domain, realm, consumer_key, consumer_secret)
1416

1517
# Step 1. Fetch temporary request token.
1618
ox.fetch_request_token()
1719

1820
# Step 2. Log in to SSO server and authorize token.
19-
ox.authorize_token(usr, pwd)
21+
ox.authorize_token(email_address, password)
2022

2123
# Step 3. Swap temporary request token for permanent access token.
22-
ox.fetch_access_token()
23-
24-
# Step 4. Validate your access token.
25-
# You'll more than likely want to call the validate_session method, but you may
26-
# need to grab the access token directly for some reason (maybe this is a
27-
# server-side proxy between a custom site and the API?). Regardless, you can get
28-
# the access token when you call fetch_access_token:
29-
#
24+
# If you need to store the access token yourself you can do so with something
25+
# similar to:
3026
# token_str = ox.fetch_access_token()
3127
# access_token = urlparse.parse_qs(token_str)['oauth_token'][0]
32-
#
33-
ox.validate_session()
28+
ox.fetch_access_token()
3429

35-
# Or manually validate your access token (but you will be resonpsible for
36-
# passing the requisite openx3_access_token for all successive API requests).
30+
# Step 4. Validate your access token.
31+
# You'll more than likely want to call the validate_session method, but you can
32+
# manually validate your access token if needed. You will be resonpsible for
33+
# passing the requisite openx3_access_token for all successive API requests. A
34+
# method might look like the following:
3735
# token_str = ox.fetch_access_token()
3836
# access_token = urlparse.parse_qs(token_str)['oauth_token'][0]
3937
# cookie_header = {'Cookie': 'openx3_access_token=' + access_token}
4038
# ox.request(url='http://youruidomain.com/ox/3.0/a/session/validate',
4139
# method='PUT',
4240
# headers=cookie_header)
41+
ox.validate_session()
4342

4443

44+
# Now that we have connected let's try making a few API requests.
4545
# Print out account names. We use overload=medium to get more than just a
4646
# listing of ids.
4747
accounts = ox.get('/a/account?overload=medium')
4848
for account in accounts:
49-
msg = '\tAccount ID: %s, Account Name: %s'
49+
msg = 'Account ID: %s, Account Name: %s'
5050
print(msg % (account['id'], account['name']))
5151

5252
# We won't test object creation with accounts because they can't be deleted
53-
# currently.
53+
# currently. Instead, we will create an order under an advertiser account.
5454
account_id = 0 #<= Replace with a valid advertiser account id for your instance.
55+
5556
if account_id:
56-
# required_fields = ox.get('/a/order/requiredFields')
57+
58+
# You can check to see what fields are required for the create action.
59+
# required_fields = ox.get('/a/order/requiredFields?action=create')
5760
# print(required_fields) #=> {u'status': u'string', u'name': u'string', u'account_id': u'int', u'start_date': u'datetime'}
61+
62+
# OX3APIClient methods accept Python dicts for data parameters, so we can
63+
# define an order as a normal dict.
5864
order = {
5965
'status': 'Active',
6066
'name': 'OX3APIClient Object Creation Test',
6167
'account_id': account_id,
6268
'start_date': datetime.datetime.today().strftime('%Y-%m-%d %H:%M:%S'),
6369
}
6470

65-
# OX3APIClient methods accept dicts for data parameters.
66-
order = ox.post('/a/order', data=order)
71+
new_order = ox.post('/a/order', data=order)
6772
# print(order) #=> {u'id': 12345}
68-
print('Created order id %s' % order['id'])
73+
print('Created order id %s' % new_order['id'])
6974

70-
# Lets get all the details on the order we just created.
71-
order = ox.get('/a/order/%s' % order['id'])
72-
print(order)
75+
# Let's get all the details on the order we just created.
76+
existing_order = ox.get('/a/order/%s' % new_order['id'])
77+
print(existing_order)
7378

7479
# Log out.
7580
ox.delete('/a/session')

0 commit comments

Comments
 (0)