Skip to content

Commit dce04f7

Browse files
committed
Add example.py
1 parent d081c83 commit dce04f7

1 file changed

Lines changed: 82 additions & 0 deletions

File tree

example.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import datetime
2+
import ox3apiclient
3+
import urllib
4+
5+
usr = ''
6+
pwd = ''
7+
8+
domain = ''
9+
realm = ''
10+
consumer_key = ''
11+
consumer_secret = ''
12+
13+
ox = ox3apiclient.OX3APIClient(domain, realm, consumer_key, consumer_secret)
14+
15+
# Step 1. Fetch temporary request token.
16+
ox.fetch_request_token()
17+
18+
# Step 2. Log in to SSO server and authorize token.
19+
ox.authorize_token(usr, pwd)
20+
21+
# 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+
#
30+
# token_str = ox.fetch_access_token()
31+
# access_token = urlparse.parse_qs(token_str)['oauth_token'][0]
32+
#
33+
#
34+
# Alternatively, after calling fetch_access_token you can grab it off the
35+
# "private" _token property like:
36+
#
37+
# access_token = ox._token.key
38+
#
39+
#
40+
ox.validate_session()
41+
42+
# Or manually validate your access token (but you will be resonpsible for
43+
# passing the requisite openx3_access_token for all successive API requests.
44+
# token_str = ox.fetch_access_token()
45+
# access_token = urlparse.parse_qs(token_str)['oauth_token'][0]
46+
# cookie_header = {'Cookie': 'openx3_access_token=' + access_token}
47+
# ox.request(url='http://youruidomain.com/ox/3.0/a/session/validate',
48+
# method='PUT',
49+
# headers=cookie_header)
50+
51+
52+
# Print out account names. We use overload=medium to get more than just a
53+
# listing of ids.
54+
accounts = ox.get('/a/account?overload=medium')
55+
for account in accounts:
56+
msg = '\tAccount ID: %s, Account Name: %s'
57+
print(msg % (account['id'], account['name']))
58+
59+
# We won't test object creation with accounts because they can't be deleted
60+
# currently.
61+
account_id = 0 #<= Replace with a valid advertiser account id for your instance.
62+
if account_id:
63+
# required_fields = ox.get('/a/order/requiredFields')
64+
# print(required_fields) #=> {u'status': u'string', u'name': u'string', u'account_id': u'int', u'start_date': u'datetime'}
65+
order = {
66+
'status': 'Active',
67+
'name': 'OX3APIClient Object Creation Test',
68+
'account_id': account_id,
69+
'start_date': datetime.datetime.today().strftime('%Y-%m-%d %H:%M:%S'),
70+
}
71+
72+
# OX3APIClient methods accept dicts for data parameters.
73+
order = ox.post('/a/order', data=order)
74+
# print(order) #=> {u'id': 12345}
75+
print('Created order id %s' % order['id'])
76+
77+
# Lets get all the details on the order we just created.
78+
order = ox.get('/a/order/%s' % order['id'])
79+
print(order)
80+
81+
# Log out.
82+
ox.delete('/a/session')

0 commit comments

Comments
 (0)