Couldn't import gdata.calendar.service

422 views
Skip to first unread message

moparthi

unread,
Sep 9, 2008, 3:34:09 AM9/9/08
to Google App Engine
hi,

I tried to use calendar service in cgi-script.

This is my code

try:
from xml.etree import ElementTree
except ImportError:
from elementtree import ElementTree

import gdata.calendar.service
import gdata.service
import atom.service
import gdata.calendar
##import atom
##import getopt
##import sys
##import string

import cgi, sys
import cgitb; cgitb.enable() # for troubleshooting
import time

##print "Content-type: text/html"
##
##print """
## <html>
##
## <head><title>Sample Script</title></head>
##
## <body>
##
## <h3> Sample CGI Script </h3>
## """

sys.stderr = sys.stdout

html = """

<p>Previous message : %s</p>
<p> Time is %s</p>
<p> Age is %s</p>
<form action="" method="post">
Enter the User name : <input type="text" name="message"><br/>
Enter the Password &nbsp;: <input type="password" name="age">
Select a value :
<select name="dropdown>
<option value="RED">red</option>
<option value="ORANGE">orange</option>
<option value="BLUE">blue</option>
</select>
<input type="submit" value="Submit">
</form>

</body>

</html>
"""


class CalendarExample:

def __init__(self):
print 'In Class'
print "You're talking to a %s server." % (sys.platform)
print "Version is %s " % (sys.version)
print "<br/>Path is %s" % (sys.path)

def login(self, email, password):
"""Creates a CalendarService and provides ClientLogin auth details
to it.
The email and password are required arguments for ClientLogin.
The
CalendarService automatically sets the service to be 'cl', as is
appropriate for calendar. The 'source' defined below is an
arbitrary
string, but should be used to reference your name or the name of
your
organization, the app name and version, with '-' between each of
the three
values. The account_type is specified to authenticate either
Google Accounts or Google Apps accounts. See gdata.service or
http://code.google.com/apis/accounts/AuthForInstalledApps.html for
more
info on ClientLogin. NOTE: ClientLogin should only be used for
installed
applications and not for multi-user web applications."""

print "<h1>Hello</h1>"
print "<p>Id is %s " % (email,)
print "<p>Password is %s " % (password,)



self.cal_client = gdata.calendar.service.CalendarService()
self.cal_client.email = email
self.cal_client.password = password
self.cal_client.source = 'Google-Calendar_Python_Sample-1.0'
self.cal_client.ProgrammaticLogin()


def main():

form = cgi.FieldStorage()
message = form.getvalue("message", "")
age = form.getvalue("age", "")



now = time.gmtime()
displayedtime = time.strftime("%A %d %B %Y, %X", now)
contentheader = "Content-type: text/html"
print contentheader
print html % (message, displayedtime, age)

sample = CalendarExample()
sample.login(message, age)

if __name__ == '__main__':
# print '\n Main If condition'
main()


when i tried to run in google app engine using command
dev_appserver.py cgi

it showing an error as

importerror : No module named gdata.calendar.service

can anyone help me how to use calendar service with cgi

regards,
moparthi

Jeff S (Google)

unread,
Sep 10, 2008, 3:13:54 PM9/10/08
to Google App Engine
Hi moparthi,

I thought of a few suggestions that might help. The first is that the
atom and gdata directories from the gdata-python-client/src directory
need to be included in the top level directory for your app. The gdata
library source code will needs to be uploaded as part of your app.
This is my first guess as to why the imports would fail.

Also, there is an additional line which you must add to your code to
tell the gdata library that HTTP requests should use the App Engine
urlfetch API (since the default for the gdata library uses raw socket
connections which are not allowed in App Engine). In the latest
version of the gdata-python-client (version 1.2.0) you would do this:

import gdata.alt.appengine
...
self.cal_client = gdata.calendar.service.CalendarService()
gdata.alt.appengine.run_on_appengine(self.cal_client)

Lastly, I noticed that you are using ClientLogin, which requires a
username and password, and these are being passed around in plaintext.
I strongly recommend that you switch to using AuthSub authorization
instead. I've written an article which explains how to use AuthSub
with App Engine which you can find here http://code.google.com/appengine/articles/gdata.html

Let me know how it goes :)

Jeff

amc

unread,
Sep 11, 2008, 12:06:01 AM9/11/08
to Google App Engine
Hi,
Thanks for the information about the new api. I tried it out with
the contacts api and ran into a problem. Here's the code ...

#!/usr/bin/python
import wsgiref.handlers
import urllib
from google.appengine.ext import webapp
import gdata.service
import gdata.contacts.service
import gdata.alt.appengine

class Lister(webapp.RequestHandler):

def get(self):

self.gd_client = gdata.contacts.service.ContactsService()

gdata.alt.appengine.run_on_appengine(self.gd_client)

self.gd_client.email = 'some....@gmail.com'
self.gd_client.password = '*******'
self.gd_client.source = 'GoogleInc-ContactsPythonSample-1'
self.gd_client.ProgrammaticLogin()

self.response.out.write('<body>')
self.response.out.write('<div id="main"/>')
self.response.out.write('</body>')

def main():
application = webapp.WSGIApplication([('/.*', Lister),], debug=True)
wsgiref.handlers.CGIHandler().run(application)

if __name__ == '__main__':
main()

And this is the trace I see when it runs ...

Traceback (most recent call last):
File "/Users/Anton/Desktop/GoogleAppEngineLauncher.app/Contents/
Resources/GoogleAppEngine-default.bundle/Contents/Resources/
google_appengine/google/appengine/ext/webapp/__init__.py", line 499,
in __call__
handler.get(*groups)
File "/Users/Anton/mail/mail.py", line 42, in get
self.gd_client.ProgrammaticLogin()
File "/Users/Anton/mail/gdata/service.py", line 449, in
ProgrammaticLogin
headers={'Content-Type':'application/x-www-form-urlencoded'})
File "/Users/Anton/mail/gdata/alt/appengine.py", line 92, in request
data_str = __ConvertDataPart(data)
NameError: global name '_AppEngineHttpClient__ConvertDataPart' is not
defined

I'm new to app engine and python, so I may well have missed something.
I'd be grateful for your advice.

Thanks.

Anton

On Sep 10, 3:13 pm, "Jeff S (Google)" <j...@google.com> wrote:
> Hi moparthi,
>
> I thought of a few suggestions that might help. The first is that the
> atom and gdata directories from the gdata-python-client/src directory
> need to be included in the top level directory for your app. The gdata
> library source code will needs to be uploaded as part of your app.
> This is my first guess as to why the imports would fail.
>
> Also, there is an additional line which you must add to your code to
> tell the gdata library that HTTP requests should use the App Engine
> urlfetch API (since the default for the gdata library uses raw socket
> connections which are not allowed in App Engine). In the latest
> version of the gdata-python-client (version 1.2.0) you would do this:
>
> import gdata.alt.appengine
> ...
> self.cal_client = gdata.calendar.service.CalendarService()
> gdata.alt.appengine.run_on_appengine(self.cal_client)
>
> Lastly, I noticed that you are using ClientLogin, which requires a
> username and password, and these are being passed around in plaintext.
> I strongly recommend that you switch to using AuthSub authorization
> instead. I've written an article which explains how to use AuthSub
> with App Engine which you can find herehttp://code.google.com/appengine/articles/gdata.html

amc

unread,
Sep 11, 2008, 2:31:14 PM9/11/08
to Google App Engine
Hi Jeff,
I don't know why Python can't see the ConvertDataPart method
- I tried a few things with it to see if I could fix the problem.

Eventually, to help me progress a bit, I just inlined the logic at
line 92 in appengine.py where it was trying to call the function. The
program did what I wanted. Not a long term fix though, since the same
function is called above for a case I'm not hitting yet.

I tested on a Mac and a PC with new installs of the libraries and saw
the same behavior. Hopefully it's a trivial problem that you might be
able to figure out quickly.

Thanks.

Anton

On Sep 11, 12:06 am, amc <anton.mcconvi...@gmail.com> wrote:
> Hi,
>     Thanks for the information about the new api. I tried it out with
> the contacts api and ran into a problem. Here's the code ...
>
> #!/usr/bin/python
> import wsgiref.handlers
> import urllib
> from google.appengine.ext import webapp
> import gdata.service
> import gdata.contacts.service
> import gdata.alt.appengine
>
> class Lister(webapp.RequestHandler):
>
>   def get(self):
>
>     self.gd_client = gdata.contacts.service.ContactsService()
>
>     gdata.alt.appengine.run_on_appengine(self.gd_client)
>
>     self.gd_client.email = 'some.th...@gmail.com'

moparthi

unread,
Sep 12, 2008, 2:10:46 AM9/12/08
to Google App Engine
Thanks jeff,

Error was rectified and its working fine.

I H've small doubt, after authentication its generating authsub token.
How to get that token.

I just followed http://code.google.com/apis/calendar/developers_guide_python.html

what this have. But how to get that token parameter to retrieve events
on my calendar

Good day,

regards
moparthi
On Sep 11, 12:13 am, "Jeff S (Google)" <j...@google.com> wrote:
> Hi moparthi,
>
> I thought of a few suggestions that might help. The first is that the
> atom and gdata directories from the gdata-python-client/src directory
> need to be included in the top level directory for your app. The gdata
> library source code will needs to be uploaded as part of your app.
> This is my first guess as to why the imports would fail.
>
> Also, there is an additional line which you must add to your code to
> tell the gdata library that HTTP requests should use the App Engine
> urlfetch API (since the default for the gdata library uses raw socket
> connections which are not allowed in App Engine). In the latest
> version of the gdata-python-client (version 1.2.0) you would do this:
>
> import gdata.alt.appengine
> ...
> self.cal_client = gdata.calendar.service.CalendarService()
> gdata.alt.appengine.run_on_appengine(self.cal_client)
>
> Lastly, I noticed that you are using ClientLogin, which requires a
> username and password, and these are being passed around in plaintext.
> I strongly recommend that you switch to using AuthSub authorization
> instead. I've written an article which explains how to use AuthSub
> with App Engine which you can find herehttp://code.google.com/appengine/articles/gdata.html
> > moparthi- Hide quoted text -
>
> - Show quoted text -

Jeff S (Google)

unread,
Sep 12, 2008, 1:33:05 PM9/12/08
to Google App Engine
Hi Anton,

I was able to reproduce this issue, and it will be fixed in the next
release. Inlining the logic is one solution, you can also rename the
function so that it doesn't start with __.

Thank you,

Jeff

Jeff S (Google)

unread,
Sep 12, 2008, 5:18:19 PM9/12/08
to Google App Engine
Hi moparthi,

Once you have your AuthSub session token, you can get it out of your
client object using:

token_string = client.GetAuthSubToken()

Then you could store this somewhere and use it later by setting the
client's token:

client.SetAuthSubToken(token_string)

For example code illustrating AuthSub and App Engine, see the article
I linked to previously (it was this one: http://code.google.com/appengine/articles/gdata.html
). If you get stuck, feel free to share some of your code so that I
can get a better idea of how you are using the library.

Happy coding,

Jeff

On Sep 11, 11:10 pm, moparthi <priya.mopar...@gmail.com> wrote:
> Thanks jeff,
>
> Error was rectified and its working fine.
>
> I H've small doubt, after authentication its generating authsub token.
> How to get that token.
>
> I just followedhttp://code.google.com/apis/calendar/developers_guide_python.html

amc

unread,
Sep 12, 2008, 7:52:55 PM9/12/08
to Google App Engine
Great. Thanks for following up.

Anton

On Sep 12, 1:33 pm, "Jeff S (Google)" <j...@google.com> wrote:
> Hi Anton,
>
> I was able to reproduce this issue, and it will be fixed in the next
> release. Inlining the logic is one solution, you can also rename the
> function so that it doesn't start with __.
>
> Thank you,
>
> Jeff
>
> On Sep 11, 11:31 am,amc<anton.mcconvi...@gmail.com> wrote:
>
> > Hi Jeff,
> >           I don't know why Python can't see the ConvertDataPart method
> > - I tried a few things with it to see if I could fix the problem.
>
> > Eventually, to help me progress a bit, I just inlined the logic at
> > line 92 in appengine.py where it was trying to call the function. The
> > program did what I wanted. Not a long term fix though, since the same
> > function is called above for a case I'm not hitting yet.
>
> > I tested on a Mac and a PC with new installs of the libraries and saw
> > the same behavior. Hopefully it's a trivial problem that you might be
> > able to figure out quickly.
>
> > Thanks.
>
> > Anton
>

moparthi

unread,
Sep 15, 2008, 1:11:58 AM9/15/08
to Google App Engine
Hi,

ok, this is my code wht i'm doing.


try:
from xml.etree import ElementTree
except ImportError:
from elementtree import ElementTree
#import gdata.alt.appengine
import gdata.calendar.service
import gdata.service
import atom.service
import atom
import getopt
import sys
import string
import cgi, sys
import cgitb; cgitb.enable() # for troubleshooting
import time

class CalendarExample:

def __init__(self):
print ""

def GetAuthSubUrl(self):
next = 'http://localhost:8080/'
scope = 'http://www.google.com/calendar/feeds/'
secure = False
session = True
self.calendar_service = gdata.calendar.service.CalendarService()
return self.calendar_service.GenerateAuthSubURL(next, scope,
secure, session)

def _retrievecals(self):
parameters = cgi.FieldStorage()
authsub_token = parameters.getvalue("token",'')
print '<br/>AuthSub token is %s' %(authsub_token,)
## authsub_token1 = parameters['token']
self.calendar_service = gdata.calendar.service.CalendarService()
self.calendar_service.auth_token = authsub_token
print '<br/><p> Calendar Auth sub is %s ' %
(self.calendar_service.auth_token,)
return authsub_token
## self.calendar_service.UpgradeToSessionToken()

def _login(self):
authSubUrl = self.GetAuthSubUrl()
print '<a href="%s">Login to your Google account</a>' % authSubUrl

value=self._retrievecals()
if value is not '':
print '<br/>hello'
parameters = cgi.FieldStorage()
authsub_token1 = parameters['token']
print '<br/>Auth Sub token1 is %s ' % (authsub_token1,)
self.calendar_service = gdata.calendar.service.CalendarService()
self.calendar_service.auth_token = authsub_token1
self.calendar_service.UpgradeToSessionToken()
## feed = calendar_service.GetCalendarListFeed()
## for i, a_calendar in enumerate(feed.entry):
## print '\t%s. %s' % (i, a_calendar.title.text,)

def main():
sample = CalendarExample()
sample._login()

if __name__ == '__main__':
# print '\n Main If condition'
main()


after retrieving token Just I tried to Updgrade to session token it is
showing some socket error.

Traceback (most recent call last):
File "C:\Program Files\Google\google_appengine\google\appengine\tools
\dev_apps
erver.py", line 2272, in _HandleRequest
base_env_dict=env_dict)
File "C:\Program Files\Google\google_appengine\google\appengine\tools
\dev_apps
erver.py", line 339, in Dispatch
base_env_dict=base_env_dict)
File "C:\Program Files\Google\google_appengine\google\appengine\tools
\dev_apps
erver.py", line 1759, in Dispatch
self._module_dict)
File "C:\Program Files\Google\google_appengine\google\appengine\tools
\dev_apps
erver.py", line 1670, in ExecuteCGI
reset_modules = exec_script(handler_path, cgi_path, hook)
File "C:\Program Files\Google\google_appengine\google\appengine\tools
\dev_apps
erver.py", line 1573, in ExecuteOrImportScript
script_module.main()
File "C:\Program Files\Google\google_appengine\demos\pythontest\cgi
\test4.py",
line 60, in main
sample._login()
File "C:\Program Files\Google\google_appengine\demos\pythontest\cgi
\test4.py",
line 53, in _login
self.calendar_service.UpgradeToSessionToken()
File "C:\Program Files\Google\google_appengine\demos\pythontest\cgi
\gdata\serv
ice.py", line 433, in UpgradeToSessionToken
content_type='application/x-www-form-urlencoded')
File "C:\Program Files\Google\google_appengine\demos\pythontest\cgi
\atom\servi
ce.py", line 316, in HttpRequest
connection.endheaders()
File "C:\Python25\lib\httplib.py", line 860, in endheaders
self._send_output()
File "C:\Python25\lib\httplib.py", line 732, in _send_output
self.send(msg)
File "C:\Python25\lib\httplib.py", line 699, in send
self.connect()
File "C:\Python25\lib\httplib.py", line 1133, in connect
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
AttributeError: 'module' object has no attribute 'socket'
INFO 2008-09-15 05:08:20,760 dev_appserver.py] "GET /?
token=CPiz26mXAhCDx7aa
Aw HTTP/1.1" 500 -

I may be mistaken some where plz let you know me that.


regards,
moparthi

aXqd

unread,
Sep 16, 2008, 6:02:51 AM9/16/08
to Google App Engine
I also ran into this problem:
global name '_AppEngineHttpClient__ConvertDataPart' is not defined

you should remember to:
from gdata.alt.appengine import run_on_appengine
run_on_appengine(client)

moparthi

unread,
Sep 17, 2008, 3:00:18 AM9/17/08
to Google App Engine
hi, i am not able to follow up.

Could you calrify wht u mean to say.

regards,
moparthi

Jeff S (Google)

unread,
Sep 17, 2008, 12:30:43 PM9/17/08
to Google App Engine
Hi moparthi,

It looks like your app is trying to use httplib to make the requests
(the default behavior for the gdata-python-client library) but when
running in App Engine, you must use urlfetch to make HTTP requests. To
allow the gdata-python-client library to run on App Engine, you need
to include the following

import gdata.alt.appengine
...
self.calendar_service = gdata.calendar.service.CalendarService()
gdata.alt.appengine.run_on_appengine(self.calendar_service)

For more details see the following article: http://code.google.com/appengine/articles/gdata.html

Cheers,

Jeff

Jeff S (Google)

unread,
Sep 17, 2008, 12:31:24 PM9/17/08
to Google App Engine
This is a known issue in the 1.2.0 release of the gdata-python-client
library. If you download 1.2.1, this should be fixed.

Happy coding,

Jeff
Reply all
Reply to author
Forward
0 new messages