Hello Group,

As specified in the docs ( 
http://code.google.com/apis/calendar/developers_guide_protocol.html
),
I coded a function in python that sends a POST message over an HTTP
connection ( with the  <entry> xml corresponding to the new calendar
in the POST request message body )  to 'owncalendars' in order to
create a new calendar in 'owncalendars' feed. This is as per the
google prescription.
 However , doing so always results in server HTTP response of '400 Bad
Request'
 Here is the function i coded in python :

import httplib
import urllib
import xml.etree.ElementTree

session_id=''

def my_authorization():

    #emailid = raw_input('enter the email id:')
    #passwd = raw_input('enter your password:')
    #service = raw_input('enter the service name:')
    params = urllib.urlencode({'Email': '[EMAIL PROTECTED]',
'Passwd': 'test123', 'service': 'cl'})
    auth_headers = {"Content-type": "application/x-www-form-
urlencoded"}

    calendar_conn = httplib.HTTPSConnection("www.google.com")
    #calendar_conn.set_debuglevel(2)
    calendar_conn.request("POST", "/accounts/
ClientLogin",params,auth_headers)
    auth_response = calendar_conn.getresponse()

    data = auth_response.read()
    #print data
    token=data.partition('Auth=')
    #global auth_token
    auth_token = token[2]
    print "Auth Token \n"+auth_token
    calendar_conn.close()
    return auth_token

def get_all_calendar_feed(auth_token,calendar_conn):
    header = {"Authorization":"GoogleLogin auth =" + auth_token}
    #calendar_conn = httplib.HTTPConnection("www.google.com")
    #calendar_conn.set_debuglevel(2)
    global session_id
    if  session_id == '':
        calendar_conn.request("GET","/calendar/feeds/default/
allcalendars/full",None,header)
        response = calendar_conn.getresponse()
        data = response.read()
        print data

        if response.status == 302 :                              #
requested object has moved to a new url

            list = response.getheaders()         # list all the
response headers
            i = 0
            for list[i] in list :
                if str(list[i]).find('location') != -1 :
                    #print list[i]
                    moved_uri =
str(list[i]).partition("www.google.com")[2].partition("'")[0]
# extract moved uri from response header
                    print moved_uri
                    session_id = moved_uri.partition("=")
[2]                                          # extract session id from
moved uri
                    print session_id

            calendar_conn.request("GET",moved_uri,None,header)
            response = calendar_conn.getresponse()
            print response.status , response.reason
            if response.status == 200 :
                all_calendar_feed = response.read()
                file = open("all-calendar-feed.xml","w")
                file.write(all_calendar_feed)
                file.close()

def get_own_calendar_feed(auth_token,calendar_conn) :
    global session_id
    header = {"Authorization":"GoogleLogin auth =" + auth_token}
    calendar_conn.set_debuglevel(2)
    if session_id ==
'' :
# if an HTTP 302 has not been encountered before
        calendar_conn.request("GET","/calendar/feeds/default/
owncalendars/full",None,header)
        response = calendar_conn.getresponse()
        data = response.read()
        print data

 
else :
# HTTP 302 has been encountered before
        calendar_conn.request("GET","/calendar/feeds/default/
owncalendars/full" + "?gsessionid=" + session_id,None,header)
        response = calendar_conn.getresponse()
        own_calendar_feed = response.read()
        print response.status,response.reason
        if response.status == 200 :
            file = open("own-calendar-feed.xml","w")
            file.write(own_calendar_feed)
            file.close()

def create_new_calendar(auth_token,message_body) :
    global session_id
    header= {"Authorization":"GoogleLogin auth="+auth_token,"Content-
type":"application/atom+xml"}
    #header = {"Authorization":"GoogleLogin auth =" +
auth_token,"Content-Type":"application/atom+xml"}
    calendar_conn = httplib.HTTPConnection("www.google.com")
    calendar_conn.set_debuglevel(2)
    if session_id ==
'' :
# if an HTTP 302 has not been encountered before
        calendar_conn.request("POST","/calendar/feeds/default/
owncalendars/full",message_body,header)
        response = calendar_conn.getresponse()
        data = response.read()
        print response.status,response.reason
 
else :
# HTTP 302 has been encountered
        calendar_conn.request("POST","/calendar/feeds/default/
owncalendars/full" + "?gsessionid=" + session_id,message_body,header)
        response = calendar_conn.getresponse()
        data = response.read()
        print response.status , response.reason










if __name__ == "__main__":

    auth_token = my_authorization()
    calendar_conn = httplib.HTTPConnection("www.google.com")
    get_all_calendar_feed(auth_token,calendar_conn)
    get_own_calendar_feed(auth_token,calendar_conn)

    new_calendar = """<?xml version ='1.0'?>
                    <entry xmlns = 'http://www.w3.org/2005/Atom'
                         xmlns:gd = 'http://schemas.google.com/g/2005'
                        xmlns:gCal='http://schemas.google.com/gcal/
2005'>

                    <title type="text">Crazy frog</title>
                   <gCal:timezone value="Asia/Calcutta" />
                    <gCal:hidden value="false" />
                    <gCal:color value="#A32929" />
                    <gCal:selected value="true" />
                    <gCal:accesslevel value="owner" />
                    <gd:where rel='' label='' valueString='Oakland'></
gd:where>
                    </entry>"""
    create_new_calendar(auth_token,new_calendar)


kindly suggest

Thanks,
Rajat


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Google Calendar Data API" group.
To post to this group, send email to 
[email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/google-calendar-help-dataapi?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to