2016-12-22 20 views
5

指出正確的文檔,教程,示例或提供一個,顯示如何將特定的身份驗證令牌添加到Swagger在Python中生成API客戶端?在python中使用swagger codegen客戶端將頭添加到api調用中的具體細節不清楚

這是我已經試過:
我的API調用工作得很好,用正確的curl命令:

curl -v -H 'X-CAG-Authorization: AG_CONSUMER_TOKEN access-key=31337-70k3n' \ 
    'https://api.company.net/api/v1/user/detail?user=1' 

* Trying 10.10.1.10... 
* Connected to api.company.net (10.10.1.10) port 443 (#0) 
* TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 
* Server certificate: *.company.net 
* Server certificate: COMODO RSA Organization Validation Secure Server CA 
* Server certificate: COMODO RSA Certification Authority 
> GET /api/v1/user/detail?user=1 HTTP/1.1 
> Host: api.company.net 
> User-Agent: curl/7.49.1 
> Accept: */* 
> X-CAG-Authorization: AG_CONSUMER_TOKEN access-key=31337-70k3n 
> 
< HTTP/1.1 200 OK 
< Server: openresty 
< Date: Thu, 22 Dec 2016 19:46:05 GMT 
< Content-Type: application/json;charset=UTF-8 
< Transfer-Encoding: chunked 
< Connection: close 
< Vary: Accept-Encoding 
< Vary: Accept-Encoding 
< X-UA-Compatible: IE=edge 
< 
{"successful":true,"message":"SUCCESS","body":{"… 

然而,當我嘗試在我的Python(2.7.12)客戶端相同的基本要求儘管確認該令牌將其放入要使用的標頭中,但我仍然收到授權失敗。更多關於正確使用客戶端的方式或關於如何獲得更多細節的確切請求和響應的細節將不勝感激。

/Users/me/VEnvs/sku-grade/bin/python /Users/me/prj/code/python_client/api_example.py 
HEADERS: 
{'X-CAG-Authorization': 'AG_CONSUMER_TOKEN access-key=31337-70k3n', 'User-Agent': 'Swagger-Codegen/1.0.0/python'} 
Exception when calling SupplierApi->get_api_v1_user_details: (401) 
Reason: Unauthorized 
HTTP response headers: HTTPHeaderDict({'Date': 'Thu, 22 Dec 2016 21:09:30 GMT', 'Content-Length': '636', 'Content-Type': 'application/json; charset=UTF-8', 'Connection': 'keep-alive', 'Server': 'nginx'}) 
HTTP response body: { 
    "code" : "PRECONDITION_FAILED", 
    "type" : "UnauthorizedApiDeniedException", 
    "message" : "Target API(/api/v1/user/details) is not available, you have to get a grant in advance.", 
    "messages" : {… 

這裏有一個招搖API SPEC:swagger.yaml

--- 
swagger: "2.0" 
info: 
    description: "API" 
    version: "TEMPORARY" 
    title: "User Details" 
    termsOfService: "http://wiki.company.net/tos" 
    contact: 
    name: "…" 
    license: 
    name: "…" 
host: "api.company.net" 
basePath: "/api/v1" 
tags: 
- name: "supplier" 
    description: "Supplier" 
schemes: 
- "https" 
produces: 
- "application/json" 
paths: 
    /user/details: 
    get: 
     tags: 
     - "supplier" 
     summary: "userDetails" 
     operationId: "getApiV1UserDetails" 
     consumes: 
     - "application/json" 
     produces: 
     - "application/json;charset=utf-8" 
     parameters: 
     - name: "user" 
     in: "query" 
     description: "user id" 
     required: true 
     type: "integer" 
     format: "Long" 
     responses: 
     200: 
      description: "OK" 
      schema: 
      $ref: "#/definitions/SupplierResponseOfUserDetailsDto" 
     401: 
      description: "Unauthorized" 
     403: 
      description: "Forbidden" 
     404: 
      description: "Not Found" 
definitions: 
    SupplierResponseOfUserDetailsDto: 
    type: "object" 
    properties: 
     body: 
     $ref: "#/definitions/UserDetailsDto" 
     message: 
     type: "string" 
     successful: 
     type: "boolean" 
    UserDetailsDto: 
    type: "object" 
    properties: 
     name: 
     type: "string" 

招搖,代碼生成是從http://editor.swagger.io/跑,我跟這裏的API例子試圖在額外頭添加:api_example.py

from __future__ import print_function 
import time 
import swagger_client 
from swagger_client import ApiClient 
from swagger_client import Configuration 
from swagger_client.rest import ApiException 
from pprint import pprint 

# Setup the authentication token header 
conf = Configuration() 
conf.api_key_prefix = {"teamname": "AG_CONSUMER_TOKEN"} 
conf.api_key = { 
    "teamname": "access-key=31337-70k3n" 
} 
conf.api_client = ApiClient(None, "X-CAG-Authorization", 
          conf.get_api_key_with_prefix("teamname")) 

# create an instance of the API class 
api_instance = swagger_client.SupplierApi() 
user = 1 
try: 
    api_response = api_instance.get_api_v1_user_details(user) 
    pprint(api_response) 
except ApiException as e: 
    print("Exception when calling " 
      "SupplierApi->get_api_v1_user_details: %s\n" % e) 

通過將print(self.api_client.default_headers)放入supplier_api.py中,我可以看到標題似乎已設置。

{'X-CAG-Authorization': 'AG_CONSUMER_TOKEN access-key=31337-70k3n', 'User-Agent': 'Swagger-Codegen/1.0.0/python'} 

所以,再一次,我應該在我的例子得到它通過對頭部和獲得授權完全是一個簡單的捲曲調用的方式做改變?

更新我也試着定義它:

 security: 
     - api_key: [] 
securityDefinitions: 
    api_key: 
    type: "apiKey" 
    name: "X-CAG-Authorization" 
    in: "header" 

,然後只設置與關鍵字:

swagger_client.configuration.api_key['X-CAG-Authorization'] = \ 
    'access-key=31337-70k3n' 
swagger_client.configuration.api_key_prefix['X-CAG-Authorization'] = \ 
    'AG_CONSUMER_TOKEN' 

但比頭消失並沒有改變多少其他來自我正在打印的默認標題。

+0

我也嘗試通過'swagger_client.configuration。...'參考''api_key_prefix'' api_key'和'api_client'的配置,其工作原理與上述相同;後來,我改變了我的規範,包括一個'securityDefinitions'並提到'security',然後刪除了這些配置設置,而只設置了api_key的名稱。這不會影響標題或更改行爲。 – dlamblin

+0

我也在swagger codegen項目中打開了這個問題:https://github.com/swagger-api/swagger-codegen/issues/4456 – dlamblin

回答

2

我試過你的代碼示例,它看起來像你的頭實際上傳遞給服務器。

您可以通過添加print headersswagger_client/rest.py文件,在此之前只是證實了這一點:

r = self.pool_manager.request(method, url, 
           fields=query_params, 
           preload_content=_preload_content, 
           timeout=timeout, 
           headers=headers) 

你肯定有在服務器端沒有什麼問題?也許有些標頭破壞了認證?

下面的curl命令是否也適用?

curl -v \ 
-H 'X-CAG-Authorization: AG_CONSUMER_TOKEN access-key=31337-70k3n' \ 
-H 'Content-Type: application/json' \ 
-H 'Accept: application/json;charset=utf-8' \ 
-H 'User-Agent: Swagger-Codegen/1.0.0/python' \ 
'https://api.company.net/api/v1/user/detail?user=1' 

因爲這應該給你完全相同的答案,返回到swagger,這是401錯誤。如果是這樣,你可以在服務器端進行調試。如果沒有,我不知道。

+0

謝謝,所有四個頭文件的curl語句繼續傳遞,所以我將仔細檢查https部分是否在python中工作,並且它不是路徑(它看起來是一樣的)。然後嘗試使用swagger-codegen的最新版本生成。 – dlamblin

+0

感謝您確定打印出所有方法,網址,字段和標題的正確位置。這樣做後,我意識到有兩個公共端點,只有一個正確工作,所以我現在得到完全相同的,正確的行爲。更多的文檔會很好,但在這種情況下,這是一個正確調試的問題。 – dlamblin

2

在你的天賦,你需要描述的安全設置與此類似example中的OpenAPI /揚鞭規範的securityDefinitions部分定義(在你的情況API密鑰)。

然後在終點,你將需要「應用」與此類似example

後的安全定義,您可以設置在自動生成的Python API客戶端configuration.py的API密鑰和HTTP請求將相應地包含API密鑰(或者在規範的安全設置中定義的標題或URL查詢字符串中)

自從Swagger Codegen的上一個穩定版本以來,已經有相當多的增強功能,生成的Python API客戶端,所以我建議你拉最新的master並在本地構建JAR以生成API客戶端。

+0

這是有用的附加細節。我在更新中注意到,我根據兩個示例更新了規範。我沒有修改configuration.py,因爲我不想在代碼中提交密鑰;有問題的操作員從氣流中的連接中獲得它。但是在測試中我直接使用了API,所以我也會嘗試。我也可以嘗試從主人的最新(我預計editor.swagger.io是最新的)。 – dlamblin

相關問題