Google App Engin的Python API问题

2024-09-29 01:19:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我的API遇到了一个问题,我无法创建用户记录/将其存储在Google数据存储中。我收到下面列出的JSON响应。在

另一件让我感到奇怪的事情是,当使用googleapisexplorer时,它的右上角有一个红色的感叹号,上面写着“method requires authorization”。它想让我为用户电子邮件授权一个OAUTH作用域。在

更新:这是我通过GAE得到的错误日志。在

    Encountered unexpected error from ProtoRPC method implementation: ServerError (Method PhotoswapAPI.user_create expected response type <class 'photoswap_api_messages.UserCreateResponseMessage'>, sent <type 'NoneType'>)
Traceback (most recent call last):
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/protorpc-1.0/protorpc/wsgi/service.py", line 181, in protorpc_service_app
    response = method(instance, request)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/endpoints-1.0/endpoints/api_config.py", line 1332, in invoke_remote
    return remote_method(service_instance, request)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/protorpc-1.0/protorpc/remote.py", line 419, in invoke_remote_method
    type(response)))
ServerError: Method PhotoswapAPI.user_create expected response type <class 'photoswap_api_messages.UserCreateResponseMessage'>, sent <type 'NoneType'>

如果返回503,有人知道如何解决这个问题吗?我错过什么了吗?

^{pr2}$

photoswap_api.py文件

import endpoints
from photoswap_api_messages import UserCreateRequestMessage
from photoswap_api_messages import UserCreateResponseMessage
from protorpc import remote
from models import User


@endpoints.api(name='photoswap', version='v1')
class PhotoswapAPI(remote.Service):
    @endpoints.method(UserCreateRequestMessage, UserCreateResponseMessage,
                      path='user', http_method='POST',
                      name='user.create')
    def user_create(self, request):
        entity = User(username=request.username, email=request.email, password=request.password)
        entity.put()


APPLICATION = endpoints.api_server([PhotoswapAPI], restricted=False)

photoswap_api_消息.py

from protorpc import messages


class UserCreateRequestMessage:
    email = messages.StringField(1)
    password = messages.StringField(2)


class UserCreateResponseMessage:
    email = messages.StringField(1)
    username = messages.StringField(2)
    id = messages.IntegerField(3)

模型.py

from google.appengine.ext import ndb


TIME_FORMAT_STRING = '%b %d, %Y %I:%M:%S %p'


class User(ndb.Model):
    username = ndb.StringProperty(required=True)
    email = ndb.StringProperty(required=True)
    id = ndb.IntegerProperty()


class Post(ndb.Model):
    user_id = ndb.IntegerProperty(required=True)
    id = ndb.IntegerProperty(required=True)
    desc = ndb.StringProperty(required=False)

主.py

    import webapp2
from protorpc import remote

class MainHandler(webapp2.RequestHandler):
    def get(self):
        self.response.write('Hello world!')

app = webapp2.WSGIApplication([
    ('/_ah/spi/.*', MainHandler)
], debug=True)

应用程序yaml

application: caramel-theory-800
version: 1
runtime: python27
threadsafe: true
api_version: 1

handlers:
# Endpoints handler
- url: /_ah/spi/.*
  script: photoswap_api.APPLICATION

libraries:
- name: pycrypto
  version: latest
- name: endpoints
  version: 1.0

Tags: frompyimportapiremoterequestmethodclass
1条回答
网友
1楼 · 发布于 2024-09-29 01:19:29

这里:

@endpoints.method(UserCreateRequestMessage, UserCreateResponseMessage,

您声明该方法返回一个UserCreateResponseMessage;但是这里:

^{pr2}$

你什么也不返回,也就是说,返回一个None。。。在

相关问题 更多 >