1、第三方接口签名调用
import json
import requests
import hashlib
import time
import hmac
access_key = 'xxxxxxxxxxxxxxx'
secret_key = 'xxxxxxxxxxxxxxx'
def _wps4_sig ( method, url, date, body) : print ( body) if body is None : bodySha = "" else : bodySha = hashlib. sha256( body. encode( 'utf-8' ) ) . hexdigest( ) content = "xxx-4" + method + url + "application/json" + date + bodyShaprint ( content) signature = hmac. new( secret_key. encode( 'utf-8' ) , content. encode( 'utf-8' ) , hashlib. sha256) . hexdigest( ) return "xxx-4 %s:%s" % ( access_key, signature) def wps4_request ( method, host, uri, body= None , cookie= None , headers= None ) : requests. packages. urllib3. disable_warnings( ) if body is not None and not isinstance ( body, str ) : body = json. dumps( body) date = time. strftime( "%a, %d %b %Y %H:%M:%S GMT" , time. gmtime( ) ) header = { "Content-type" : "application/json" } header[ 'xxx-Docs-Date' ] = dateheader[ 'xxx-Docs-Authorization' ] = _wps4_sig( method, uri, date, body) if headers != None : for key, value in headers. items( ) : header[ key] = valueurl = "%s%s" % ( host, uri) r = requests. request( method, url, data= body, headers= header, cookies= cookie, verify= False ) return r. status_code, r. text
def edit ( file_id= 1 ) : url = '/api/edit/v1/files/{0}/link?type=w' . format ( file_id) result = wps4_request( 'GET' , 'http://xx.xx.xx.xxx:xxxx/open' , url) print ( result[ 0 ] ) print ( result[ 1 ] )
edit( )
2、调用结果