?
一、如何注冊
- 為什么要注冊?
訪問 OpenAPI必須擁有Consumer Key和Consumer Secret。
- 如何注冊?
要獲取Consumer Key及Consumer Secret,需要消費方(Consumer)向服務提供方申請注冊,服務提供方審核通過后會向消費方分配Consumer Key和Consumer Secret
- 注冊時需提供什么信息?
消費方注冊時需要向服務提供方提供以下信息:
條目 | 說明 |
---|---|
消費方名稱 | 第三方名稱 |
消費方電子郵箱 | API變化時方便通知 |
消費方OAuth請求URL地址 | 消費方進行OAuth驗證,獲取令牌及密鑰的請求URL地址(不包含參數部分) 如:http://youwebsite/request_token |
消費方OAuth回調URL地址 | 消費方OAuth驗證通過后的回調URL地址(不包含參數部分) 如:http://youwebsite/request_token_ready |
?
?
二、如何簽名
對 OpenAPI 進行REST請求時有如下幾種參數:
API方法 | api_method |
不需簽名參數 | UnSignArguments |
需簽名參數 | SignArguments |
簽名 | api_signature |
注意:各參數值需要進行url encode編碼(尤其中文時)
調用API時需要對第3種參數進行簽名,簽名步驟如下:
No. | 說明 |
---|---|
1 | 所有需簽參數名稱必須小寫 |
2 | 所有參數值要進行url encode編碼(尤其中文時),把編碼結果轉小寫 |
3 | 簽名參數按字母升序排序 |
4 | 合成簽名字符串,合成時把“簽名密鑰”放到最前面,然后把已排序的需簽名參數清單按照“參數名稱+參數值”的方式追加到字符串 |
5 | 對合成的簽名字符串進行 UTF8 編碼 |
6 | 用MD5對簽名字符串進行加密生成32位簽名 |
第4步合成簽名串規則如下:
簽名密鑰 | 參數名稱 | 參數值 | 參數名稱 | 參數值 | ...... |
關于簽名密鑰的特殊說明:
調用API | 使用的簽名密鑰 |
---|---|
OpenAPI.OAuth.RequestToken | oauth_consumer_secret |
Others | oauth_token_secret |
?
UTF8編碼及MD5加密的C#參考實現代碼如下:
using System.Security; using System.Security.Cryptography; using System.Text; |
?
//MD5加密 public static string GetMd5String(string str) { // First we need to convert the string into bytes, which // means using a text encoder. byte[] unicodeText = System.Text.Encoding.UTF8.GetBytes(str); // Now that we have a byte array we can ask the CSP to hash it MD5 md5 = new MD5CryptoServiceProvider(); byte[] result = md5.ComputeHash(unicodeText); // Build the final string by converting each byte // into hex and appending it to a StringBuilder StringBuilder sb = new StringBuilder(); for (int i = 0; i < result.Length; i++) { sb.Append(result[i].ToString("x2")); } // And return it return sb.ToString(); } |
?
三、請求REST格式
請求REST格式由以下幾部分組成:
- 請求REST地址,如:
http://youserverwebsite/api/rest |
- 調用API方法,如:
?api_method=OpenAPI.OAuth.RequestToken |
- 不需簽名參數,如:
&oauth_consumer_key=************* |
- 需簽名參數,如:
&oauth_timestamp=****&oauth_nonce=****&oauth_version=**** |
- 簽名,如:
&api_signature=**** |
?
完整請求REST串:
http://youserverwebsite/api/rest? api_method=OpenAPI.oauth.requesttoken &oauth_consumer_key=**** &oauth_nonce=a65c5e5e903942a994bcb07250431e2b &oauth_timestamp=1277216551 &oauth_version=1.0 &api_signature=6431e9d5b8508a2177920e8c3c624b1b |
?
四、請求REST返回格式?
請求REST返回格式為xml格式或者json格式,目前僅支持xml格式,示例如下:
<rsp method="openapi.oauth.authorizetoken" flag="True" code="" desc="ok"> ? <data>??? <token>4941c6de38650e58e75a1536d847c2b6</token> ??? <token_secret>c48bcf5ba5166a5b5c409e233811025b</token_secret>? </data></rsp> |
返回結果格式說明:
參數名稱 | 備注說明 |
---|---|
method | 請求的API方法名稱 |
flag | 請求成功與否標識:true=成功;false=失敗; |
code | 結果狀態碼 |
desc | 結果說明 |
data | 結果數據 |
?
?
[返回導航]