全部产品
阿里云办公

官方服务API校验规范

更新时间:2018-04-16 09:45:07

您可以通过调用API来实现对您在数加订购的官方服务的调用。

您可以通过发起HTTPS请求来调用API,首先您需要创建AccessKey

注意

AccessKey相当于您访问阿里云产品的口令,拥有您完整的权限,请妥善保管、避免泄露,并定期更换您的AccessKey。

API整体的校验规则

API签名包含公共请求头(HTTP协议header参数)、URL(规范资源)、Body

  1. Authorization = Dataplus AccessKeyId + ":" + Signature
  2. Signature = Base64( HMAC-SHA1( AccessSecret, UTF-8-Encoding-Of(StringToSign) ) )
  3. StringToSign =
  4. //HTTP协议header
  5. HTTP-Verb + "\n" + //GET|POST|PUT...
  6. Accept + "\n" +
  7. Content-MD5 + "\n" + //Body的MD5值放在此处
  8. Content-Type + "\n" +
  9. Date + "\n" +
  10. url

签名计算方法

API请求使用标准的Authorization头来签名自己的请求,请求格式如下所示:

  1. Authorization: Dataplus AccessKeyId:Signature

签名算法遵循RFC 2104HMAC-SHA1规范,要签名的元素是请求自身的一些参数,由于每个API请求基本不同,所以签名的结果也不相同。

公共请求头计算签名

HTTP协议header

计算签名必须包含参数Accept、Content-Type、Date的值(Content-Length不计入签名),按顺序排列,若值不存在则以\n补齐。

名称 含义 类型 默认值
Authorization 用于验证请求合法性的认证信息,采用AccessKeyId:Signature的形式。 字符串 默认值:无
Content-Type RFC 2616中定义的HTTP请求内容类型。 类型:字符串 默认值:无
Date HTTP 1.1协议中规定的GMT时间,例如:Wed, 05 Sep. 2012 23:00:00 GMT 字符串
Accept 客户端需要的返回值类型。 类型:字符串

URL

完整的URL,比如http://127.0.0.1:808/xxx/yyy?a=1&c=2。

  1. URL: '/xxx/yyy?a=1&c=2'

Case

Node.js

  1. var request = require('request');
  2. var url = require('url');
  3. var crypto = require('crypto');
  4. var date = new Date().toUTCString()
  5. // 这里填写AK和请求
  6. var ak_id = 'NNV..........5jv';
  7. var ak_secret = 'FGs.....................3Zu';
  8. var options = {
  9. url : 'https://shujuapi.aliyun.com/org_code/service_code/api_name?param1=xxx&param2=xxx',
  10. method: 'GET',
  11. body: '',
  12. headers: {
  13. 'accept': 'application/json',
  14. 'content-type': 'application/json',
  15. 'date': date,
  16. 'Authorization': ''
  17. }
  18. };
  19. // 这里填写AK和请求
  20. md5 = function(buffer) {
  21. var hash;
  22. hash = crypto.createHash('md5');
  23. hash.update(buffer);
  24. return hash.digest('base64');
  25. };
  26. sha1 = function(stringToSign, secret) {
  27. var signature;
  28. return signature = crypto.createHmac('sha1', secret).update(stringToSign).digest().toString('base64');
  29. };
  30. // step1: 组stringToSign [StringToSign = #{method}\\n#{accept}\\n#{data}\\n#{contentType}\\n#{date}\\n#{action}]
  31. var body = options.body || '';
  32. var bodymd5;
  33. if(body === void 0 || body === ''){
  34. bodymd5 = body;
  35. } else {
  36. bodymd5 = md5(new Buffer(body));
  37. }
  38. console.log(bodymd5)
  39. var stringToSign = options.method + "\n" + options.headers.accept + "\n" + bodymd5 + "\n" + options.headers['content-type'] + "\n" + options.headers.date + "\n" + url.parse(options.url).path;
  40. console.log("step1-Sign string:", stringToSign);
  41. // step2: 加密 [Signature = Base64( HMAC-SHA1( AccessSecret, UTF-8-Encoding-Of(StringToSign) ) )]
  42. var signature = sha1(stringToSign, ak_secret);
  43. // console.log("step2-signature:", signature);
  44. // step3: 组authorization header [Authorization = Dataplus AccessKeyId + ":" + Signature]
  45. var authHeader = "Dataplus " + ak_id + ":" + signature;
  46. console.log("step3-authorization Header:", authHeader);
  47. options.headers.Authorization = authHeader;
  48. console.log('authHeader', authHeader);
  49. // step4: send request
  50. function callback(error, response, body) {
  51. if (error) {
  52. console.log("error", error)
  53. }
  54. console.log("step4-response body:", response.statusCode, body)
  55. }
  56. request(options, callback);

Java

  1. package com.alibaba.vo;
  2. import java.net.HttpURLConnection;
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.io.PrintWriter;
  7. import java.net.URL;
  8. import java.net.URLConnection;
  9. import java.security.MessageDigest;
  10. import java.text.SimpleDateFormat;
  11. import java.util.Date;
  12. import java.util.Locale;
  13. import javax.crypto.spec.SecretKeySpec;
  14. import sun.misc.BASE64Encoder;
  15. import javax.crypto.Mac;
  16. @SuppressWarnings("restriction")
  17. public class AESDecode {
  18. /*
  19. * 计算MD5+BASE64
  20. */
  21. public static String MD5Base64(String s) {
  22. if (s == null)
  23. return null;
  24. String encodeStr = "";
  25. byte[] utfBytes = s.getBytes();
  26. MessageDigest mdTemp;
  27. try {
  28. mdTemp = MessageDigest.getInstance("MD5");
  29. mdTemp.update(utfBytes);
  30. byte[] md5Bytes = mdTemp.digest();
  31. BASE64Encoder b64Encoder = new BASE64Encoder();
  32. encodeStr = b64Encoder.encode(md5Bytes);
  33. } catch (Exception e) {
  34. throw new Error("Failed to generate MD5 : " + e.getMessage());
  35. }
  36. return encodeStr;
  37. }
  38. /*
  39. * 计算 HMAC-SHA1
  40. */
  41. public static String HMACSha1(String data, String key) {
  42. String result;
  43. try {
  44. SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), "HmacSHA1");
  45. Mac mac = Mac.getInstance("HmacSHA1");
  46. mac.init(signingKey);
  47. byte[] rawHmac = mac.doFinal(data.getBytes());
  48. result = (new BASE64Encoder()).encode(rawHmac);
  49. } catch (Exception e) {
  50. throw new Error("Failed to generate HMAC : " + e.getMessage());
  51. }
  52. return result;
  53. }
  54. /*
  55. * 等同于javaScript中的 new Date().toUTCString();
  56. */
  57. public static String toGMTString(Date date) {
  58. SimpleDateFormat df = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z", Locale.UK);
  59. df.setTimeZone(new java.util.SimpleTimeZone(0, "GMT"));
  60. return df.format(date);
  61. }
  62. /*
  63. * 发送POST请求
  64. */
  65. public static String sendPost(String url, String body, String ak_id, String ak_secret) throws Exception {
  66. PrintWriter out = null;
  67. BufferedReader in = null;
  68. String result = "";
  69. int statusCode = 200;
  70. try {
  71. URL realUrl = new URL(url);
  72. /*
  73. * http header 参数
  74. */
  75. String method = "POST";
  76. String accept = "application/json";
  77. String content_type = "application/json";
  78. String path = realUrl.getFile();
  79. String date = toGMTString(new Date());
  80. // 1.对body做MD5+BASE64加密
  81. String bodyMd5 = MD5Base64(body);
  82. String stringToSign = method + "\n" + accept + "\n" + bodyMd5 + "\n" + content_type + "\n" + date + "\n"
  83. + path;
  84. // 2.计算 HMAC-SHA1
  85. String signature = HMACSha1(stringToSign, ak_secret);
  86. // 3.得到 authorization header
  87. String authHeader = "Dataplus " + ak_id + ":" + signature;
  88. // 打开和URL之间的连接
  89. URLConnection conn = realUrl.openConnection();
  90. // 设置通用的请求属性
  91. conn.setRequestProperty("accept", accept);
  92. conn.setRequestProperty("content-type", content_type);
  93. conn.setRequestProperty("date", date);
  94. conn.setRequestProperty("Authorization", authHeader);
  95. // 发送POST请求必须设置如下两行
  96. conn.setDoOutput(true);
  97. conn.setDoInput(true);
  98. // 获取URLConnection对象对应的输出流
  99. out = new PrintWriter(conn.getOutputStream());
  100. // 发送请求参数
  101. out.print(body);
  102. // flush输出流的缓冲
  103. out.flush();
  104. // 定义BufferedReader输入流来读取URL的响应
  105. statusCode = ((HttpURLConnection)conn).getResponseCode();
  106. if(statusCode != 200) {
  107. in = new BufferedReader(new InputStreamReader(((HttpURLConnection)conn).getErrorStream()));
  108. } else {
  109. in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  110. }
  111. String line;
  112. while ((line = in.readLine()) != null) {
  113. result += line;
  114. }
  115. } catch (Exception e) {
  116. e.printStackTrace();
  117. } finally {
  118. try {
  119. if (out != null) {
  120. out.close();
  121. }
  122. if (in != null) {
  123. in.close();
  124. }
  125. } catch (IOException ex) {
  126. ex.printStackTrace();
  127. }
  128. }
  129. if (statusCode != 200) {
  130. throw new IOException("\nHttp StatusCode: "+ statusCode + "\nErrorMessage: " + result);
  131. }
  132. return result;
  133. }
  134. /*
  135. * GET请求
  136. */
  137. public static String sendGet(String url, String ak_id, String ak_secret) throws Exception {
  138. String result = "";
  139. BufferedReader in = null;
  140. int statusCode = 200;
  141. try {
  142. URL realUrl = new URL(url);
  143. /*
  144. * http header 参数
  145. */
  146. String method = "GET";
  147. String accept = "application/json";
  148. String content_type = "application/json";
  149. String path = realUrl.getFile();
  150. String date = toGMTString(new Date());
  151. // 1.对body做MD5+BASE64加密
  152. // String bodyMd5 = MD5Base64(body);
  153. String stringToSign = method + "\n" + accept + "\n" + "" + "\n" + content_type + "\n" + date + "\n" + path;
  154. // 2.计算 HMAC-SHA1
  155. String signature = HMACSha1(stringToSign, ak_secret);
  156. // 3.得到 authorization header
  157. String authHeader = "Dataplus " + ak_id + ":" + signature;
  158. // 打开和URL之间的连接
  159. URLConnection connection = realUrl.openConnection();
  160. // 设置通用的请求属性
  161. connection.setRequestProperty("accept", accept);
  162. connection.setRequestProperty("content-type", content_type);
  163. connection.setRequestProperty("date", date);
  164. connection.setRequestProperty("Authorization", authHeader);
  165. connection.setRequestProperty("Connection", "keep-alive");
  166. // 建立实际的连接
  167. connection.connect();
  168. // 定义 BufferedReader输入流来读取URL的响应
  169. statusCode = ((HttpURLConnection)connection).getResponseCode();
  170. if(statusCode != 200) {
  171. in = new BufferedReader(new InputStreamReader(((HttpURLConnection)connection).getErrorStream()));
  172. } else {
  173. in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  174. }
  175. String line;
  176. while ((line = in.readLine()) != null) {
  177. result += line;
  178. }
  179. } catch (Exception e) {
  180. e.printStackTrace();
  181. } finally {
  182. try {
  183. if (in != null) {
  184. in.close();
  185. }
  186. } catch (Exception e) {
  187. e.printStackTrace();
  188. }
  189. }
  190. if (statusCode != 200) {
  191. throw new IOException("\nHttp StatusCode: "+ statusCode + "\nErrorMessage: " + result);
  192. }
  193. return result;
  194. }
  195. public static void main(String[] args) throws Exception {
  196. // 发送POST请求示例
  197. String ak_id1 = "NMV.............5jv"; //用户ak
  198. String ak_secret1 = "Fgs...............3zu"; // 用户ak_secret
  199. String url = "https://shujuapi.aliyun.com/org_code/service_code/api_name";
  200. String body = "{\"param1\": \"xxx\", \"param2\":\"xxx\"}";
  201. System.out.println("response body:" + sendPost(url, body, ak_id, ak_secret));
  202. // 发送GET请求
  203. String ak_id1 = "NMV.............5jv"; //用户ak
  204. String ak_secret1 = "Fgs...............3zu"; // 用户ak_secret
  205. String url1 = "https://shujuapi.aliyun.com/org_code/service_code/api_name?param1=xxx&param2=xxx";
  206. System.out.println("response body:" + sendGet(url1, ak_id1, ak_secret1));
  207. }
  208. }

PHP

  1. <?php
  2. $akId = "*************";
  3. $akSecret = "******************";
  4. //更新api信息
  5. $url = "https://shujuapi.aliyun.com/org_code/service_code/api_name?param1=xxx&param2=xxx";
  6. $options = array(
  7. 'http' => array(
  8. 'header' => array(
  9. 'accept'=> "application/json",
  10. 'content-type'=> "application/json",
  11. 'date'=> gmdate("D, d M Y H:i:s \G\M\T"),
  12. 'authorization' => ''
  13. ),
  14. 'method' => "GET", //可以是 GET, POST, DELETE, PUT
  15. 'content' => '' //如有数据,请用json_encode()进行编码
  16. )
  17. );
  18. $http = $options['http'];
  19. $header = $http['header'];
  20. $urlObj = parse_url($url);
  21. if(empty($urlObj["query"]))
  22. $path = $urlObj["path"];
  23. else
  24. $path = $urlObj["path"]."?".$urlObj["query"];
  25. $body = $http['content'];
  26. if(empty($body))
  27. $bodymd5 = $body;
  28. else
  29. $bodymd5 = base64_encode(md5($body,true));
  30. $stringToSign = $http['method']."\n".$header['accept']."\n".$bodymd5."\n".$header['content-type']."\n".$header['date']."\n".$path;
  31. $signature = base64_encode(
  32. hash_hmac(
  33. "sha1",
  34. $stringToSign,
  35. $akSecret, true));
  36. $authHeader = "Dataplus "."$akId".":"."$signature";
  37. $options['http']['header']['authorization'] = $authHeader;
  38. $options['http']['header'] = implode(
  39. array_map(
  40. function($key, $val){
  41. return $key.":".$val."\r\n";
  42. },
  43. array_keys($options['http']['header']),
  44. $options['http']['header']));
  45. $context = stream_context_create($options);
  46. $file = file_get_contents($url, false, $context );
  47. echo($file);
  48. ?>

Python 2.7

  1. #!/usr/bin/python
  2. # -*- coding:utf-8 -*-
  3. from urlparse import urlparse
  4. import datetime
  5. import base64
  6. import hmac
  7. import hashlib
  8. import json
  9. import urllib2
  10. def get_current_date():
  11. date = datetime.datetime.strftime(datetime.datetime.utcnow(), "%a, %d %b %Y %H:%M:%S GMT")
  12. return date
  13. def to_md5_base64(strBody):
  14. hash = hashlib.md5()
  15. hash.update(body)
  16. return hash.digest().encode('base64').strip()
  17. def to_sha1_base64(stringToSign, secret):
  18. hmacsha1 = hmac.new(secret, stringToSign, hashlib.sha1)
  19. return base64.b64encode(hmacsha1.digest())
  20. ak_id = '<用户的AK_ID>'
  21. ak_secret = '<用户的AK_SECRET>'
  22. options = {
  23. 'url': '<请求的url>',
  24. 'method': 'POST',
  25. 'body': json.dumps({"name": "hello"}, separators=(',', ':')),
  26. 'headers': {
  27. 'accept': 'application/json',
  28. 'content-type': 'application/json',
  29. 'date': get_current_date(),
  30. 'authorization': ''
  31. }
  32. }
  33. # options = {
  34. # 'url': '<请求的url>',
  35. # 'method': 'GET',
  36. # 'headers': {
  37. # 'accept': 'application/json',
  38. # 'content-type': 'application/json',
  39. # 'date': get_current_date(), # 'Sat, 07 May 2016 08:19:52 GMT', # get_current_date(),
  40. # 'authorization': ''
  41. # }
  42. # }
  43. body = ''
  44. if 'body' in options:
  45. body = options['body']
  46. print body
  47. bodymd5 = ''
  48. if not body == '':
  49. bodymd5 = to_md5_base64(body)
  50. print bodymd5
  51. urlPath = urlparse(options['url'])
  52. if urlPath.query != '':
  53. urlPath = urlPath.path + "?" + urlPath.query
  54. else:
  55. urlPath = urlPath.path
  56. stringToSign = options['method'] + '\n' + options['headers']['accept'] + '\n' + bodymd5 + '\n' + options['headers']['content-type'] + '\n' + options['headers']['date'] + '\n' + urlPath
  57. signature = to_sha1_base64(stringToSign, ak_secret)
  58. print stringToSign
  59. authHeader = 'Dataplus ' + ak_id + ':' + signature
  60. options['headers']['authorization'] = authHeader
  61. print authHeader
  62. request = None
  63. method = options['method']
  64. url = options['url']
  65. print method
  66. print url
  67. if 'GET' == method or 'DELETE' == method:
  68. request = urllib2.Request(url)
  69. elif 'POST' == method or 'PUT' == method:
  70. request = urllib2.Request(url, body)
  71. request.get_method = lambda: method
  72. for key, value in options['headers'].items():
  73. request.add_header(key, value)
  74. try:
  75. conn = urllib2.urlopen(request)
  76. response = conn.read()
  77. print response
  78. except urllib2.HTTPError, e:
  79. print e.read()
  80. raise SystemExit(e)
本文导读目录