2017-04-10 32 views
0

我對Google雲端點尤其是V2是新手,我認爲它是製作基於服務的應用程序的重要層。 從文檔我觀察他們提供了使用端點框架,然後打開API的選項。現在,當我使用所需的API密鑰的框架,然後生成openapi.json。我似乎無法得到任何錯誤,當我提出請求沒有API密鑰。我想知道爲什麼當我將我的課程標記爲需要的API密鑰時。爲什麼我自己提供驗證邏輯? 這是使用api驗證所有方法的配置。如何驗證Google雲終端中的API密鑰v2

@Api(name = "testapi", 
    version = "v1", 
    apiKeyRequired = AnnotationBoolean.TRUE, 
    scopes = {Constants.EMAIL_SCOPE}, 
    clientIds = {Constants.WEB_CLIENT_ID, Constants.ANDROID_CLIENT_ID}, 
    audiences = {Constants.ANDROID_AUDIENCE} 

然後生成openapi.json文件,這是POM的配置,雖然我仍然試圖掌握如何以這樣的方式,我不會要進入我的服務進行配置類

<profiles> 
    <profile> 
     <id>GetSwaggerDoc</id> 
     <activation> 
      <property> 
       <name>GetSwaggerDoc</name> 
      </property> 
     </activation> 
     <build> 
      <plugins> 
       <plugin> 
        <groupId>org.codehaus.mojo</groupId> 
        <artifactId>exec-maven-plugin</artifactId> 
        <version>1.4.0</version> 
        <configuration> 
         <includePluginDependencies>true</includePluginDependencies> 
         <mainClass>com.google.api.server.spi.tools.EndpointsTool</mainClass> 
         <arguments> 
          <argument>get-swagger-doc</argument> 
          <argument>--hostname=test-api.endpoints.${endpoints.project.id}.cloud.goog</argument> 
          <argument>--war=target/test-1.0-SNAPSHOT</argument> 
          <argument>com.rareatom.test.services.TestApi</argument> 
         </arguments> 
        </configuration> 
        <dependencies> 
         <dependency> 
          <groupId>com.google.endpoints</groupId> 
          <artifactId>endpoints-framework-tools</artifactId> 
          <version>${endpoints.framework.version}</version> 
         </dependency> 
         <dependency> 
          <groupId>com.google.appengine</groupId> 
          <artifactId>appengine-api-1.0-sdk</artifactId> 
          <version>1.9.30</version> 
         </dependency> 
        </dependencies> 
       </plugin> 
      </plugins> 
     </build> 
    </profile> 
</profiles>` 

當我運行mvn exec:java -DGetSwaggerDoc 這產生

{ 
 
"swagger": "2.0", 
 
"info": { 
 
    "version": "1.0.0", 
 
    "title": "test-api.endpoints.test-test-160113.cloud.goog" 
 
}, 
 
"host": "test-api.endpoints.test-test-160113.cloud.goog", 
 
"basePath": "/_ah/api", 
 
"schemes": [ 
 
    "https" 
 
], 
 
"consumes": [ 
 
    "application/json" 
 
], 
 
"produces": [ 
 
    "application/json" 
 
], 
 
"paths": { 
 
    "/testapi/v1/test": { 
 
    "post": { 
 
    "operationId": "TestapiTest", 
 
    "parameters": [ ], 
 
    "responses": { 
 
    "200": { 
 
     "description": "A successful response", 
 
     "schema": { 
 
     "$ref": "#/definitions/Response" 
 
     } 
 
    } 
 
    }, 
 
    "security": [ 
 
    { 
 
     "google_id_token": [ ] 
 
    }, 
 
    { 
 
     "google_id_token_https": [ ] 
 
    }, 
 
    { 
 
     "api_key": [ ] 
 
    } 
 
    ], 
 
    "x-security": [ 
 
    { 
 
     "google_id_token": { 
 
     "audiences": [ 
 
     "AIzaSyAsnv2yeF6003txjfBVrZrlUe8jvfUJAtE" 
 
     ] 
 
     } 
 
    }, 
 
    { 
 
     "google_id_token_https": { 
 
     "audiences": [ 
 
     "AIzaSyAsnv2yeF6003txjfBVrZrlUe8jvfUJAtE" 
 
     ] 
 
     } 
 
    } 
 
    ] 
 
    } 
 
    } 
 
}, 
 
"securityDefinitions": { 
 
    "google_id_token_https": { 
 
    "type": "oauth2", 
 
    "authorizationUrl": "", 
 
    "flow": "implicit", 
 
    "x-google-issuer": "https://accounts.google.com", 
 
    "x-google-jwks_uri": "https://www.googleapis.com/oauth2/v1/certs" 
 
    }, 
 
    "api_key": { 
 
    "type": "apiKey", 
 
    "name": "key", 
 
    "in": "query" 
 
    }, 
 
    "google_id_token": { 
 
    "type": "oauth2", 
 
    "authorizationUrl": "", 
 
    "flow": "implicit", 
 
    "x-google-issuer": "accounts.google.com", 
 
    "x-google-jwks_uri": "https://www.googleapis.com/oauth2/v1/certs" 
 
    } 
 
}, 
 
"definitions": { 
 
    "Status": { 
 
    "enum": [ 
 
    "SUCCESS", 
 
    "FAILURE" 
 
    ] 
 
    }, 
 
    "Response": { 
 
    "properties": { 
 
    "code": { 
 
    "type": "integer", 
 
    "format": "int32" 
 
    }, 
 
    "data": { 
 
    "$ref": "#/definitions/_any" 
 
    }, 
 
    "message": { 
 
    "type": "string" 
 
    }, 
 
    "status": { 
 
    "$ref": "#/definitions/Status" 
 
    } 
 
    } 
 
    }, 
 
    "_any": { } 
 
} 
 
}

然後使用gcloud service-management deploy部署此配置。

+0

可以請您發佈您的配置。你如何使你的API使用API​​密鑰? –

+0

@MichaelMeyer所有問題都已更新。 –

+0

你如何提出要求? – Nick

回答

1

我注意到,API密鑰進行驗證,當你有一個以上的參數或在你的pom.xml多個@Api註解的類和你的web.xml

的pom.xml

<arguments> 
    <argument>get-swagger-doc</argument> 
    <argument>--hostname=tita-api.endpoints.${endpoints.project.id}.cloud.goog</argument> 
    <argument>--war=target/tita-1.0-SNAPSHOT</argument>   
    <argument>com.rareatom.tita.services.TestApi</argument>        
    <argument>com.rareatom.tita.services.SessionServices</argument>      
         </arguments> 

併爲您的web.xml

<servlet> 
    <servlet-name>EndpointsServlet</servlet-name> 
    <servlet-class>com.google.api.server.spi.EndpointsServlet</servlet-class> 
    <init-param> 
     <param-name>services</param-name> 
     <param-value>com.rareatom.tita.services.TestApi, 
     com.rareatom.tita.services.SessionServices</param-value> 
</init-param> 
</servlet> 
+1

你能舉個例子說明你如何提出請求,以及處理請求的方法是什麼樣子的?您應該匿名處理您不想分享的任何內容。這對再現這種行爲是非常有幫助的,這點我沒有注意到。 – Nick