1688开放平台 签名算法 python、go、V

文章中涉及的数据并非真实数据,使用时请自行替换

阿里巴巴开放平台 - 签名工具

在这里插入图片描述

Python3实现

import hashlib
import hmac

app_key = '5648521'
app_secret = '2d5d5sg6'
api_info = 'param2/1/com.alibaba.fenxiao.crossborder/product.search.queryProductDetail/'
access_token = '9b4305e9-2c5a-4711-8399'
req_params = "{'offerId': '46369207','country': 'en'}"

sign_str = api_info+app_key+"access_token"+access_token+"offerDetailParam"+req_params
# sign_str = "param2/1/com.alibaba.fenxiao.crossborder/product.search.queryProductDetail/5648521access_token99b4305e9-2c5a-4711-8399offerDetailParam{'offerId':'46369207','country':'en'}"

code_sign = hmac.new(app_secret.encode(), sign_str.encode(), hashlib.sha1).hexdigest().upper()

print(f'Generated Signature: {code_sign}')

go实现

package main

import (
	"crypto/hmac"
	"crypto/sha1"
	"encoding/hex"
	"fmt"
	"strings"
)

func main() {
	// url := "http://gw.api.alibaba.com/openapi"
	app_key := "5648521"
	app_secret := "2d5d5sg6"
	api_info := "param2/1/com.alibaba.fenxiao.crossborder/product.search.queryProductDetail/" + app_key
	access_token := "9b4305e9-2c5a-4711-8399"
	// const req_params = "{'offerId': '46369207','country': 'en'}"

	signStr := api_info + "access_token" + access_token + "offerDetailParam{'offerId':'46369207','country':'en'}"
	// signStr = "param2/1/com.alibaba.fenxiao.crossborder/product.search.queryProductDetail/5648521access_token9b4305e9-2c5a-4711-8399offerDetailParam{'offerId':'46369207','country':'en'}"

	// Calculate HMAC-SHA1 signature
	hmacHash := hmac.New(sha1.New, []byte(app_secret))
	hmacHash.Write([]byte(signStr))
	codeSign := hex.EncodeToString(hmacHash.Sum(nil))

	fmt.Printf("Generated Signature: %s\n", strings.ToUpper(codeSign))
}

V实现(vlang)

module main

import crypto.hmac
import crypto.sha1
import encoding.hex

pub fn auth_sign_1688(app_key string, app_secret string, api_info string, access_token string, req_params map[string]string) string {

	sign_str := "${api_info}/${app_key}access_token${access_token}offerDetailParam${req_params}"
	println("sign_str:${sign_str}")
	// Calculate HMAC-SHA1 signature using external SHA-1 hashing function
	hmac_hash := hmac.new(app_secret.bytes(), sign_str.bytes(), sha1_hash, 64)
	code_sign := hex.encode(hmac_hash)

	auth_sigh := code_sign.to_upper()
	println('auth_sigh:${typeof(auth_sigh).name}')
	println('Generated Signature: ${code_sign.to_upper()}')
	return auth_sigh
}

// Define a SHA-1 hashing function
fn sha1_hash(data []u8) []u8 {
	mut hash := sha1.new()
	hash.write(data) or { panic(err) }
	return hash.sum([]u8{})
}


fn main(){
  app_key := '5648521'
  app_secret := '2d5d5sg6'
  api_info := 'param2/1/com.alibaba.fenxiao.crossborder/product.search.queryProductDetail'
  access_token := '9b4305e9-2c5a-4711-8399'

  // Configuration parameters (replace with actual API parameters)
  req_params := {'offerId':'46369207','country':'en'}

  auth := auth_sign_1688(app_key , app_secret , api_info, access_token, req_params)
  print(auth)
}

以上1688签名计算,经过生产环境验证。sign_str(签名因子) 的每个字符变化(包括空格) ,都会产生不同的签名结果

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值