Skip to content

Commit 18e9017

Browse files
committed
Added AdSense Host API v4.1 samples.
Reviewed in https://codereview.appspot.com/7749046/. Index: samples/adsensehost/README =================================================================== new file mode 100644
1 parent 956c120 commit 18e9017

21 files changed

Lines changed: 1298 additions & 0 deletions

samples/adsensehost/README

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
A collection of command-line samples for the AdSense Host API.
2+
3+
api: adsensehost
4+
keywords: cmdline
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#!/usr/bin/python
2+
#
3+
# Copyright 2012 Google Inc. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""This example adds a new ad unit to a publisher ad client.
18+
19+
To get ad clients, run get_all_ad_clients_for_publisher.py.
20+
21+
Tags: accounts.adunits.insert
22+
"""
23+
24+
__author__ = 'jalc@google.com (Jose Alcerreca)'
25+
26+
import argparse
27+
import sys
28+
29+
from apiclient import sample_tools
30+
from oauth2client import client
31+
from sample_utils import GetUniqueName
32+
33+
# Declare command-line flags.
34+
argparser = argparse.ArgumentParser(add_help=False)
35+
argparser.add_argument(
36+
'account_id',
37+
help='The ID of the pub account on which to create the ad unit')
38+
argparser.add_argument(
39+
'ad_client_id',
40+
help='The ID of the ad client on which to create the ad unit')
41+
42+
43+
def main(argv):
44+
# Authenticate and construct service.
45+
service, flags = sample_tools.init(
46+
argv, 'adsensehost', 'v4.1', __doc__, __file__, parents=[argparser])
47+
account_id = flags.account_id
48+
ad_client_id = flags.ad_client_id
49+
50+
try:
51+
ad_unit = {
52+
'name': 'Ad Unit #%s' % GetUniqueName(),
53+
'contentAdsSettings': {
54+
'backupOption': {
55+
'type': 'COLOR',
56+
'color': 'ffffff'
57+
},
58+
'size': 'SIZE_200_200',
59+
'type': 'TEXT'
60+
},
61+
'customStyle': {
62+
'colors': {
63+
'background': 'ffffff',
64+
'border': '000000',
65+
'text': '000000',
66+
'title': '000000',
67+
'url': '0000ff'
68+
},
69+
'corners': 'SQUARE',
70+
'font': {
71+
'family': 'ACCOUNT_DEFAULT_FAMILY',
72+
'size': 'ACCOUNT_DEFAULT_SIZE'
73+
}
74+
}
75+
}
76+
77+
# Create ad unit.
78+
request = service.accounts().adunits().insert(adClientId=ad_client_id,
79+
accountId=account_id,
80+
body=ad_unit)
81+
82+
result = request.execute()
83+
print ('Ad unit of type "%s", name "%s" and status "%s" was created.' %
84+
(result['contentAdsSettings']['type'], result['name'],
85+
result['status']))
86+
87+
except client.AccessTokenRefreshError:
88+
print ('The credentials have been revoked or expired, please re-run the '
89+
'application to re-authorize')
90+
91+
if __name__ == '__main__':
92+
main(sys.argv)
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/python
2+
#
3+
# Copyright 2012 Google Inc. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""This example adds a custom channel to a host ad client.
18+
19+
To get ad clients, run get_all_ad_clients_for_host.py.
20+
21+
Tags: customchannels.insert
22+
"""
23+
24+
__author__ = 'jalc@google.com (Jose Alcerreca)'
25+
26+
import argparse
27+
import sys
28+
29+
from apiclient import sample_tools
30+
from oauth2client import client
31+
from sample_utils import GetUniqueName
32+
33+
# Declare command-line flags.
34+
argparser = argparse.ArgumentParser(add_help=False)
35+
argparser.add_argument(
36+
'ad_client_id',
37+
help='The ID of the ad client on which to create the custom channel')
38+
39+
40+
def main(argv):
41+
# Authenticate and construct service.
42+
service, flags = sample_tools.init(
43+
argv, 'adsensehost', 'v4.1', __doc__, __file__, parents=[argparser])
44+
ad_client_id = flags.ad_client_id
45+
46+
try:
47+
custom_channel = {
48+
'name': 'Sample Channel #%s' % GetUniqueName()
49+
}
50+
51+
# Add custom channel.
52+
request = service.customchannels().insert(adClientId=ad_client_id,
53+
body=custom_channel)
54+
55+
result = request.execute()
56+
print ('Custom channel with id "%s", code "%s" and name "%s" was created.'
57+
% (result['id'], result['code'], result['name']))
58+
59+
except client.AccessTokenRefreshError:
60+
print ('The credentials have been revoked or expired, please re-run the '
61+
'application to re-authorize')
62+
63+
if __name__ == '__main__':
64+
main(sys.argv)
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/python
2+
#
3+
# Copyright 2012 Google Inc. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""This example adds a URL channel to a host ad client.
18+
19+
To get ad clients, run get_all_ad_clients_for_host.py.
20+
21+
Tags: urlchannels.insert
22+
"""
23+
24+
__author__ = 'jalc@google.com (Jose Alcerreca)'
25+
26+
import argparse
27+
import sys
28+
29+
from apiclient import sample_tools
30+
from oauth2client import client
31+
32+
# Declare command-line flags.
33+
argparser = argparse.ArgumentParser(add_help=False)
34+
argparser.add_argument(
35+
'ad_client_id',
36+
help='The ID of the ad client on which to create the URL channel')
37+
argparser.add_argument(
38+
'url_pattern',
39+
help='The URL pattern for the new custom channel')
40+
41+
42+
def main(argv):
43+
# Authenticate and construct service.
44+
service, flags = sample_tools.init(
45+
argv, 'adsensehost', 'v4.1', __doc__, __file__, parents=[argparser])
46+
ad_client_id = flags.ad_client_id
47+
url_pattern = flags.url_pattern
48+
49+
try:
50+
custom_channel = {
51+
'urlPattern': url_pattern
52+
}
53+
54+
# Add URL channel.
55+
request = service.urlchannels().insert(adClientId=ad_client_id,
56+
body=custom_channel)
57+
58+
result = request.execute()
59+
print ('URL channel with id "%s" and URL pattern "%s" was created.' %
60+
(result['id'], result['urlPattern']))
61+
62+
except client.AccessTokenRefreshError:
63+
print ('The credentials have been revoked or expired, please re-run the '
64+
'application to re-authorize')
65+
66+
if __name__ == '__main__':
67+
main(sys.argv)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"installed": {
3+
"client_id": "[[INSERT CLIENT ID HERE]]",
4+
"client_secret": "[[INSERT CLIENT SECRET HERE]]",
5+
"redirect_uris": [],
6+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
7+
"token_uri": "https://accounts.google.com/o/oauth2/token"
8+
}
9+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/usr/bin/python
2+
#
3+
# Copyright 2012 Google Inc. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""This example deletes an ad unit on a publisher ad client.
18+
19+
To get ad clients, run get_all_ad_clients_for_publisher.py.
20+
To get ad units, run get_all_ad_units_for_publisher.py.
21+
22+
Tags: accounts.adunits.delete
23+
"""
24+
25+
__author__ = 'jalc@google.com (Jose Alcerreca)'
26+
27+
import argparse
28+
import sys
29+
30+
from apiclient import sample_tools
31+
from oauth2client import client
32+
33+
# Declare command-line flags.
34+
argparser = argparse.ArgumentParser(add_help=False)
35+
argparser.add_argument(
36+
'account_id',
37+
help='The ID of the pub account on which the ad unit exists')
38+
argparser.add_argument(
39+
'ad_client_id',
40+
help='The ID of the ad client on which the ad unit exists')
41+
argparser.add_argument(
42+
'ad_unit_id',
43+
help='The ID of the ad unit to be deleted')
44+
45+
46+
def main(argv):
47+
# Authenticate and construct service.
48+
service, flags = sample_tools.init(
49+
argv, 'adsensehost', 'v4.1', __doc__, __file__, parents=[argparser])
50+
account_id = flags.account_id
51+
ad_client_id = flags.ad_client_id
52+
ad_unit_id = flags.ad_unit_id
53+
54+
try:
55+
# Delete ad unit.
56+
request = service.accounts().adunits().delete(accountId=account_id,
57+
adClientId=ad_client_id,
58+
adUnitId=ad_unit_id)
59+
60+
result = request.execute()
61+
print 'Ad unit with ID "%s" was deleted.' % result['id']
62+
63+
except client.AccessTokenRefreshError:
64+
print ('The credentials have been revoked or expired, please re-run the '
65+
'application to re-authorize')
66+
67+
if __name__ == '__main__':
68+
main(sys.argv)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/python
2+
#
3+
# Copyright 2012 Google Inc. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""This example deletes a custom channel on a host ad client.
18+
19+
To get ad clients, run get_all_ad_clients_for_host.py.
20+
To get custom channels, run get_all_custom_channels_for_host.py.
21+
22+
Tags: customchannels.delete
23+
"""
24+
25+
__author__ = 'jalc@google.com (Jose Alcerreca)'
26+
27+
import argparse
28+
import sys
29+
30+
from apiclient import sample_tools
31+
from oauth2client import client
32+
33+
# Declare command-line flags.
34+
argparser = argparse.ArgumentParser(add_help=False)
35+
argparser.add_argument(
36+
'ad_client_id',
37+
help='The ad client ID that contains the custom channel')
38+
argparser.add_argument(
39+
'custom_channel_id',
40+
help='The ID of the custom channel to be deleted')
41+
42+
43+
def main(argv):
44+
# Authenticate and construct service.
45+
service, flags = sample_tools.init(
46+
argv, 'adsensehost', 'v4.1', __doc__, __file__, parents=[argparser])
47+
ad_client_id = flags.ad_client_id
48+
custom_channel_id = flags.custom_channel_id
49+
50+
try:
51+
# Delete custom channel.
52+
request = service.customchannels().delete(
53+
adClientId=ad_client_id, customChannelId=custom_channel_id)
54+
55+
result = request.execute()
56+
print 'Custom channel with ID "%s" was deleted.' % result['id']
57+
58+
except client.AccessTokenRefreshError:
59+
print ('The credentials have been revoked or expired, please re-run the '
60+
'application to re-authorize')
61+
62+
if __name__ == '__main__':
63+
main(sys.argv)

0 commit comments

Comments
 (0)