|
1 | 1 | import datetime |
2 | 2 | import ox3apiclient |
3 | | -import urllib |
4 | 3 |
|
5 | | -usr = '' |
6 | | -pwd = '' |
| 4 | +# User credentials |
| 5 | +email_address = '' |
| 6 | +password = '' |
7 | 7 |
|
| 8 | +# OAuth credentials. These will be supplied by your Account Manager or Support. |
8 | 9 | domain = '' |
9 | 10 | realm = '' |
10 | 11 | consumer_key = '' |
11 | 12 | consumer_secret = '' |
12 | 13 |
|
| 14 | + |
13 | 15 | ox = ox3apiclient.OX3APIClient(domain, realm, consumer_key, consumer_secret) |
14 | 16 |
|
15 | 17 | # Step 1. Fetch temporary request token. |
16 | 18 | ox.fetch_request_token() |
17 | 19 |
|
18 | 20 | # Step 2. Log in to SSO server and authorize token. |
19 | | -ox.authorize_token(usr, pwd) |
| 21 | +ox.authorize_token(email_address, password) |
20 | 22 |
|
21 | 23 | # 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: |
30 | 26 | # token_str = ox.fetch_access_token() |
31 | 27 | # access_token = urlparse.parse_qs(token_str)['oauth_token'][0] |
32 | | -# |
33 | | -ox.validate_session() |
| 28 | +ox.fetch_access_token() |
34 | 29 |
|
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: |
37 | 35 | # token_str = ox.fetch_access_token() |
38 | 36 | # access_token = urlparse.parse_qs(token_str)['oauth_token'][0] |
39 | 37 | # cookie_header = {'Cookie': 'openx3_access_token=' + access_token} |
40 | 38 | # ox.request(url='http://youruidomain.com/ox/3.0/a/session/validate', |
41 | 39 | # method='PUT', |
42 | 40 | # headers=cookie_header) |
| 41 | +ox.validate_session() |
43 | 42 |
|
44 | 43 |
|
| 44 | +# Now that we have connected let's try making a few API requests. |
45 | 45 | # Print out account names. We use overload=medium to get more than just a |
46 | 46 | # listing of ids. |
47 | 47 | accounts = ox.get('/a/account?overload=medium') |
48 | 48 | for account in accounts: |
49 | | - msg = '\tAccount ID: %s, Account Name: %s' |
| 49 | + msg = 'Account ID: %s, Account Name: %s' |
50 | 50 | print(msg % (account['id'], account['name'])) |
51 | 51 |
|
52 | 52 | # 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. |
54 | 54 | account_id = 0 #<= Replace with a valid advertiser account id for your instance. |
| 55 | + |
55 | 56 | 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') |
57 | 60 | # 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. |
58 | 64 | order = { |
59 | 65 | 'status': 'Active', |
60 | 66 | 'name': 'OX3APIClient Object Creation Test', |
61 | 67 | 'account_id': account_id, |
62 | 68 | 'start_date': datetime.datetime.today().strftime('%Y-%m-%d %H:%M:%S'), |
63 | 69 | } |
64 | 70 |
|
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) |
67 | 72 | # print(order) #=> {u'id': 12345} |
68 | | - print('Created order id %s' % order['id']) |
| 73 | + print('Created order id %s' % new_order['id']) |
69 | 74 |
|
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) |
73 | 78 |
|
74 | 79 | # Log out. |
75 | 80 | ox.delete('/a/session') |
0 commit comments