2014-12-28 125 views
1

我遇到了API問題,我無法創建用戶記錄/將其存儲在Google數據存儲中。我收到下面列出的JSON響應。Google App Engine的Python API問題

另一件我覺得奇怪的事情是,在右上角使用Google API瀏覽器時,它有一個紅色感嘆號,其中顯示「方法需要授權」。它希望我爲用戶電子郵件授權OAUTH Scope。

更新:這是我能夠通過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?我錯過了什麼嗎?

503 Service Unavailable 

- Hide headers - 

cache-control: private, max-age=0 
content-encoding: gzip 
content-length: 135 
content-type: application/json; charset=UTF-8 
date: Sun, 28 Dec 2014 23:23:55 GMT 
expires: Sun, 28 Dec 2014 23:23:55 GMT 
server: GSE 

{ 
"error": { 
    "errors": [ 
    { 
    "domain": "global", 
    "reason": "backendError", 
    "message": "Internal Server Error" 
    } 
    ], 
    "code": 503, 
    "message": "Internal Server Error" 
} 
} 

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_messages.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) 

models.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) 

main.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) 

的app.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 

回答

6

這裏:

@endpoints.method(UserCreateRequestMessage, UserCreateResponseMessage, 

你,說明該方法返回一個UserCreateResponseMessage;但隨後在這裏:

def user_create(self, request): 
    entity = User(username=request.username, email=request.email, 
        password=request.password) 
    entity.put() 

你回來沒有,就是返回None ...

+0

我才意識到這一點。我對後端工作和python相對來說比較陌生。我很感激幫助。 –

+0

真的很快。如何在存儲後返回實體。我想要的響應是用戶的JSON被存儲。返回UserCreateResponseMessage(email = request.email,password = request.password) –

+0

您定義的UserCreateResponseMessage它上面有三個字段:'email','username'和'id',這就是你必須設置的;它沒有'password'字段 - 只在請求中。 –