Date
or X-Date
and Authorization
. More optional headers can be added in the request. If Date
is used, the server will not check the time; if X-Date
is used, the server will check the time.Date
header is the construction time of the HTTP request in GMT format, such as Fri, 09 Oct 2015 00:00:00 GMT.X-Date
header is the construction time of the HTTP request in GMT format, such as Mon, 19 Mar 2018 12:08:40 GMT. It cannot deviate from the current time for more than 15 minutes.X-NameSpace-Code
and X-MicroService-Name
. They are not needed for general APIs and are included in the demo by default.# -*- coding: utf-8 -*-import requestsimport datetimeimport hashlibfrom hashlib import sha1import hmacimport base64GMT_FORMAT = '%a, %d %b %Y %H:%M:%S GMT'def getSimpleSign(source, SecretId, SecretKey) :dateTime = datetime.datetime.utcnow().strftime(GMT_FORMAT)auth = "hmac id=\\"" + SecretId + "\\", algorithm=\\"hmac-sha1\\", headers=\\"date source\\", signature=\\""signStr = "date: " + dateTime + "\\n" + "source: " + sourcesign = hmac.new(SecretKey, signStr, hashlib.sha1).digest()sign = base64.b64encode(sign)sign = auth + sign + "\\""return sign, dateTimeSecretId = 'your SecretId' # `SecretId` in key pairSecretKey = 'your SecretKey' # `SecretKey` in key pairurl = 'http://service-xxxxxx-1234567890.ap-guangzhou.apigateway.myqcloud.com/release/xxx' # API access path#header = {}header = { 'Host':'service-xxxxxx-1234567890.ap-guangzhou.apigateway.myqcloud.com', # Service domain name of API'Accept': 'text/html, */*; q=0.01','X-Requested-With': 'XMLHttpRequest','User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.89 Safari/537.36','Accept-Encoding': 'gzip, deflate, sdch','Accept-Language': 'zh-CN,zh;q=0.8,ja;q=0.6'}Source = 'xxxxxx' # Arbitrary signature watermark valuesign, dateTime = getSimpleSign(Source, SecretId, SecretKey)header['Authorization'] = signheader['Date'] = dateTimeheader['Source'] = Source# If it is a microservice API, you need to add two fields in the header: 'X-NameSpace-Code' and 'X-MicroService-Name'. They are not needed for general APIs.header['X-NameSpace-Code'] = 'testmic'header['X-MicroService-Name'] = 'provider-demo'print headerr = requests.get(url, headers=header)print rprint r.text
# -*- coding: utf-8 -*-import base64import datetimeimport hashlibimport hmacimport requestsGMT_FORMAT = '%a, %d %b %Y %H:%M:%S GMT'def getSimpleSign(source, SecretId, SecretKey):dateTime = datetime.datetime.utcnow().strftime(GMT_FORMAT)auth = "hmac id=\\"" + SecretId + "\\", algorithm=\\"hmac-sha1\\", headers=\\"date source\\", signature=\\""signStr = "date: " + dateTime + "\\n" + "source: " + sourcesign = hmac.new(SecretKey.encode(), signStr.encode(), hashlib.sha1).digest()sign = base64.b64encode(sign).decode()sign = auth + sign + "\\""return sign, dateTimeSecretId = 'your SecretId' # SecretId in key pairSecretKey = 'your SecretKey' # SecretKey in key pairurl = 'http://service-xxxxxx-1234567890.ap-guangzhou.apigateway.myqcloud.com/release/xxx' # API access pathheader = {'Host': 'service-xxxxxx-1234567890.ap-guangzhou.apigateway.myqcloud.com', # Service domain name of API'Accept': 'text/html, */*; q=0.01','X-Requested-With': 'XMLHttpRequest','User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.89 Safari/537.36','Accept-Encoding': 'gzip, deflate, sdch','Accept-Language': 'zh-CN,zh;q=0.8,ja;q=0.6'}Source = 'xxxxxx' # Arbitrary signature watermark valuesign, dateTime = getSimpleSign(Source, SecretId, SecretKey)header['Authorization'] = signheader['Date'] = dateTimeheader['Source'] = Sourcer = requests.get(url, headers=header)print(r.text)
Was this page helpful?