I'm currently using Gdata v3, and OAuth for authentication.

This code will get a previously stored in datastore document and
update its title according to a new string passed to the handler.

   download = # I get my model from datastore here

    token = gdata.gauth.AeLoad(config.GDATA_TOKEN_KEY)
    if not token:
      logging.error('Cannot change download title. Token not found to
access GData API.')
      return

    client = gdata.docs.client.DocsClient(source=config.OAUTH_SOURCE,
auth_token=token)
    doc = client.GetDoc(download.resource_id)
    doc.title.text = title
    client.Update(doc)

The token is retrieved and store with this handlers:


class OAuthDancePage(templates.BaseRequestHandler):
  PATH = '/get-oauth-token'

  @templates.loginAdminRequired
  def get(self):
    client = gdata.docs.client.DocsClient(source=config.OAUTH_SOURCE)

    saved_request_token = gdata.gauth.AeLoad(config.GDATA_TOKEN_KEY)
    if not saved_request_token:
      self.error(403)
      return

    request_token =
gdata.gauth.AuthorizeRequestToken(saved_request_token,
self.request.uri)
    try:
      access_token = client.GetAccessToken(request_token)
    except gdata.client.RequestError, err:
      if 'Unable to upgrade' in str(err):
        #upgrade process stopped
        gdata.gauth.AeDelete(self.session.account.key().name())
        self.redirect('/gdata-auth')
        return

    gdata.gauth.AeSave(access_token, config.GDATA_TOKEN_KEY)

    #success
    self.redirect('/gdata-auth')


  @templates.loginAdminRequired
  def post(self):
    client = gdata.docs.client.DocsClient(source=config.OAUTH_SOURCE)

    oauth_callback_url = 'http://%s/get-oauth-token' %
self.request.host
    request_token = client.GetOAuthToken(config.OAUTH_SCOPES,
oauth_callback_url,
      config.OAUTH_CONSUMER_KEY,
consumer_secret=config.OAUTH_CONSUMER_SECRET)

    gdata.gauth.AeSave(request_token, config.GDATA_TOKEN_KEY)

    self.redirect(str(request_token.generate_authorization_url()))


class OAuthRevokeTokenPage(templates.BaseRequestHandler):
  PATH = '/revoke-oauth-token'

  @templates.loginAdminRequired
  def get(self):
    token = gdata.gauth.AeLoad(config.GDATA_TOKEN_KEY)
    if not token:
      self.redirect('/gdata-auth')
      return

    try:
      client =
gdata.docs.client.DocsClient(source=config.OAUTH_SOURCE)
      client.revoke_token(token)
    except gdata.client.RequestError:
      #error
      self.redirect('/gdata-auth')
      return

    gdata.gauth.AeDelete(config.GDATA_TOKEN_KEY)

    #success
    self.redirect('/gdata-auth')

How it works? Well, I have a page (/gdata-auth) which sends a POST to /
get-oauth-token, this redirects to google authorization page, where
the user may press ok or cancel. Then, the user is redirected back to
my page and make a GET request to /get-oauth-token. I store it and
then I use it as I previously show you. /revoke-oauth-token is
transparent.

My config (among others settings):
OAUTH_SOURCE = 'yourCompany-yourAppName-v1' # only for follow
standars, not necessary to use it
OAUTH_CONSUMER_KEY = 'consumer_key here'
OAUTH_CONSUMER_SECRET = 'consumer_secret here'
OAUTH_SCOPES = (
  'https://docs.google.com/feeds/',
)

consumer key and secret are obtained following this guide:
http://code.google.com/intl/es-ES/apis/accounts/docs/RegistrationForWebAppsAuto.html

I cannot post here all the code I use for my pages, but if you need
more info, or want more examples (uploading little files from app
engine to docs, updating it, etc.) contact me and I will show you them
=)

By the way, 
http://code.google.com/intl/es-ES/apis/documents/docs/3.0/developers_guide_python.html

On 7 feb, 17:06, Iap <[email protected]> wrote:
> Hi,
> I am looking for an easier tutorial about to get documents list from a
> Google Docs account.
> What I can find are:
>
> #1
> Google Documents List Data API 
> v3.0http://code.google.com/apis/documents/docs/3.0/developers_guide_pytho...
>
> #2
> OAuth in the Google Data Protocol Client 
> Librarieshttp://code.google.com/apis/gdata/docs/auth/oauth.html
>
> It's frustrated, that the above two documents do not provide a workable
> example to follow.
> The #1 reads that there are 3 methods to authenticate , but as stupid as I
> am,
> failed to figure out the right way to do it correctly. Then, I turned to #2.
>
> The #2 reads great and easier to follow than #2 (from a beginner's point of
> view)
> Until the last section: "Using an access token". They seem to be written by
> different persons.
> There is no "bridge" between that section and others,
> I have difficulty to follow and failed to make my script to work.
>
> It would be appreciated if anybody kindly provides a document
> with more complete example to follow.
> (I have searched Nick's blog , nothing found)
>
> Thanks in advance.
> Iap

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine" 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-appengine?hl=en.

Reply via email to