Google应用引擎Calendar API

时间:2013-05-14 10:16:35

标签: python google-app-engine google-calendar-api

我在网站enter link description here中进行安装,并且效果很好但是当我从示例pf calendarsList中放入代码时它会失败。

main.py中的代码是:

#!/usr/bin/env python
#
# Copyright 2012 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

"""Starting template for Google App Engine applications.

Use this project as a starting point if you are just beginning to build a Google
App Engine project. Remember to download the OAuth 2.0 client secrets which can
be obtained from the Developer Console <https://code.google.com/apis/console/>
and save them as 'client_secrets.json' in the project directory.
"""

import httplib2
import logging
import os
import pickle

from apiclient.discovery import build
from oauth2client.appengine import oauth2decorator_from_clientsecrets
from oauth2client.client import AccessTokenRefreshError
from google.appengine.api import memcache
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app


# CLIENT_SECRETS, name of a file containing the OAuth 2.0 information for this
# application, including client_id and client_secret.
# You can see the Client ID and Client secret on the API Access tab on the
# Google APIs Console <https://code.google.com/apis/console>
CLIENT_SECRETS = os.path.join(os.path.dirname(__file__), 'client_secrets.json')

# Helpful message to display in the browser if the CLIENT_SECRETS file
# is missing.
MISSING_CLIENT_SECRETS_MESSAGE = """
<h1>Warning: Please configure OAuth 2.0</h1>
<p>
To make this sample run you will need to populate the client_secrets.json file
found at:
</p>
<code>%s</code>
<p>You can find the Client ID and Client secret values
on the API Access tab in the <a
href="https://code.google.com/apis/console">APIs Console</a>.
</p>

""" % CLIENT_SECRETS


http = httplib2.Http(memcache)
service = build("calendar", "v3", http=http)


# Set up an OAuth2Decorator object to be used for authentication.  Add one or
# more of the following scopes in the scopes parameter below. PLEASE ONLY ADD
# THE SCOPES YOU NEED. For more information on using scopes please see
# <https://developers.google.com/+/best-practices>.
decorator = oauth2decorator_from_clientsecrets(
    CLIENT_SECRETS,
    scope=[
      'https://www.googleapis.com/auth/calendar.readonly',
      'https://www.googleapis.com/auth/calendar',
    ],
    message=MISSING_CLIENT_SECRETS_MESSAGE)

class MainHandler(webapp.RequestHandler):

    @decorator.oauth_required
    def get(self):
        test = ""
        page_token = None
#example from calendarsList
        while True:
            calendar_list = service.calendarList().list(pageToken=page_token).execute()
            if calendar_list['items']:
                for calendar_list_entry in calendar_list['items']:
                    test+=calendar_list_entry['summary']
            page_token = calendar_list.get('nextPageToken')
            if not page_token:
                break

        self.response.out.write("""<html><body>

  <p>Congratulations, you are up and running! At this point you will want to add
  calls into the Calendar API to the <code>main.py</code> file. Please read the
  <code>main.py</code> file carefully, it contains detailed information in
  the comments.  For more information on the Calendar API Python library
  surface you can visit: </p>

 <blockquote>
   <p>
   <a href="https://google-api-client-libraries.appspot.com/documentation/calendar/v3/python/latest/">
   https://google-api-client-libraries.appspot.com/documentation/calendar/v3/python/latest/
   </a>
   </p>
 </blockquote>

  <p>
  Also check out the <a
    href="https://developers.google.com/api-client-library/python/start/get_started">
    Python Client Library documentation</a>, and get more information on the
  Calendar API at:
  </p>

  <blockquote>
    <p>
    <a href="https://developers.google.com/google-apps/calendar/firstapp">https://developers.google.com/google-apps/calendar/firstapp</a>
    </p>
  </blockquote>
""")

def main():
  application = webapp.WSGIApplication(
      [
       ('/', MainHandler),
       (decorator.callback_path, decorator.callback_handler()),
      ],
      debug=True)
  run_wsgi_app(application)


if __name__ == '__main__':
  main()

并且行中有错误:

calendar_list = service.calendarList().list(pageToken=page_token).execute()


HttpError: <HttpError 401 when requesting https://www.googleapis.com/calendar/v3/users/me/calendarList?alt=json returned "Login Required">

在文件client_secrets.json,当我打开时,我看到我的client_secret它是正确的,我的client_id也是,其他键我不认识它。

有人能帮我一个忙吗?

提前致谢

2 个答案:

答案 0 :(得分:0)

这个问题已经以各种方式被问过几次。 答案是将额外的参数传递给.execute()

e.g。

....execute(http=decorator.http())

另见

Why would I get Login Required for Calendar List Query but not for Calendar Queries

HttpError 401 "Login Required" while using Calendar v3 api python to insert event

答案 1 :(得分:0)

我长期以来一直反对这一点,因为无效的凭据是错误信息。

在开发环境中运行,您必须授权此链接对我有很大帮助!

https://gist.github.com/pwalsh/b8563e1a1de3347a8066