【发布时间】:2014-08-22 06:35:58
【问题描述】:
我有一个非常简单的应用程序,我从https://developers.google.com/google-apps/calendar/instantiate 复制并粘贴在下面:
import gflags
import httplib2
from apiclient.discovery import build
from oauth2client.file import Storage
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.tools import run
FLAGS = gflags.FLAGS
# Set up a Flow object to be used if we need to authenticate. This
# sample uses OAuth 2.0, and we set up the OAuth2WebServerFlow with
# the information it needs to authenticate. Note that it is called
# the Web Server Flow, but it can also handle the flow for native
# applications
# The client_id and client_secret can be found in Google Developers Console
FLOW = OAuth2WebServerFlow(
client_id='YOUR_CLIENT_ID',
client_secret='YOUR_CLIENT_SECRET',
scope='https://www.googleapis.com/auth/calendar',
user_agent='YOUR_APPLICATION_NAME/YOUR_APPLICATION_VERSION')
# To disable the local server feature, uncomment the following line:
# FLAGS.auth_local_webserver = False
# If the Credentials don't exist or are invalid, run through the native client
# flow. The Storage object will ensure that if successful the good
# Credentials will get written back to a file.
storage = Storage('calendar.dat')
credentials = storage.get()
if credentials is None or credentials.invalid == True:
credentials = run(FLOW, storage)
# Create an httplib2.Http object to handle our HTTP requests and authorize it
# with our good Credentials.
http = httplib2.Http()
http = credentials.authorize(http)
# Build a service object for interacting with the API. Visit
# the Google Developers Console
# to get a developerKey for your own application.
service = build(serviceName='calendar', version='v3', http=http,
developerKey='YOUR_DEVELOPER_KEY')
然后我添加了以下代码来访问 Google 日历 api(复制自 https://developers.google.com/google-apps/calendar/v3/reference/events/list):
page_token = None
while True:
events = service.events().list(calendarId='primary', pageToken=page_token).execute()
for event in events['items']:
print event['summary']
page_token = events.get('nextPageToken')
if not page_token:
break
但是,我需要在 Google 开发人员控制台中提供两个 IP 地址才能使服务正常运行;即我的 LAN ip(例如 192.168.1.111)和我当前的 IPv4 地址(我通过在 Google 上搜索 What is my IP address? 获得)。问题是,我的 ISP 不断更改分配给我的 IP 地址。因此,为了让我的测试应用程序正常工作,我必须去谷歌开发者的控制台,在My Project>>APIs & Auth>>Credentials>>Public API access 中我必须修改允许的 IP 列表。为方便起见,我将 LAN ip 设为静态。但是,我无法控制 ISP 分配给我的 IP 地址。如何配置使用我的笔记本电脑作为服务器并能够使用不断变化的 IP 地址进行身份验证的服务?
【问题讨论】:
-
不,如果不注册您的应用程序,您将无法访问任何 Google API。我对 python 的了解不够,无法提供帮助,但我很惊讶你不能像使用 php 进行测试那样将它设置为 localhost。
标签: python google-app-engine python-2.7 google-calendar-api