-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgoogle_reporting_api.py
executable file
·45 lines (39 loc) · 1.54 KB
/
google_reporting_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/python
# Sample code for google reporting API
# More info on http://code.google.com/apis/apps/reporting/google_apps_reporting_api.html
import httplib, urllib
email = ""
passwd = ""
domain = "jnanaprabodhini.org"
date = "2007-12-01"
report_type = "daily"
report_name = "disk_space"
params = urllib.urlencode({'accountType': "HOSTED", 'Email': email, 'Passwd': passwd})
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
conn = httplib.HTTPSConnection("www.google.com")
conn.request("POST", "/accounts/ClientLogin", params, headers)
response = conn.getresponse()
data = response.read()
conn.close()
data = data.split('\n')
authtoken = data[0].split('=')[1]
envelope = """<?xml version="1.0" encoding="UTF-8"?>
<rest xmlns="google:accounts:rest:protocol"
xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance ">
<type>Report</type>"""
envelope += "<token>"+authtoken+"</token>"
envelope += "<domain>"+domain+"</domain>"
envelope += "<date>"+date+"</date>"
envelope += "<reportType>"+report_type+"</reportType>"
envelope += "<reportName>"+report_name+"</reportName>"
envelope += "</rest>"
envlen = len(envelope)
http_conn = httplib.HTTPS('www.google.com')
http_conn.putrequest('POST', '/hosted/services/v1.0/reports/ReportingData')
http_conn.putheader('Content-Type', 'text/xml; charset="utf-8"')
http_conn.putheader('Content-Length', str(envlen))
http_conn.endheaders()
http_conn.send(envelope)
(status_code, message, reply_headers) = http_conn.getreply()
response = http_conn.getfile().read()
print response