大きな API ドキュメントがあり、各リクエストには や のような同じリクエスト ヘッダーがAccept: application/json
ありCookies: SessionID
ます。重複を避けるためにこれらをグローバルに宣言する方法はありますか?
1170 次
1 に答える
1
私がその件に関して申請してきた最も一般的なことは、データ構造を定義し、それをすべてのリクエストで使用することです。私の場合、すべて同じ認証ヘッダーに使用しました。
データ構造の例とその使用法 (実際に使用する場所、ヘッダーまたはリクエストの本文は関係ありません。この場合は本文にあります):
FORMAT: 1A
HOST: http://polls.apiblueprint.org/
# Auth API
This is an Auth API, where you can obtain authentication/authorization for your app in our system.
It is necessary that you provide your credentials in order to use the API. The endpoints of the Auth API are only to obtain a valid access token, which would be provided along with each call.
# Group Authentication
## Get a request token [/auth/request]
Obtain a request token so you can exchange it for an access token.
Requests tokens have a short expiry rate, and are only one time use.
### GET
+ Request (application/json)
+ Attributes
- Authorization (OAuth Request)
+ Response 200 (application/json)
{
"oauth_token" : "request-token-ng7805hg85hjt89gu258ty25",
"oauth_token_secret" : "TOKEN SECRET TO BE USED WHILE SIGNING REQUESTS"
}
## Exchange a request token for an access token [/auth/exchange]
Once you have got a request token, you will be able to trade it for an access token and then be able to call the different API endpoints.
### GET
+ Request (application/json)
+ Attributes
- Authorization (OAuth Exchange)
+ Response 200 (application/json)
{
"oauth_token" : "AN_INACTIVE_FRESH_ACCESS_TOKEN"
}
# Data Structures
## OAuth base (object)
+ oauth_consumer_key: YOUR_CONSUMER_KEY (string, required)
+ oauth_nonce: A_UNIQUE_TOKEN_FOR_THIS_REQUEST_GENERATED_BY_YOU (string, required)
+ oauth_signature: A_SIGNATURE_HASH_BASED_ON_THE_REQUEST_PARAMS (string, required)
+ oauth_signature_method: `HMAC-SHA256` (string, required)
+ oauth_timestamp: MILLISECONDS_FROM_EPOC_UNIX_TIME (string, required)
+ oauth_version: 1.0 (string, required)
## OAuth Request (object)
+ Include OAuth base
+ oauth_callback: YOUR_CALLBACK_URL (string, required)
## OAuth Exchange (object)
+ Include OAuth base
+ oauth_token: YOUR_RECENTLY_OBTAINED_REQUEST_TOKEN (string, required)
かっこの間にデータ構造の名前を追加するだけで済みます。好きなだけ再利用できます。必要に応じて、他のデータ構造を構築する場合でも、以前に定義されたデータ構造に基づいて他のデータ構造を作成したい場合があります。
于 2016-06-29T14:06:42.260 に答える