python - AuthSub 与 Google 应用引擎中的 Text_db

标签 python google-app-engine authentication gdata-api

我正在尝试使用 text_db 和 authsub 从应用程序引擎读取电子表格。

我读到http://code.google.com/appengine/articles/gdata.html并让它发挥作用。然后我读到http://code.google.com/p/gdata-python-client/wiki/AuthSubWithTextDB我尝试将两个文件合并到下面的文件(step4.py)中,但是当我在本地运行它时,我得到:

Traceback (most recent call last):
  File "/home/jmvidal/share/progs/googleapps/google_appengine/google/appengine/ext/webapp/__init__.py", line 498, in __call__
    handler.get(*groups)
  File "/home/jmvidal/share/progs/googleapps/google_appengine/glassboard/step4.py", line 56, in get
    session_token = client._GetDocsClient().UpgradeToSessionToken(auth_token) #If I don't pass this argument I get a NonAuthSubToken
  File "/home/jmvidal/share/progs/googleapps/google_appengine/glassboard/gdata/service.py", line 866, in UpgradeToSessionToken
    self.SetAuthSubToken(self.upgrade_to_session_token(token))
  File "/home/jmvidal/share/progs/googleapps/google_appengine/glassboard/gdata/service.py", line 885, in upgrade_to_session_token
    headers={'Content-Type':'application/x-www-form-urlencoded'})
  File "/home/jmvidal/share/progs/googleapps/google_appengine/glassboard/gdata/auth.py", line 678, in perform_request
    return http_client.request(operation, url, data=data, headers=headers)
  File "/home/jmvidal/share/progs/googleapps/google_appengine/glassboard/atom/http.py", line 163, in request
    return connection.getresponse()
  File "/home/jmvidal/share/progs/googleapps/google_appengine/google/appengine/dist/httplib.py", line 200, in getresponse
    self._allow_truncated, self._follow_redirects)
  File "/home/jmvidal/share/progs/googleapps/google_appengine/google/appengine/api/urlfetch.py", line 267, in fetch
    raise DownloadError(str(e))
DownloadError: ApplicationError: 2 nonnumeric port: ''

有人能解释一下吗?具体来说,为什么原始版本(第一个链接中的step3.py)有效,但我在此处对 UpgradeToSessionToken 的调用失败了?

# step4.py
#
# Trying to read spreadsheets from app engine using text_db and authsub.
#
# Merge of this code
# http://code.google.com/p/gdata-python-client/wiki/AuthSubWithTextDB
# with this one
# http://code.google.com/appengine/articles/gdata.html (step 3)

import wsgiref.handlers
import cgi
from google.appengine.ext import webapp
from google.appengine.api import users
import atom.url
import gdata.service
import gdata.alt.appengine
import gdata.spreadsheet.text_db
import settings


class Fetcher(webapp.RequestHandler):

  def get(self):
    # Write our pages title
    self.response.out.write("""<html><head><title>
        Google Data Feed Fetcher: read Google Data API Atom feeds</title>""")
    self.response.out.write('</head><body>')
    next_url = atom.url.Url('http', settings.HOST_NAME, path='/step4')
    # Allow the user to sign in or sign out
    if users.get_current_user():
      self.response.out.write('<a href="%s">Sign Out</a><br>' % (
          users.create_logout_url(str(next_url))))
    else:
      self.response.out.write('<a href="%s">Sign In</a><br>' % (
          users.create_login_url(str(next_url))))

    # Initialize a client to talk to Google Data API services.
#    client = gdata.service.GDataService()
#    auth_url =  client.GenerateAuthSubURL(
#      next_url,
#      ('http://docs.google.com/feeds/',), secure=False, session=True)

    client = gdata.spreadsheet.text_db.DatabaseClient()
    auth_url = client._GetDocsClient().GenerateAuthSubURL(
      next_url,
      ('http://spreadsheets.google.com/feeds/','http://docs.google.com/feeds/documents/'), secure=False, session=True)

    gdata.alt.appengine.run_on_appengine(client)

    feed_url = self.request.get('feed_url')

    session_token = None
    # Find the AuthSub token and upgrade it to a session token.
    auth_token = gdata.auth.extract_auth_sub_token_from_url(self.request.uri)
    if auth_token:
      # Upgrade the single-use AuthSub token to a multi-use session token.
      client._GetDocsClient().SetAuthSubToken(auth_token)
      session_token = client._GetDocsClient().UpgradeToSessionToken(auth_token) #If I don't pass this argument I get a NonAuthSubToken
      client._GetSpreadsheetsClient().SetAuthSubToken(client._GetDocsClient().GetAuthSubToken())
#      session_token = client.upgrade_to_session_token(auth_token)
    if session_token and users.get_current_user():
      # If there is a current user, store the token in the datastore and
      # associate it with the current user. Since we told the client to
      # run_on_appengine, the add_token call will automatically store the
      # session token if there is a current_user.
      client.token_store.add_token(session_token)
    elif session_token:
      # Since there is no current user, we will put the session token
      # in a property of the client. We will not store the token in the
      # datastore, since we wouldn't know which user it belongs to.
      # Since a new client object is created with each get call, we don't
      # need to worry about the anonymous token being used by other users.
      client.current_token = session_token

    self.response.out.write('<div id="main">')
    self.fetch_feed(client, feed_url)
    self.response.out.write('</div>')
    self.response.out.write(
        '<div id="sidebar"><div id="scopes"><h4>Request a token</h4><ul>')
    self.response.out.write('<li><a href="%s">Google Documents</a></li>' % (auth_url))
    self.response.out.write('</ul></div><br/><div id="tokens">')

  def fetch_feed(self, client, feed_url):
    # Attempt to fetch the feed.
    if not feed_url:
      self.response.out.write(
          'No feed_url was specified for the app to fetch.<br/>')
      example_url = atom.url.Url('http', settings.HOST_NAME, path='/step4',
          params={'feed_url':
              'http://docs.google.com/feeds/documents/private/full'}
          ).to_string()
      self.response.out.write('Here\'s an example query which will show the'
          ' XML for the feed listing your Google Documents <a '
          'href="%s">%s</a>' % (example_url, example_url))
      return
    try:
      response = client.Get(feed_url, converter=str)
      self.response.out.write(cgi.escape(response))
    except gdata.service.RequestError, request_error:
      # If fetching fails, then tell the user that they need to login to
      # authorize this app by logging in at the following URL.
      if request_error[0]['status'] == 401:
        # Get the URL of the current page so that our AuthSub request will
        # send the user back to here.
        next = atom.url.Url('http', settings.HOST_NAME, path='/step4',
          params={'feed_url': feed_url})
        # If there is a current user, we can request a session token, otherwise
        # we should ask for a single use token.
        auth_sub_url = client.GenerateAuthSubURL(next, feed_url,
            secure=False, session=True)
        self.response.out.write('<a href="%s">' % (auth_sub_url))
        self.response.out.write(
            'Click here to authorize this application to view the feed</a>')
      else:
        self.response.out.write(
            'Something went wrong, here is the error object: %s ' % (
                str(request_error[0])))


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


if __name__ == '__main__':
  main()

最佳答案

一如既往,我只有在放弃并寻求帮助后才能找到答案。

我们需要再添加两个对 run_on_appengine 的调用(以注册 text_db 客户端拥有的两个客户端):

gdata.alt.appengine.run_on_appengine(client)
gdata.alt.appengine.run_on_appengine(client._GetDocsClient())
gdata.alt.appengine.run_on_appengine(client._GetSpreadsheetsClient()) here

我本以为对 run_on_appengine 的第一次调用会导致另外两个调用,但我想不会。

哦,将 auth_url 行更改为:

auth_url = client._GetDocsClient().GenerateAuthSubURL(
      next_url,scope='http://spreadsheets.google.com/feeds/ http://docs.google.com/feeds/documents/', secure=False, session=True)

在列表中传递范围 url 导致“ token 没有正确的范围”错误。

关于python - AuthSub 与 Google 应用引擎中的 Text_db,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/870192/

相关文章:

python - 在 Google App Engine 上移植基本脚本

node.js - 如何在 Google Cloud Datastore (Node.js) 中执行 "keys-only-query"

php - 如何(正确)使用具有依赖关系的表单请求+策略+资源路由?

python - 如何以通用换行模式访问上传的文件?

python - python 中 os.rename 的中断

java - Endpoints Frameworks 2.0 迁移后 Endpoint Api 调用错误

authentication - SendGrid 域身份验证 Heroku

authentication - Symfony 2 UserInterface::equals($ user):比较以进行重新认证

python - 在 Python 中运行 ioctl 返回 ENOTTY - 不适合设备的 ioctl

python - 如何在 VSCode 中整体执行 python 函数(它拆分并仅将第一行发送给解释器)