【问题标题】:Is there a way to communicate with Google API without using the API key?有没有办法在不使用 API 密钥的情况下与 Google API 进行通信?
【发布时间】: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


【解决方案1】:

它不仅可以让您输入 IP 地址,还可以输入子网。您的 ISP 将从一个池中分配您的公共地址,您可以将该池键入开发者控制台。

例如,福特汽车公司的 IP 地址为 19.0.0.1,但实际上它的 IP 地址不止于此,它具有整个范围 19.0.0.0/8,即 19.0.0.0 到 19.255.255.255。

您可以在允许的地址列表中输入 19.0.0.1,也可以在整个范围内输入 19.0.0.0/8。

您的宽带调制解调器或网关很可能从您的 ISP 获得 DHCP 租约。 DHCP 租约包括网络掩码信息。从网络掩码信息中,您可以得出您的子网。

http://www.aelius.com/njh/subnet_sheet.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-18
    • 1970-01-01
    • 1970-01-01
    • 2019-01-01
    • 1970-01-01
    • 2012-09-18
    • 2019-08-06
    相关资源
    最近更新 更多