2017-02-28 10 views
0

python標準環境のクイックスタートでは、エンドポイントメソッドのtest_api_keyが503 Service Unavailableを返します。このエラーは、dev_appser.pyで実行したときとAPIをデプロイしたときにAPIエクスプローラで発生します。そのためのコードは次のとおりです。Google Cloud Endpoints Pythonクイックスタートエコーサンプルの問題

import endpoints 
from protorpc import message_types 
from protorpc import messages 
from protorpc import remote 

class TestResponse(messages.Message): 
    content = messages.StringField(1) 

@endpoints.api(name='practice', version='v1', description='My Practice API') 
class PracticeApi(remote.Service): 

    @endpoints.method(
     message_types.VoidMessage, 
     TestResponse, 
     path='test/getApiKey', 
     http_method='GET', 
     name='test_api_key') 
    def test_api_key(self, request): 
     return TestResponse(content=request.get_unrecognized_field_info('key')) 

api = endpoints.api_server([PracticeApi]) 

私は.get_unrecognized_field_info(「キー」)をよく理解していないので、私は問題が何であるかわかりませんか?ありがとう。

答えて

0

503エラーを作成していた3つのものがありました。

まず、方法または全体をApi require an Api Keyにする必要がありました。私はそれを展開する前put the Key into the openapi.json fileに必要なクラウドコンソールでAPIキーを生成した後、第二に

@endpoints.api(name='practice', version='v1', api_key_required=True) 
class PracticeApi(remote.Service): 

:この場合、私はちょうどAPI全体に適用しました。

ValidationError: Expected type <type 'unicode'> for field content, found (u'My Api Key', Variant(STRING, 9)) (type <type 'tuple'>) 

get_unrecognized_field_info()関数returns a tuple of (value, variant)

最後には、私はまだ検証エラーを得ていました。応答によってタプルは期待できませんでしたので、値を表示するだけの方法を更新しました:

def test_api_key(self, request): 
     return TestResponse(content=request.get_unrecognized_field_info('key')[0]) 
+0

ファイル(openapi.json)のどこにapiキーを配置しましたか?ありがとう –

+0

私は[このドキュメント](https://cloud.google.com/endpoints/docs/restricting-api-access-with-api-keys-openapi)のようにすべてのメソッドを追加していたので、制限していました。それをファイルの最上位レベルに置き、メソッド情報に入れ子にしないでください。 – Nicholas

1

まず、Google Cloud Endpointsが広範に使用しているので、Google Protocol RPC Library Overviewと読むことをお勧めします。

@ endpoints.methodでは、APIで特定のメソッドを設定できます。設定オプションについては、Google Cloud PlatformのドキュメントCloud Endpoints Frameworks for App EngineでのAPIの作成のセクションDefining an API method (@endpoints.method)に記載されています。

test/getApiKey/test_api_keyメソッドへのアクセスを制限する場合は、​​オプションを使用してメソッドを設定する必要があります。あなたの方法は、HTTPリクエスト(あなたのAPIを使用して、つまりクライアント)を表すリクエストパラメータを受け入れ注意

@endpoints.method(
     message_types.VoidMessage, 
     TestResponse, 
     path='test/getApiKey', 
     http_method='GET', 
     name='test_api_key', 
     api_key_required=True 
) 

def test_api_key(self, request): 

しかし、requestパラメータをさらにが、あなたの方法注釈があるべきRestricting API Access with API Keys (Frameworks)を議論実際にはGoogle Protocol RPC Message(Proto RPC)Messageオブジェクトであり、非常によく定義されています。追加のフィールドは、ProtoRPCのリクエストパラメータに存在する場合、正式に定義されたものを超えて、彼らはまだ要求オブジェクトで保存されているが、以下の方法を使用して取得する必要があります。

def get_unrecognized_field_info(self, key, value_default=None, 
            variant_default=None): 
    """Get the value and variant of an unknown field in this message. 
    Args: 
     key: The name or number of the field to retrieve. 
     value_default: Value to be returned if the key isn't found. 
     variant_default: Value to be returned as variant if the key isn't 
     found. 
    Returns: 
     (value, variant), where value and variant are whatever was passed 
     to set_unrecognized_field. 
    """ 

Message class code on GitHub

は非常によく文書化されています。 。

あなたはHTTPのGETで呼び出されるとメソッドを構成したので、引数なしでは、要求の本文に表示されません:

http_method='GET' 

...あなたは正しく値message_types.VoidMessageを使用しています。

エラーの点では、503は一般的なサーバーエラーです。StackDriver logsからの情報を提供できますか?彼らはあなたのコードの正確な行とエラーを指摘します。

+0

ありがとうございました。私はプロジェクトのAPIキーを生成しましたが、test_api_keyメソッドで必要なので、APIエクスプローラをどのように渡すかわかりません。 StackDriverからの503エラーメッセージは次のとおりです。ValidationError:フィールドコンテンツの予期されるタイプ(None、None)(タイプ<タイプ 'タプル'>) 'コード行 'return TestResponse(content = request.get_unrecognized_field_info( 'key'))' – Nicholas

+1

で発生します。API Explorerを使用していますか?https://apis-explorer.appspot.com/apis-explorer/?base = [...] '?その場合は、キーが設定されているアカウントでOAuthを使用することを許可しましたか?カールを使用している場合は、[https://cloud.google.com/endpoints/docs/restricting-api-access-with-api-keys-frameworks#calling_an_api_using_an_api_key](APIキーが記載されています)を渡します。 – Jack

+0

すべてのあなたの答えをありがとう。彼らは助けになり、私は上記の答えに私を導いた。 – Nicholas

関連する問題