【问题标题】:Not work sample basic不工作示例基本
【发布时间】:2016-02-29 01:33:28
【问题描述】:

我正在使用 Google Endpoints 和 http://endpoints-proto-datastore.appspot.com/ 中的 endpoints-proto-datastore 的基本示例

这是我的文件 widgettiny_api.py

import endpoints

from google.appengine.ext import ndb
from protorpc import remote

from endpoints_proto_datastore.ndb import EndpointsModel



class Noticia(EndpointsModel):
    _message_fields_schema = ('id', 'titulo', 'contenido', 'creado')

    titulo = ndb.StringProperty()
    contenido = ndb.StringProperty()
    url_imagen = ndb.StringProperty()
    url = ndb.StringProperty()
    creado = ndb.DateTimeProperty(auto_now_add=True)


@endpoints.api(
    name='WidgetTinyApi',
    version='v1',
    description='API usada para consumir las noticias desde el cliente android o widget.')
class WidgetTinyApi(remote.Service):

    @Noticia.method(
        path="noticia",
        http_method="POST",
        name="noticia.insert")
    def NoticiaInsert(self, my_model):
        my_model.put()
        return my_model


    @Noticia.method(
        request_fields=('id',),
        path='noticia/{id}',
        http_method='GET',
        name='noticia.get')
    def NoticiaGet(self, my_model):
        if not my_model.from_datastore:
            raise endpoints.NotFoundException('Noticia no encontrada')
        return my_model


    @Noticia.query_method(
        path="noticias",
        name="noticia.list")
    def NoticiaList(self, query):
        return query


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

我打开链接 /_ah/api/explorer 。我得到这个错误:

INFO     2013-11-22 16:24:28,992 module.py:608] default: "POST /_ah/spi/BackendService.getApiConfigs HTTP/1.1" 200 2957
ERROR    2013-11-22 16:24:29,354 discovery_api_proxy.py:55] Discovery API proxy failed on /_ah/api/discovery/v1/apis/generate/directory with 400.

Request: {"configs": ["{\"description\": \"API usada para consumir las noticias desde el cliente android o widget.\", \"abstract\": false, \"name\": \"WidgetTinyApi\", \"descriptor\": {\"methods\": {\"WidgetTinyApi.NoticiaList\": {\"response\": {\"$ref\": \"NoticiaCollection\"}}, \"WidgetTinyApi.NoticiaInsert\": {\"request\": {\"$ref\": \"Noticia\"}, \"response\": {\"$ref\": \"Noticia\"}}}, \"schemas\": {\"NoticiaCollection\": {\"type\": \"object\", \"id\": \"NoticiaCollection\", \"properties\": {\"nextPageToken\": {\"type\": \"string\"}, \"items\": {\"items\": {\"$ref\": \"Noticia\"}, \"type\": \"array\"}}}, \"Noticia\": {\"type\": \"object\", \"id\": \"Noticia\", \"properties\": {\"contenido\": {\"type\": \"string\"}, \"titulo\": {\"type\": \"string\"}, \"url_imagen\": {\"type\": \"string\"}, \"url\": {\"type\": \"string\"}, \"created\": {\"type\": \"string\"}}}}}, \"version\": \"v1\", \"extends\": \"thirdParty.api\", \"defaultVersion\": true, \"root\": \"http://localhost:20999/_ah/api\", \"adapter\": {\"bns\": \"http://localhost:20999/_ah/spi\", \"type\": \"lily\", \"deadline\": 10.0}, \"methods\": {\"widgetTinyApi.noticia.insert\": {\"scopes\": [\"https://www.googleapis.com/auth/userinfo.email\"], \"clientIds\": [\"292824132082.apps.googleusercontent.com\"], \"rosyMethod\": \"WidgetTinyApi.NoticiaInsert\", \"request\": {\"body\": \"autoTemplate(backendRequest)\", \"bodyName\": \"resource\"}, \"authLevel\": \"NONE\", \"httpMethod\": \"POST\", \"path\": \"noticia\", \"response\": {\"body\": \"autoTemplate(backendResponse)\", \"bodyName\": \"resource\"}}, \"widgetTinyApi.noticia.list\": {\"scopes\": [\"https://www.googleapis.com/auth/userinfo.email\"], \"clientIds\": [\"292824132082.apps.googleusercontent.com\"], \"rosyMethod\": \"WidgetTinyApi.NoticiaList\", \"request\": {\"body\": \"empty\"}, \"authLevel\": \"NONE\", \"httpMethod\": \"GET\", \"path\": \"noticias\", \"response\": {\"body\": \"autoTemplate(backendResponse)\", \"bodyName\": \"resource\"}}}}"]}

Response: {
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "invalid",
    "message": "Invalid Value"
   }
  ],
  "code": 400,
  "message": "Invalid Value"
 }
}

ERROR    2013-11-22 16:24:29,355 discovery_service.py:137] Failed to get API directory
INFO     2013-11-22 16:24:29,355 module.py:608] default: "GET /_ah/api/discovery/v1/apis HTTP/1.1" 404 9

什么是错误?

【问题讨论】:

    标签: python api endpoints-proto-datastore


    【解决方案1】:

    name='WidgetTinyApi' 无效。你需要一个小写的值。

    来自documentation

    名称值:

    • 必须以小写字母开头。
    • 必须匹配正则表达式[a-z]+[A-Za-z0-9]*

    【讨论】:

    • 我也遇到了同样的问题。对我来说,这是错误的版本号
    【解决方案2】:

    我在实施过程中遇到了同样的错误。我检查了 API 的名称是否与正则表达式匹配,并且它以小写字符开头。

    正如@0xAffe 评论的那样,我检查了版本号是“v1”。

    这是我的代码:

    import logging
    import endpoints
    from models.inmobiliaria import SalesExecutive
    from protorpc import remote
    
    
    @endpoints.api(name='juustoRestApi',
                   version='v1',
                   description='Enpoints API for the Juus.to app')
    class JuustoRestApi(remote.Service):
    
        @SalesExecutive.method(path='executive/create',
                               http_method='POST',
                               name='executive.create')
        def create_sales_executive(self, executive):
            executive.put()
            return executive
    
        @SalesExecutive.query_method(query_fields=('email', 'password'),
                                                   path='executive/login',
                                                   name='executive.login')
        def login(self, query):
            logging.info(query)
            return query
    
    application = endpoints.api_server([JuustoRestApi], restricted=False)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-22
      • 2018-06-03
      • 1970-01-01
      • 2011-10-09
      相关资源
      最近更新 更多