从Python客户端访问后端API(Google App Engine)

时间:2015-01-18 20:51:28

标签: python google-app-engine google-cloud-endpoints

我已经使用Google App Engine开发了一个项目的后端api。后端(用Java编写)目前有一个返回字符串列表的受保护方法。我遇到的问题是从python客户端访问此后端;专门访问我写的后端。我得到的错误来自于在以下代码中设置凭据。

def main():
  # Parse command line flags used by the oauth2client library.
  parser = argparse.ArgumentParser(
      description='Auth sample',
      formatter_class=argparse.RawDescriptionHelpFormatter,
      parents=[tools.argparser])
  flags = parser.parse_args(args=[])

  # Acquire and store oauth token.
  storage = oauth2client.file.Storage('<PROJECT_NAME>.json')
  credentials = storage.get()

  if credentials is None or credentials.invalid:
    flow = oauth2client.client.OAuth2WebServerFlow(
        client_id=CLIENT_ID,
        client_secret=CLIENT_SECRET,
        scope=SCOPE,
        user_agent=USER_AGENT,
        xoauth_displayname=OAUTH_DISPLAY_NAME)
    credentials = tools.run_flow(flow, storage, flags)
  http = httplib2.Http()
  http = credentials.authorize(http)

  # Build a service object for interacting with the API.
  api_root = 'https://localhost:8080/_ah/api'
  api = '<BACKEND_API_NAME>'
  version = 'v1'
  discovery_url = '%s/discovery/v1/apis/%s/%s/rest' % (api_root, api, version)
  service = discovery.build(
      api, version, discoveryServiceUrl=discovery_url, http=http)

  # Fetch all greetings and print them out.
  response = service.greetings().list().execute()
  return response

此代码是他们在App Engine网站(https://cloud.google.com/appengine/docs/python/endpoints/access_from_python)上提供的示例,但是,当&#34; tools.run_flow(流量,存储,标记)&#34;运行我收到此错误:&#34;运行时错误:错误(&#39;非法IP地址字符串传递给inet_pton&#39;,)&#34;有关为什么会发生这种情况的任何建议?或者有没有人知道访问OAuth保护方法的更好方法?谢谢。

0 个答案:

没有答案