简体   繁体   English

云端点HTTP Cookie

[英]Cloud Endpoints HTTP Cookies

I am implementing Cloud Endpoints with a Python app that uses custom authentication ( GAE Sessions ) instead of Google Accounts. 我正在使用Python应用程序实施Cloud Endpoints,该应用程序使用自定义身份验证( GAE Sessions )而不是Google帐户。 I need to authenticate the requests coming from the Javascript client, so I would like to have access to the cookie information. 我需要验证来自Javascript客户端的请求,因此我希望能够访问cookie信息。

Reading this other question leads me to believe that it is possible, but perhaps not documented. 阅读这个其他问题让我相信这是可能的,但也许没有记录。 I'm not familiar with the Java side of App Engine, so I'm not quite sure how to translate that snippet into Python. 我不熟悉App Engine的Java端,所以我不太确定如何将该片段翻译成Python。 Here is an example of one of my methods: 这是我的一个方法的示例:

class EndpointsAPI(remote.Service):
  @endpoints.method(Query_In, Donations_Out, path='get/donations',
                    http_method='GET', name='get.donations')
  def get_donations(self, req):
    #Authenticate request via cookie

where Query_In and Donations_Out are both ProtoRPC messages ( messages.Message ). 其中Query_InDonations_Out都是ProtoRPC消息( messages.Message )。 The parameter req in the function is just an instance of Query_In and I didn't find any properties related to HTTP data, however I could be wrong. 函数中的参数req只是Query_In一个实例,我没有找到任何与HTTP数据相关的属性,但是我可能错了。

First, I would encourage you to try to use OAuth 2.0 from your client as is done in the Tic Tac Toe sample . 首先,我建议您尝试使用来自客户端的OAuth 2.0,就像在Tic Tac Toe 示例中所做的那样。

Cookies are sent to the server in the Cookie Header and these values are typically set in the WSGI environment with the keys 'HTTP_...' where ... corresponds to the header name: Cookie会在Cookie标头中发送到服务器,这些值通常在WSGI环境中使用密钥'HTTP_...' ,其中...对应于标头名称:

http = {key: value for key, value in os.environ.iteritems() 
        if key.lower().startswith('http')}

For cookies, os.getenv('HTTP_COOKIE') will give you the header value you seek. 对于cookie, os.getenv('HTTP_COOKIE')将为您提供所寻找的标题值。 Unfortunately, this doesn't get passed along through Google's API Infrastructure by default. 不幸的是,默认情况下,这不会通过Google的API基础结构传递。

UPDATE : This has been enabled for Python applications as of version 1.8.0 . 更新 :自1.8.0版本起,已为Python应用程序启用此功能。 To send cookies through, specify the following: 要通过cookie发送,请指定以下内容:

from google.appengine.ext.endpoints import api_config

AUTH_CONFIG = api_config.ApiAuth(allow_cookie_auth=True)

@endpoints.api(name='myapi', version='v1', auth=AUTH_CONFIG, ...)
class MyApi(remote.service):
    ...

This is a (not necessarily comprehensive list) of headers that make it through: 这是一个(不一定是全面的列表)标题,通过它:

  • HTTP_AUTHORIZATION
  • HTTP_REFERER
  • HTTP_X_APPENGINE_COUNTRY
  • HTTP_X_APPENGINE_CITYLATLONG
  • HTTP_ORIGIN
  • HTTP_ACCEPT_CHARSET
  • HTTP_ORIGINALMETHOD
  • HTTP_X_APPENGINE_REGION
  • HTTP_X_ORIGIN
  • HTTP_X_REFERER
  • HTTP_X_JAVASCRIPT_USER_AGENT
  • HTTP_METHOD
  • HTTP_HOST
  • HTTP_CONTENT_TYPE
  • HTTP_CONTENT_LENGTH
  • HTTP_X_APPENGINE_PEER
  • HTTP_ACCEPT
  • HTTP_USER_AGENT
  • HTTP_X_APPENGINE_CITY
  • HTTP_X_CLIENTDETAILS
  • HTTP_ACCEPT_LANGUAGE

For the Java people who land here. 对于在这里降落的Java人。 You need to add the following annotation in order to use cookies in endpoints: 您需要添加以下注释才能在端点中使用Cookie:

@Api(auth = @ApiAuth(allowCookieAuth = AnnotationBoolean.TRUE))

source 资源

(Without that it will work on the local dev server but not on the real GAE instance.) (没有它,它将在本地开发服务器上工作,但不在真正的GAE实例上工作。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM