This API is now deprecated. Access to this API will be removed in late June 2023 after which the Tapjoy GraphQL API must be used for reporting purposes.
The following samples show how to access the publisher reporting API in Python (2 and 3).
#!/usr/bin/env python
"""
This example was written for Python 2.7
"""
import requests
import time
class PublisherReport:
def __init__(self, auth_key):
print 'Setting up'
self.base_url = 'https://api.tapjoy.com/%s'
self.auth_url = self.base_url % 'v1/oauth2/token'
self.report_url = self.base_url % 'v2/publisher/reports?'
self.auth_key = auth_key
self.token_valid_until = time.time() - 1
self.token = None
self.get_token()
self.response = {}
def get_token(self):
# get new token if we don't yet have one or if it's invalid
if self.token is None or self.token_invalid():
headers = {
'Authorization': 'Basic %s' % self.auth_key,
'Accept': 'application/json; */*'
}
print "Fetching auth token"
res = requests.post(self.auth_url, headers=headers)
try:
response = res.json()
self.token_valid_until = time.time() + response.get('expires_in')
self.token = response.get('access_token')
except ValueError:
print "An error occurred while we tried to decode the response. " \
"This could be due to invalid auth credentials."
exit(0) # For the sake of this tutorial, we'll just exit here and not do anything else.
return self.token
def get_report(self, date, page_size=1, page=1, time_increment='daily', group_by='content_types', mock=0):
headers = {
'Authorization': 'Bearer %s' % self.get_token(),
'Accept': 'application/json; */*'
}
data = {
'date': date,
'page_size': page_size,
'page': page,
'time_increment': time_increment,
'group_by': group_by,
'mock': mock
}
res = requests.get(self.report_url, headers=headers, data=data)
self.response = res.json()
def pretty_print(self):
print "\n"
print "Total number of apps: %s" % self.get_total_apps()
print "Page size: %s" % self.get_page_size()
print "Total pages: %s" % self.get_total_pages()
print "Date: %s" % self.get_date()
print "Current Page: %s" % self.get_current_page()
self.print_apps()
def token_invalid(self):
# in our example, a token is considered invalid if
# there are less than 10 seconds left before it expires
return self.token_valid_until - time.time() < 10
def get_total_apps(self):
return self.response.get('TotalApps')
def get_page_size(self):
return self.response.get('PageSize')
def get_total_pages(self):
return self.response.get('TotalPages')
def get_date(self):
return self.response.get('Date')
def get_current_page(self):
return self.response.get('CurrentPage')
def get_apps(self):
return self.response.get('Apps')
def print_apps(self):
for app in self.get_apps():
self.print_app(app)
def print_app(self, app):
print "\n" # print new line before each app
print "App Key: %s" % app.get('AppKey')
print "App Platform: %s" % app.get('Platform')
print "ContentTypes: "
app_content_types = app.get('ContentTypes')
for content_type in app_content_types:
self.print_content_types(content_type)
@staticmethod
def print_content_types(content_type):
content_type_name = content_type.get('ContentType')
content_type_global = content_type.get('Global')
content_type_countries = content_type.get('Countries')
print " Type: %s" % content_type_name
print " (Global)"
for key, val in content_type_global.iteritems():
# horizontal tab for formatting
print "\t%s : %s" % (key, val)
print " (Countries)"
for country in content_type_countries:
for key, val in country.iteritems():
# horizontal tab for formatting
print "\t%s : %s" % (key, val)
if __name__ == '__main__':
requests.packages.urllib3.disable_warnings()
p = PublisherReport(auth_key='INSERT API KEY HERE')
p.get_report('2016-12-10', page_size=2, mock=1)
p.pretty_print()
#!/usr/bin/env python
"""
This example was written for Python 3.8
"""
import requests
import time
class PublisherReport:
def __init__(self, auth_key):
print ('Setting up')
self.base_url = 'https://api.tapjoy.com/%s'
self.auth_url = self.base_url % 'v1/oauth2/token'
self.report_url = self.base_url % 'v2/publisher/reports?'
self.auth_key = auth_key
self.token_valid_until = time.time() - 1
self.token = None
self.get_token()
self.response = {}
def get_token(self):
# get new token if we don't yet have one or if it's invalid
if self.token is None or self.token_invalid():
headers = {
'Authorization': 'Basic %s' % self.auth_key,
'Accept': 'application/json; */*'
}
print ("Fetching auth token")
res = requests.post(self.auth_url, headers=headers)
try:
response = res.json()
self.token_valid_until = time.time() + response.get('expires_in')
self.token = response.get('access_token')
except ValueError:
print ("An error occurred while we tried to decode the response. " \
"This could be due to invalid auth credentials.")
exit(0) # For the sake of this tutorial, we'll just exit here and not do anything else.
return self.token
def get_report(self, date, page_size=1, page=1, time_increment='daily', group_by='content_types', mock=0):
headers = {
'Authorization': 'Bearer %s' % self.get_token(),
'Accept': 'application/json; */*'
}
data = {
'date': date,
'page_size': page_size,
'page': page,
'time_increment': time_increment,
'group_by': group_by,
'mock': mock
}
res = requests.get(self.report_url, headers=headers, data=data)
self.response = res.json()
def pretty_print(self):
print ("\n")
print ("Total number of apps: %s" % self.get_total_apps())
print ("Page size: %s" % self.get_page_size())
print ("Total pages: %s" % self.get_total_pages())
print ("Date: %s" % self.get_date())
print ("Current Page: %s" % self.get_current_page())
self.print_apps()
def token_invalid(self):
# in our example, a token is considered invalid if
# there are less than 10 seconds left before it expires
return self.token_valid_until - time.time() < 10
def get_total_apps(self):
return self.response.get('TotalApps')
def get_page_size(self):
return self.response.get('PageSize')
def get_total_pages(self):
return self.response.get('TotalPages')
def get_date(self):
return self.response.get('Date')
def get_current_page(self):
return self.response.get('CurrentPage')
def get_apps(self):
return self.response.get('Apps')
def print_apps(self):
for app in self.get_apps():
self.print_app(app)
def print_app(self, app):
print ("\n") # print new line before each app
print ("App Key: %s" % app.get('AppKey'))
print ("App Platform: %s" % app.get('Platform'))
print ("ContentTypes: ")
app_content_types = app.get('ContentTypes')
for content_type in app_content_types:
self.print_content_types(content_type)
@staticmethod
def print_content_types(content_type):
content_type_name = content_type.get('ContentType')
content_type_global = content_type.get('Global')
content_type_countries = content_type.get('Countries')
print (" Type: %s" % content_type_name)
print (" (Global)")
for key, val in content_type_global.items():
# horizontal tab for formatting
print ("\t%s : %s" % (key, val))
print (" (Countries)")
for country in content_type_countries:
for key, val in country.items():
# horizontal tab for formatting
print ("\t%s : %s" % (key, val))
if __name__ == '__main__':
requests.packages.urllib3.disable_warnings()
p = PublisherReport(auth_key='INSERT_AUTH_KEY')
p.get_report('2019-11-11', page_size=2, mock=1)
p.pretty_print()