1.唤起授权页
byte[] byteData = Encoding.UTF8.GetBytes(你可以自定义参数,回调时会返回);
var base64 = Convert.ToBase64String(byteData);
string Url = "https://services.tiktokshop.com/open/authorize?service_id=" + AppSettingsHelper.TTID + "&state=" + base64;
2.回调
public IActionResult TTAuthCallBackAsync(string code, string state)
{
try
{
byte[] byteData = Convert.FromBase64String(state);
// 设置请求的URL
string url = "https://auth.tiktok-shops.com/api/v2/token/get?app_key=" + AppSettingsHelper.TTAppKey + "&app_secret=" + AppSettingsHelper.TTSecretAey + "&auth_code=" + code + "&grant_type=authorized_code";
GetPostHelper getPostHelper = new GetPostHelper();
string res = getPostHelper.GetRequest(url);
if (res != "")
{
CommonHelper comm = new CommonHelper();
TTAuthCallBackInfo info = JsonConvert.DeserializeObject<TTAuthCallBackInfo>(res);
if (info.code == 0) //成功
{
long timeStamp = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
//获取已授权店铺信息
GetPostHelper com = new GetPostHelper();
var host = "https://open-api.tiktokglobalshop.com";
var path = "/authorization/202309/shops";
var base_string = $"{AppSettingsHelper.TTSecretAey}{path}app_key{AppSettingsHelper.TTAppKey}timestamp{timeStamp}{AppSettingsHelper.TTSecretAey}";
加密一点要按照顺序,一定一定要顺序对
var sign = comm.SHA256(base_string, AppSettingsHelper.TTSecretAey);
string getUrl = $"{host}{path}?app_key={AppSettingsHelper.TTAppKey}&sign={sign}×tamp={timeStamp}";
string bodydate = comm.GetTikTok(info.data.access_token, getUrl);
if (bodydate=="")
{
失败啦
}
TikTokAuthOpenIDInfo openIDInfo = JsonConvert.DeserializeObject<TikTokAuthOpenIDInfo>(bodydate);
//根据opneid 判断是否已存在这个店铺
string openID = "";
string cipher = "";
foreach (ShopsItem item in openIDInfo.data.shops)
{
openID = item.id;
cipher=item.cipher;
}
if (openID == "") {
失败啦
}
自己逻辑代码
}
else
{
失败啦
}
}
else
{
失败啦
}
}
catch (Exception ex)
{
}
}
删除逻辑的时候 大括号可能对应不上,你弄一下
3.加密方法
public string SHA256(string message, string key)
{
if (string.IsNullOrEmpty(key))
{
return "";
}
byte[] keyBytes = Encoding.UTF8.GetBytes(key); // 正确编码密钥为字节数组
byte[] messageBytes = Encoding.UTF8.GetBytes(message); // 编码消息为字节数组
using (HMACSHA256 hmac = new HMACSHA256(keyBytes)) // 使用有效的密钥创建HMAC实例
{
byte[] hashMessage = hmac.ComputeHash(messageBytes); // 计算哈希值
string hashString = BitConverter.ToString(hashMessage).Replace("-", "").ToLower(); // 转换为十六进制字符串并格式化输出
return hashString;
}
}
4.Get请求(一定要传Token)
public string GetTikTok(string token, string url)
{
// 创建HttpWebRequest对象
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET"; // 设置请求方法为GET
// 添加自定义的Headers
request.Headers.Add("x-tts-access-token", token);
request.Headers.Add("content-type", "application/json");
try
{
// 获取响应
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Console.WriteLine("Response Status: " + response.StatusDescription);
// 读取响应内容
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
string responseContent = reader.ReadToEnd();
return responseContent;
}
}
catch (WebException ex)
{
_logger.Error("Get请求异常:" + ex.Message);
return "";
}
}
下面是获取订单详情代码,浏览一下得了
一定一定一定要根据官方文档的必填字段首字母排序,除了前后的秘钥
一定一定一定要根据官方文档的必填字段首字母排序,除了前后的秘钥
一定一定一定要根据官方文档的必填字段首字母排序,除了前后的秘钥
long timeStamp = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
//获取已授权店铺信息
GetPostHelper com = new GetPostHelper();
var host = "https://open-api.tiktokglobalshop.com";
var path = "/order/202309/orders";
var base_string = $"{AppSettingsHelper.TTSecretAey}{path}app_key{AppSettingsHelper.TTAppKey}ids{要查询的订单号}shop_cipher{授权时时获取店铺信息返回的}timestamp{timeStamp}{AppSettingsHelper.TTSecretAey}";
var sign = comm.SHA256(base_string, AppSettingsHelper.TTSecretAey);
string getUrl = $"{host}{path}?app_key={AppSettingsHelper.TTAppKey}&shop_cipher={shopInfo.ShopCipher}&sign={sign}×tamp={timeStamp}&ids={info.OrderID}";
string bodydate = comm.GetTikTok(shopInfo.access_token, getUrl);