- 首先是授权
接口文档说明了:授权需要四个参数:合作伙伴ID,回调地址,生成加密签名,时间戳。
有了这四个参数,我们要把拿到的返回参数code还有shop_id存进数据库,code很重要,是用来后面更新token用的。 - 如何生成sign
使用加密算法HmacSHA256计算
/// 加密算法HmacSHA256
/// </summary>
/// <param name="secret"></param>
/// <param name="signKey"></param>
/// <returns></returns>
public static string HmacSHA256(string secret, string signKey)
{
string signRet = string.Empty;
using (HMACSHA256 mac = new HMACSHA256(Encoding.UTF8.GetBytes(signKey)))
{
byte[] hash = mac.ComputeHash(Encoding.UTF8.GetBytes(secret));
signRet = ToHexString(hash);
}
return signRet;
}
/// <summary>
/// byte[]转16进制格式string
/// </summary>
public static string ToHexString(byte[] bytes)
{
string hexString = string.Empty;
if (bytes != null)
{
StringBuilder strB = new StringBuilder();
foreach (byte b in bytes)
{
strB.AppendFormat("{0:x2}", b);
}
hexString = strB.ToString();
}
return hexString;
}
返回参数会在回调地址上,拿到返回参数后,用来生成token
3.首次生成token
/// <summary>
/// 首次获取Token
/// </summary>
/// <param name="code"></param>
/// <param name="partner_id"></param>
/// <param name="partner_key"></param>
/// <param name="shop_id"></param>
/// <returns></returns>
public ReturnMeg get_token(string code, int partner_id, string partner_key, int shop_id)
{
long timest = Gettimest();
var bodto = new
{
code = code,
partner_id = partner_id,
shop_id = shop_id,
};
string body = JsonConvert.SerializeObject(bodto);
var host = System.Configuration.ConfigurationManager.AppSettings["ShpoeeUrl"];
var path2 = "/api/v2/auth/token/get";
var base_string = $"{partner_id}{path2}{timest}";
var sign = HmacSHA256(base_string, partner_key);
var u