虾皮(shopee)是一个亚洲区域的电商平台,主要在东南亚地区提供电商服务。虾皮提供了丰富的电商数据,包括商品数据、订单数据、会员数据、评价数据等。
虾皮Shopee♥♥♥♥♥♥
1.授权
接口文档说明了:授权需要四个参数:合作伙伴ID,回调地址,生成加密签名,时间戳。
有了这四个参数,我们要把拿到的返回参数code还有shop_id存进数据库,code很重要,是用来后面更新token用的。
2.如何生成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 url = $"{host}{path2}?sign={sign}&partner_id={partner_id}×tamp={timest}&|";
string sd = Tool.HttpPostJSONs(url, body);
JObject tokendata = (JObject)JsonConvert.DeserializeObject(sd);
if (tokendata["error"].ToString() != "")
{
return new ReturnMeg
{
State = ReturnState.error,
Msg = "授权失败"
};
}
Shopee s = new Shopee();
s.Access_token = tokendata["access_token"].ToString();
s.Refresh_token = tokendata["refresh_token"].ToString();
int expire_in = MsParse.ToInt32(tokendata["expire_in"].ToString());//有效时间
s.GetTokenTime = DateTime.Now.AddSeconds((expire_in - 600));
var path = "/api/v2/shop/get_shop_info";
var redirect_url = setup.PcUserUrl + "/SQ/Index";
var base_strings = $"{partner_id}{path}{timest}{s.Access_token}{shop_id}";
var signs = HmacSHA256(base_strings, partner_key);
string strData = $"{partner_key}{redirect_url}";
string aaa = ($"{host}{path}?shop_id={shop_id}&partner_id={partner_id}&access_token={s.Access_token}&sign={signs}×tamp={timest}");
string back = Tool.HttpGet(aaa);
if (back == "")
{
return new ReturnMeg
{
State = ReturnState.error,
Msg = "获取店铺信息失败"
};
}
JObject joP = (JObject)JsonConvert.DeserializeObject(back);
s.ShopName = joP["shop_name"].ToString();
s.region = joP["region"].ToString();
s.ShopState = joP["status"].ToString();
s.auth_time = StampToDateTime(joP["auth_time"].ToString());
s.expire_time = StampToDateTime(joP["expire_time"].ToString());
s.Creationtime = DateTime.Now;
s.shop_id = shop_id;
s.code = code;
Shopee s1 = shopeeIBLL.GetShopee(shop_id);
s.Id = s1.Id;
if (s1.Id > 0)
{
shopeeIBLL.UpDate(s);
}
else
{
shopeeIBLL.Add(s);
}
return new ReturnMeg
{
State = ReturnState.success,
Msg = "授权成功"
};
}
/// <summary>
/// 用店铺Id刷新Token
/// </summary>
/// <param name="shop_Id"></param>
/// <returns></returns>
public void SxToken(int shop_Id)
{
Shopee s = shopeeIBLL.GetShopee(shop_Id);
//时间不到,不用刷新
if (s.GetTokenTime >= DateTime.Now)
{
return;
}
int partner_id = MsParse.ToInt32(System.Configuration.ConfigurationManager.AppSettings["Shpoeepartner_id"]);
var partner_key = System.Configuration.ConfigurationManager.AppSettings["Shpoeepartner_key"];
long timest = Gettimest();
var bodto = new
{
refresh_token = s.Refresh_token,
partner_id = partner_id,
shop_id = s.shop_id,
code = s.code,
};
string body = JsonConvert.SerializeObject(bodto);
var host = System.Configuration.ConfigurationManager.AppSettings["ShpoeeUrl"];
var path2 = "/api/v2/auth/access_token/get";
var base_string = $"{partner_id}{path2}{timest}";
var sign = HmacSHA256(base_string, partner_key);
var url = $"{host}{path2}?sign={sign}&partner_id={partner_id}×tamp={timest}&|";
string sd = Tool.HttpPostJSONs(url, body);
JObject tokendata = (JObject)JsonConvert.DeserializeObject(sd);
s.Access_token = tokendata["access_token"].ToString();
s.Refresh_token = tokendata["refresh_token"].ToString();
int expire_in = MsParse.ToInt32(tokendata["expire_in"].ToString());//有效时间
s.GetTokenTime = DateTime.Now.AddSeconds((expire_in - 600));
shopeeIBLL.UpDate(s);
}
获取到返回参数后,存入数据库。
4.刷新token
拿到首次获取token返回参数refresh_token,调用刷新token接口。
注意(之后的授权,或是对接虾皮其他接口都要先判断token是否过期,过期了就要调用刷新接口)