Files
KJD/Work/Api/PinduoduoAuth.cs
T
2025-06-29 19:38:31 +08:00

718 lines
39 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AllBase;
using Work.ManageClass;
using System.Net;
namespace Work.Api
{
public class PinduoduoAuth
{
public bool NoCache = false;
public bool MustAuth = false,MustAuthCps = false;
public bool IsCps = false, SignDuoProtocol = false;
public static List<string> Shops = new List<string>();
/// <summary>
/// 拼多多api网关
/// </summary>
public static string Gate = "https://gw-api.pinduoduo.com/api/router";
public static string strContent = "";
public static string ExecuteScript(string sExpression, string sCode)
{
MSScriptControl.ScriptControl scriptControl = new MSScriptControl.ScriptControl();
scriptControl.UseSafeSubset = true;
scriptControl.Language = "JScript";
scriptControl.AddCode(sCode);
try
{
string str = scriptControl.Eval(sExpression).ToString();
return str;
}
catch (Exception ex)
{
string str = ex.Message;
}
return null;
}
public static void UpdateMallId(ref ShopChatBase shop)
{
string url = BaseClass.BuildObjString("shop&act=update_mall_id");
string data = UserLogin.GetSignaturex(shop.ShopId, shop.ShopName);
data += "&mall_id=" + shop.MallId;
data += "&access_token=" + shop.AccessToken;
data += "&refresh_token=" + shop.RefreshToken;
data += "&from_tool=" + GetHtml.UrlEncodeUtf8(BaseClass.AgentName);
string html = GetHtml.PostUtf8(url, data);
url = BaseClass.LoginApiUrl + "?obj=product/image_pinbaobao&act=update_cookies";
data = UserLogin.GetSignaturex(shop.ShopId, shop.ShopName);
data += "&cookies=" + GetHtml.UrlEncodeUtf8(shop.Cookies);
html = GetHtml.PostUtf8(url, data);
}
public bool ReFresh(string refresh)
{
string url = BaseClass.LoginApiUrl + "?obj=upshopid/pdd_refreshx&act=refresh&refresh_token=" + refresh
+ "&client_id=" + BaseClass.ClientId + "&client_secret=" + BaseClass.ApiKey.Replace("2", "z").Replace("1", "I").Replace("c", "C").Replace("9", "O");
string html = GetHtml.Utf8(url);
if (!string.IsNullOrEmpty(html) && html.Contains("access_token"))
return true;
return false;
}
public bool Auth(ShopChatBase shop)
{
string url = BaseClass.LoginApiUrl + "?obj=upshopid/pdd_shop_tempx&act=get_token&shop_name=" + shop.ShopName + "&" + UserLogin.GetSignaturex();
string html = GetHtml.Utf8(url);
if (!string.IsNullOrEmpty(html) && html.Contains("access_token"))
{
shop.AccessToken = BaseRegex.GetStringJson(ref html, "access_token");
shop.RefreshToken = BaseRegex.GetStringJson(ref html, "refresh_token");
UpdateMallId(ref shop);
if (ReFresh(shop.RefreshToken))
{
return true;
}
}
if (string.IsNullOrEmpty(strContent))
{
string path = AppDomain.CurrentDomain.BaseDirectory + "crawlerInfoInterface.js";
strContent = FileBase.ReadAllText(path);
}
string toolCode = "";
if (BaseClass.MainPath == "findgoldcat")
toolCode = "eb-";
if (BaseClass.MainPath == "findgoldcat_main")
toolCode = "oms-";
if (BaseClass.MainPath == "pinbaobao")
toolCode = "pbb-";
string refresh = "";
try
{
string cookies = "";
string sid = BaseClass.ShopId;
cookies = shop.Cookies;
sid = shop.ShopId;
if (MustAuth)
goto AuthGo;
url = BaseClass.LoginApiUrl + "pddx.php?act=get&cid=" + BaseClass.CompanyId + "&sid=" + sid
+ "&client_id=" + BaseClass.ClientId + "&client_secret=" + BaseClass.ApiKey.Replace("2", "z").Replace("1", "I").Replace("c", "C").Replace("9", "O")
+ "&state=" + toolCode + sid + "-" + BaseClass.CompanyId;
html = GetHtml.Utf8(url);
string token = BaseRegex.GetStringJson(ref html, "access_token");
refresh = BaseRegex.GetStringJson(ref html, "refresh_token");
if (!string.IsNullOrEmpty(refresh))
{
bool ok = ReFresh(refresh);
if (ok)
{
goto End;
}
}
AuthGo:
string strTime = BaseClass.GetTime(DateTime.Now).ToString() + "000";
string str = "";
for (int i = 0; i < 16; i++)
{
str += new Random(Guid.NewGuid().GetHashCode()).Next(9);
}
string strToken = str + "-" + strTime;
string strNumber = "";
string fun2 = string.Format(@"getEncode('{0}','{1}','{2}')", strTime, strToken, strNumber);
string codex = ExecuteScript(fun2, strContent);
string refere = "http://mms.pinduoduo.com/open.html?response_type=code&client_id=" + BaseClass.ClientId +
"&redirect_uri=http://www." + Randomx.GetWord(5) + ".com/pdd.php&state=";
refere += toolCode;
refere += sid + "-" + BaseClass.CompanyId;
url = "https://mms.pinduoduo.com/mars/application/authorize";
string data =
"{\"clientId\":\"" + BaseClass.ClientId + "\",\"responseType\":\"code\",\"scope\":[],\"crawlerInfo\":\"" + codex + "\"}";
html = GetPostTaobaoHtml.GetJsonPost(url, refere, true, data, cookies);
string code = BaseRegex.GetStringJson(ref html, "code");
if (string.IsNullOrEmpty(code))
return false;
url = BaseClass.LoginApiUrl + "pddx.php?code=" + code + "&state=" + toolCode + sid + "-" + BaseClass.CompanyId
+ "&client_id=" + BaseClass.ClientId + "&client_secret=" + BaseClass.ApiKey.Replace("2", "z").Replace("1", "I").Replace("c", "C").Replace("9", "O");
html = GetHtml.Utf8(url);
token = BaseRegex.GetStringJson(ref html, "access_token");
refresh = BaseRegex.GetStringJson(ref html, "refresh_token");
url = BaseClass.LoginApiUrl + "?obj=upshopid/pdd_shop_tempx&act=add_token" +
"&shop_name=" + shop.ShopName + "&access_token=" + token + "&refresh_token=" + refresh + "&" + UserLogin.GetSignaturex();
html = GetHtml.Utf8(url);
End:
shop.AccessToken = token;
if (!string.IsNullOrEmpty(refresh))
shop.RefreshToken = refresh;
UpdateMallId(ref shop);
return !string.IsNullOrEmpty(token);
}
catch
{
return false;
}
}
public bool Auth(ShopChatBase shop,ref string client_id)
{
string prevAccessToken = shop.AccessToken;
string prevRefreshToken = shop.RefreshToken;
try
{
string url = BaseClass.LoginApiUrl + "?obj=upshopid/pdd_shop_tempx&act=get_token&shop_name=" + shop.ShopName + "&" + UserLogin.GetSignaturex();
string html = GetHtml.Utf8(url);
if (!IsCps && !string.IsNullOrEmpty(html) && html.Contains("access_token"))
{
shop.AccessToken = BaseRegex.GetStringJson(ref html, "access_token");
shop.RefreshToken = BaseRegex.GetStringJson(ref html, "refresh_token");
if (!InvalidClientId(shop,client_id)&&ReFresh(shop.RefreshToken, client_id))
return true;
}
string TokenResult = GetToken(shop.ShopName);
if (!IsCps && !TokenResult.Contains("error"))
{
shop.AccessToken = BaseRegex.GetStringJson(ref TokenResult, "access_token");
shop.RefreshToken = BaseRegex.GetStringJson(ref TokenResult, "refresh_token");
string client_idTemp = BaseRegex.GetStringJson(ref TokenResult, "client_id");
if (ReFresh(shop.RefreshToken, client_idTemp))//通过缓存的三个值 来判断是否匹配 否则重新取
{
if (!(InvalidClientId(shop, client_id) && shop.AccessToken == prevAccessToken && shop.RefreshToken == prevRefreshToken))
{
client_id = client_idTemp;
shop.ClientId = client_idTemp;
return true;
}
}
}
TokenResult = GetToken(shop.ShopName);
if (!TokenResult.Contains("error"))
{
shop.AccessToken = BaseRegex.GetStringJson(ref TokenResult, "access_token");
shop.RefreshToken = BaseRegex.GetStringJson(ref TokenResult, "refresh_token");
string client_idTemp = BaseRegex.GetStringJson(ref TokenResult, "client_id");
if (!InvalidClientId(shop, client_id)&&ReFresh(shop.RefreshToken, client_idTemp))//通过缓存的三个值 来判断是否匹配 否则重新取
{
client_id = client_idTemp;
shop.ClientId = client_idTemp;
return true;
}
}
#region 之前循环七次请求 现改为一次批量请求
string ClientIdxReSult = "";
int retryCount = 0;
for (int i = 0; i < BaseClass.ClientIdx.Count; i++)
{
ClientIdxReSult += BaseClass.ClientIdx[i] + ",";
}
Again:
url = BaseClass.LoginApiUrl + "?obj=pinbaobao/reflesh_token&act=getx&client_id=" + ClientIdxReSult +
"&shop_name=" + shop.ShopName + "&" + UserLogin.GetSignaturex(shop.ShopId, shop.ShopName);
html = GetHtml.Utf8(url);
if (string.IsNullOrEmpty(html))
{
if (retryCount < 2)//服务器返回超时 则重试两次
{
retryCount++;
goto Again;
}
retryCount = 0;
}
if (!string.IsNullOrEmpty(html) && html.Contains("access_token"))
{
shop.AccessToken = BaseRegex.GetStringJson(ref html, "access_token");
shop.RefreshToken = BaseRegex.GetStringJson(ref html, "refresh_token");
string clientidTemp = BaseRegex.GetStringJson(ref html, "client_id");
if (ReFresh(shop.RefreshToken, clientidTemp))
{
client_id = clientidTemp;
shop.ClientId = clientidTemp;
SaveToken(shop);//保存
return true;
}
}
#endregion
if (BaseClass.UserVersion < 4 && BaseClass.ClientIdSave.Count > 0)
{
int i = Randomx.GetId(BaseClass.ClientIdSave.Count);
url = BaseClass.LoginApiUrl + "?obj=pinbaobao/reflesh_token&act=get&client_id=" + BaseClass.ClientIdSave[i] +
"&shop_name=" + shop.ShopName + "&" + UserLogin.GetSignaturex(shop.ShopId, shop.ShopName);
html = GetHtml.Utf8(url);
if (!string.IsNullOrEmpty(html) && html.Contains("access_token"))
{
shop.AccessToken = BaseRegex.GetStringJson(ref html, "access_token");
shop.RefreshToken = BaseRegex.GetStringJson(ref html, "refresh_token");
if (!InvalidClientId(shop, client_id) && ReFresh(shop.RefreshToken, BaseClass.ClientIdSave[i]))
{
client_id = BaseClass.ClientIdSave[i];
shop.ClientId = BaseClass.ClientIdSave[i];
return true;
}
}
}
for (int i = 0; !IsCps && i < BaseClass.ClientIdx.Count; i++)
{
url = BaseClass.LoginApiUrl + "?obj=pinbaobao/reflesh_token&act=get&client_id=" + BaseClass.ClientIdx[i] +
"&shop_name=" + shop.ShopName + "&" + UserLogin.GetSignaturex(shop.ShopId, shop.ShopName);
html = GetHtml.Utf8(url);
if (!string.IsNullOrEmpty(html) && html.Contains("access_token"))
{
shop.AccessToken = BaseRegex.GetStringJson(ref html, "access_token");
shop.RefreshToken = BaseRegex.GetStringJson(ref html, "refresh_token");
if (!InvalidClientId(shop, client_id) && ReFresh(shop.RefreshToken, BaseClass.ClientIdx[i]))
{
client_id = BaseClass.ClientIdx[i];
shop.ClientId = BaseClass.ClientIdx[i];
return true;
}
}
}
if (IsCps && !MustAuthCps)
{
url = BaseClass.LoginApiUrl + "?obj=pinbaobao/reflesh_token&act=get&client_id=" + client_id +
"&shop_name=" + shop.ShopName + "&" + UserLogin.GetSignaturex(shop.ShopId, shop.ShopName);
html = GetHtml.Utf8(url);
if (!string.IsNullOrEmpty(html) && html.Contains("access_token"))
{
shop.CpsShop.AccessToken = BaseRegex.GetStringJson(ref html, "access_token");
shop.CpsShop.RefreshToken = BaseRegex.GetStringJson(ref html, "refresh_token");
if (!InvalidClientId(shop, client_id)&&ReFresh(shop.CpsShop.RefreshToken, client_id))
{
return true;
}
}
}
string cookies = "";
string sid = BaseClass.ShopId;
if (shop != null)
{
cookies = shop.Cookies;
sid = shop.ShopId;
}
string cookiesx = "";
string urlxs = "https://mms.pinduoduo.com/hades/oauth/authed/list";
string referes = "https://mms.pinduoduo.com/service-market/my-auth";
string dataxs = "{\"page\":1,\"pageSize\":50,\"crawlerInfo\":\"" + CpsSetAllGoods.getAnti0aon() + "\"}";
string htmlxs = GetPostTaobaoHtml.UploadTaobaoPost(urlxs, referes, true, dataxs, cookies, CpsSetAllGoods.getAnti0aon()); //GetPostTaobaoHtml.GetJsonPostAnti(url, refere, true, data, GetShopLoginInfo.Shops[i].Cookies, crawlerInfo);
if (string.IsNullOrEmpty(htmlxs)||htmlxs.Contains("无效"))
{
urlxs = "https://mms.pinduoduo.com/janus/api/subSystem/generateAccessToken";
referes = "https://fuwu.pinduoduo.com/service-market/my-service";
dataxs = "{\"redirectUrl\":\"https://fuwu.pinduoduo.com/clint/api/login?redirect=https%3A%2F%2Ffuwu.pinduoduo.com%2Fservice-market%2Fmy-service\"}";
htmlxs = CpsSetAllGoods.Request_mms_pinduoduo_com(urlxs, referes, cookies, dataxs, CpsSetAllGoods.getAnti0aon());
string accessToken = BaseRegex.GetStringJson(ref htmlxs, "accessToken");
urlxs = "https://fuwu.pinduoduo.com/clint/api/login?redirect=https%3A%2F%2Ffuwu.pinduoduo.com%2Fservice-market%2F&accessToken=" + accessToken;
htmlxs = GetJsonPostURL(urlxs, referes, true, "", cookies);
accessToken = BaseRegex.GetMatchRegexString(ref htmlxs, "SUB_PASS_ID=([^;]+?);");
cookiesx = "SUB_PASS_ID=" + accessToken;
string codexs = CpsSetAllGoods.getAnti0aon();
urlxs = "https://fuwu.pinduoduo.com/columbine/oauth/authed/list";
referes = "https://fuwu.pinduoduo.com/service-market/my-auth";
dataxs = "{\"page\":1,\"pageSize\":50,\"crawlerInfo\":\"" + codexs + "\"}";
htmlxs = CpsSetAllGoods.Request_mms_pinduoduo_com(urlxs, referes, cookies + cookiesx, dataxs, codexs);
}
var m = BaseRegex.GetMatchRegex(ref htmlxs, @"\{[\w\W]+?\}");
for (int j = 0; m != null && j < m.Count; j++)
{
string list = m[j].Groups[0].Value;
string id = BaseRegex.GetStringJson(ref list, "clientId");
if (id == "9558f920cf77403bb475243fe44482ad"||id== "81fd2bbabbd54ec5bf312e05401a0343"
|| id == "9f7d0ce6460b46e7a3833ac2e8325519"||id== "3069c1c325e44e85bcf5b6295694bdae"
||id== "f23bda7f6d9c4b6c8751268891779660"||id== "5d755c0cf97a4cbdb8aa958beea25011" || id == "911bd1c0c0a0403da88334e24ca288a2")
continue;
long auditAt = BaseRegex.GetLongJson(ref list, "auditAt");
long refreshTokenExpiredAt = BaseRegex.GetLongJson(ref list, "refreshTokenExpiredAt");
DateTime t1 = DateTime.Now;// DateTime.Parse(DateTime.Now.ToString("1970-01-01 00:00:00")).AddMilliseconds(auditAt);
DateTime t2 = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)).AddMilliseconds(refreshTokenExpiredAt);
System.TimeSpan t3 = t2 - t1; //两个时间相减 。默认得到的是 两个时间之间的天数 得到:365.00:00:00
double getDay = t3.TotalDays;
if (BaseClass.ClientIdx.Contains(id) && getDay > 0)
{
client_id = id;
break;
}
}
if (client_id == "9558f920cf77403bb475243fe44482ad" || client_id == "81fd2bbabbd54ec5bf312e05401a0343"
|| client_id == "9f7d0ce6460b46e7a3833ac2e8325519" || client_id == "3069c1c325e44e85bcf5b6295694bdae"
|| client_id == "f23bda7f6d9c4b6c8751268891779660" || client_id == "5d755c0cf97a4cbdb8aa958beea25011" || client_id == "911bd1c0c0a0403da88334e24ca288a2")
return false;
//string urlxs = "https://mms.pinduoduo.com/janus/api/subSystem/generateAccessToken";
//string referes = "https://fuwu.pinduoduo.com/service-market/my-service";
//string dataxs = "{\"redirectUrl\":\"https://fuwu.pinduoduo.com/clint/api/login?redirect=https%3A%2F%2Ffuwu.pinduoduo.com%2Fservice-market%2Fmy-service\"}";
//string htmlxs = CpsSetAllGoods.Request_mms_pinduoduo_com(urlxs, referes,cookies, dataxs, CpsSetAllGoods.getAnti0aon());
//string accessToken = BaseRegex.GetStringJson(ref htmlxs, "accessToken");
////urlxs = "https://fuwu.pinduoduo.com/columbine/public/user/login";
////dataxs = "{\"accessToken\":\"" + accessToken + "\"}";
////htmlxs = GetJsonPostURL(urlxs, referes, true, dataxs, cookies);
//urlxs = "https://fuwu.pinduoduo.com/clint/api/login?redirect=https%3A%2F%2Ffuwu.pinduoduo.com%2Fservice-market%2F&accessToken=" + accessToken;
//htmlxs = GetJsonPostURL(urlxs, referes, true, "", cookies);
//accessToken = BaseRegex.GetMatchRegexString(ref htmlxs, "SUB_PASS_ID=([^;]+?);");
//string cookiesx = "SUB_PASS_ID=" + accessToken;
//string serverUrl = "https://fuwu.pinduoduo.com/columbine/pop/service/time/length/list";
//string serverRefere = "https://fuwu.pinduoduo.com/service-market/my-service";
//string serverdata = "{\"page\":1,\"pageSize\":10,\"isExpired\":false}";
//string serverhtml = CpsSetAllGoods.Request_mms_pinduoduo_com(serverUrl, serverRefere, cookies + cookiesx, serverdata, CpsSetAllGoods.getAnti0aon());//GetPostTaobaoHtml.GetJsonPost(serverUrl, serverRefere, true, serverdata, cookies+ cookiesx);
//var m = BaseRegex.GetMatchRegex(ref serverhtml, @"\{[\w\W]+?\}");
//for (int j = 0; m != null && j < m.Count; j++)
//{
// string list = m[j].Groups[0].Value;
// string clientName = BaseRegex.GetStringJson(ref list, "serviceName");
// string id=GetClientId(clientName);
// if (BaseClass.ClientIdx.Contains(id))
// {
// client_id = id;
// break;
// }
//}
if (MustAuth)
goto AuthGo;
url = BaseClass.LoginApiUrl + "pddx.php?act=get&cid=" + BaseClass.CompanyId + "&sid=" + sid + "&state=" + "pbb-" + sid + "-" + BaseClass.CompanyId
+ "&client_id=" + client_id;
html = GetHtml.Utf8(url);
string time = BaseRegex.GetStringJson(ref html, "token_time");
DateTime token_time = BaseClass.GetServerTimeString().AddHours(-1);
if (!string.IsNullOrEmpty(time))
token_time = DateTime.Parse(time);
string token = BaseRegex.GetStringJson(ref html, "access_token");
string refresh = BaseRegex.GetStringJson(ref html, "refresh_token");
if (token_time.AddMinutes(3) > BaseClass.GetServerTimeString() || !NoCache && !string.IsNullOrEmpty(token))
goto End;
if (!string.IsNullOrEmpty(refresh))
{
bool ok = ReFresh(refresh, client_id);
if (ok)
{
goto End;
}
}
AuthGo:
string refere = "https://mms.pinduoduo.com/open.html?response_type=code&client_id=" +
client_id + "&redirect_uri=http://www.qimake.cn/pdd.php&state=";
refere += "pbb-";
refere += sid + "-" + BaseClass.CompanyId;
if (IsCps && SignDuoProtocol && BaseClass.Balance >= 20)
{
url = "https://mms.pinduoduo.com/cps/api/mall/signDuoProtocol";
html = GetPostTaobaoHtml.UploadTaobaoPost(url, refere, true, "{}", cookies);
if (!string.IsNullOrEmpty(html) && html.Contains("\"success\":true"))
{
//url = BaseClass.BuildObjString("shop&act=time_addx");
//string datax = UserLogin.GetSignaturex("", shop.ShopName) + "&tool_name=" + GetHtml.UrlEncodeUtf8(BaseClass.AgentName);
//html = GetHtml.PostUtf8(url, datax);
//if (html.Contains("ok,"))
// BaseClass.Balance = int.Parse(html.Replace("ok,", "").Trim());
shop.CpsShop.Status = 1;
CpsShopNew.ShopTemp.Add(shop);
}
}
url = "https://mms.pinduoduo.com/mars/application/authorize";
cookies = shop.Cookies;
string data = "{\"clientId\":\"" + client_id + "\",\"responseType\":\"code\",\"scope\":[]}";
html = GetPostTaobaoHtml.UploadTaobaoPost(url, refere, true, data, cookies);
if (html.Contains("接口已下线")|| html.Contains("403"))
{
string codex = UploadPinduoduoMain.AntiContent;
url = "https://mms.pinduoduo.com/hades/oauth/authorize";
data = "{\"clientId\":\""+ client_id + "\",\"responseType\":\"code\",\"scope\":[],\"isTrial\":0,\"crawlerInfo\":\"" + codex + "\"}";
html = GetPostTaobaoHtml.UploadTaobaoPost(url, refere, true, data, cookies);
}
if (html.Contains("接口已下线") || html.Contains("403"))
{
if (string.IsNullOrEmpty(cookiesx))
{
urlxs = "https://mms.pinduoduo.com/janus/api/subSystem/generateAccessToken";
referes = "https://fuwu.pinduoduo.com/service-market/my-service";
dataxs = "{\"redirectUrl\":\"https://fuwu.pinduoduo.com/clint/api/login?redirect=https%3A%2F%2Ffuwu.pinduoduo.com%2Fservice-market%2Fmy-service\"}";
htmlxs = CpsSetAllGoods.Request_mms_pinduoduo_com(urlxs, referes, cookies, dataxs, CpsSetAllGoods.getAnti0aon());
string accessToken = BaseRegex.GetStringJson(ref htmlxs, "accessToken");
urlxs = "https://fuwu.pinduoduo.com/clint/api/login?redirect=https%3A%2F%2Ffuwu.pinduoduo.com%2Fservice-market%2F&accessToken=" + accessToken;
htmlxs = GetJsonPostURL(urlxs, referes, true, "", cookies);
accessToken = BaseRegex.GetMatchRegexString(ref htmlxs, "SUB_PASS_ID=([^;]+?);");
cookiesx = "SUB_PASS_ID=" + accessToken;
}
string codex = UploadPinduoduoMain.AntiContent;
url = "https://fuwu.pinduoduo.com/columbine/oauth/authorize";
data = "{\"clientId\":\"" + client_id + "\",\"responseType\":\"code\",\"scope\":[],\"isTrial\":0,\"crawlerInfo\":\"" + codex + "\"}";
html = GetPostTaobaoHtml.UploadTaobaoPost(url, referes, true, data, cookiesx);
}
if (html.Contains("403"))
{
for (int j = 0; j < GetProxyHtml.ipCount; j++)
{
var proxyObject = GetProxyHtml.GetIPSProxyQueue();
if (proxyObject == null)
continue;
html = GetProxyHtml.UploadNewProxyTaobaoPost(url, refere, true, data, cookies, proxyObject);
GetProxyHtml.IpIndex++;
if (html.Contains("\"success\":true"))
break;
}
}
for (int i = 0; (html.Contains("应用处罚中")|| html.Contains("购买")) && i < BaseClass.ClientIdx.Count; i++)
{
shop.ClientId = BaseClass.ClientIdx[i];
client_id = shop.ClientId;
data = "{\"clientId\":\"" + client_id + "\",\"responseType\":\"code\",\"scope\":[]}";
html = GetPostTaobaoHtml.UploadTaobaoPost(url, refere, true, data, cookies);
}
string code = BaseRegex.GetStringJson(ref html, "code");
if (string.IsNullOrEmpty(code) && !html.Contains("应用处罚中"))
{
html = GetPostTaobaoHtml.GetJsonPostByProxy(url, refere, true, data, cookies);
code = BaseRegex.GetStringJson(ref html, "code");
}
if (string.IsNullOrEmpty(code) && !html.Contains("应用处罚中") && !html.Contains("403"))
{
string crawlerInfo = "";
string serverUrl = "https://mms.pinduoduo.com/hades/oauth/authed/list";
string serverRefere = "https://mms.pinduoduo.com/service-market/my-auth";
string serverdata = "{\"page\":1,\"pageSize\":50,\"crawlerInfo\":\"" + crawlerInfo + "\"}";
string serverhtml = GetPostTaobaoHtml.GetJsonPost(serverUrl, serverRefere, true, serverdata, cookies, crawlerInfo);
m = BaseRegex.GetMatchRegex(ref serverhtml, @"\{[\w\W]+?\}");
for (int j = 0; m != null && j < m.Count; j++)
{
string list = m[j].Groups[0].Value;
string id = BaseRegex.GetStringJson(ref list, "clientId");
if (BaseClass.ClientIdx.Contains(id))
{
url = "https://mms.pinduoduo.com/mars/application/authorize";
cookies = shop.Cookies;
data = "{\"clientId\":\"" + id + "\",\"responseType\":\"code\",\"scope\":[]}";
html = GetPostTaobaoHtml.UploadTaobaoPost(url, refere, true, data, cookies);
if (html.Contains("接口已下线") || html.Contains("403"))
{
string codex = UploadPinduoduoMain.AntiContent;
url = "https://mms.pinduoduo.com/hades/oauth/authorize";
data = "{\"clientId\":\"" + client_id + "\",\"responseType\":\"code\",\"scope\":[],\"isTrial\":0,\"crawlerInfo\":\"" + codex + "\"}";
html = GetPostTaobaoHtml.UploadTaobaoPost(url, refere, true, data, cookies);
}
if (html.Contains("403"))
{
for (int i = 0; i < GetProxyHtml.ipCount; i++)
{
var proxyObject = GetProxyHtml.GetIPSProxyQueue();
if (proxyObject == null)
continue;
html = GetProxyHtml.UploadNewProxyTaobaoPost(url, refere, true, data, cookies, proxyObject);
GetProxyHtml.IpIndex++;
if (html.Contains("\"success\":true"))
break;
}
}
code = BaseRegex.GetStringJson(ref html, "code");
if (!string.IsNullOrEmpty(code))
{
client_id = id;
shop.ClientId = id;
break;
}
}
}
}
if (string.IsNullOrEmpty(code) && !html.Contains("应用处罚中"))
{
for (int i = 0; !IsCps && i < BaseClass.ClientIdx.Count; i++)
{
url = "https://mms.pinduoduo.com/mars/application/authorize";
cookies = shop.Cookies;
data = "{\"clientId\":\"" + BaseClass.ClientIdx[i] + "\",\"responseType\":\"code\",\"scope\":[]}";
html = GetPostTaobaoHtml.UploadTaobaoPost(url, refere, true, data, cookies);
code = BaseRegex.GetStringJson(ref html, "code");
if (!string.IsNullOrEmpty(code))
{
client_id = BaseClass.ClientIdx[i];
shop.ClientId = BaseClass.ClientIdx[i];
break;
}
}
if (string.IsNullOrEmpty(code))
return false;
}
Shops.Add(shop.Name);
url = BaseClass.LoginApiUrl + "pddx.php?code=" + code + "&state=" + "pbb-";
url += sid + "-" + BaseClass.CompanyId
+ "&client_id=" + client_id;
html = GetHtml.Utf8(url);
token = BaseRegex.GetStringJson(ref html, "access_token");
refresh = BaseRegex.GetStringJson(ref html, "refresh_token");
if (string.IsNullOrEmpty(token) || string.IsNullOrEmpty(refresh))
return false;
//if (string.IsNullOrEmpty(shop.ClientId))
shop.ClientId = client_id;
if (!IsCps)
{
url = BaseClass.LoginApiUrl + "?obj=upshopid/pdd_shop_tempx&act=add_token" +
"&shop_name=" + shop.ShopName + "&access_token=" + token + "&refresh_token=" + refresh + "&" + UserLogin.GetSignaturex();
html = GetHtml.Utf8(url);
}
url = BaseClass.LoginApiUrl + "?obj=pinbaobao/reflesh_token&act=add&client_id="+client_id +
"&shop_name=" + shop.ShopName + "&access_token=" + token + "&refresh_token=" + refresh + "&" + UserLogin.GetSignaturex(shop.ShopId,shop.ShopName);
html = GetHtml.Utf8(url);
End:
if (!IsCps)
{
shop.AccessToken = token;
shop.RefreshToken = refresh;
}
else
{
shop.CpsShop.AccessToken = token;
shop.CpsShop.RefreshToken = refresh;
}
return !string.IsNullOrEmpty(token);
}
catch
{
return false;
}
}
/// <summary>
/// 判断失效id
/// </summary>
/// <param name="shop"></param>
/// <param name="client_id"></param>
/// <returns></returns>
public bool InvalidClientId(ShopChatBase shop, string client_id)
{
bool invalfalg = false;
if (PinduoduoApiGetProduct.ShopInvalidClientID.ContainsKey(shop.ShopId))
{
var lst = PinduoduoApiGetProduct.ShopInvalidClientID[shop.ShopId];
for (int i = 0; i < lst.Count; i++)
{
if (lst[i] == client_id)
{
invalfalg = true;
break;
}
}
}
return invalfalg;
}
/// <summary>
/// 保存ClientId
/// </summary>
/// <param name="Shop"></param>
private void SaveToken(ShopChatBase Shop)
{
string url = BaseClass.LoginApiUrl + "?obj=pinbaobao/cache_authorize&act=save&shop_name=" + Shop.ShopName + "&access_token=" + Shop.AccessToken + "&client_id=" + Shop.ClientId + "&refresh_token=" + Shop.RefreshToken + "&" + UserLogin.GetSignaturex();
string html = GetHtml.Utf8(url);
}
private string GetToken(string ShopName)
{
string url = BaseClass.LoginApiUrl + "?obj=pinbaobao/cache_authorize&act=get&shop_name=" + ShopName + "&" + UserLogin.GetSignaturex();
string html = GetHtml.Utf8(url);
return html;
}
public bool ReFresh(string refresh, string client_id)
{
string url = BaseClass.LoginApiUrl + "?obj=upshopid/pdd_refreshx&act=refresh&refresh_token=" + refresh
+ "&client_id=" + client_id;
string html = GetHtml.Utf8(url);
if (!string.IsNullOrEmpty(html) && html.Contains("access_token"))
return true;
return false;
}
public static string GetJsonPostURL(string url, string refere, bool isUtf8, string data, string cookies)
{
string userAgent = string.IsNullOrEmpty(BaseClass.ChromeUserAgent) ? BaseClass.UserAgent : BaseClass.ChromeUserAgent;
WebClientx wb = new WebClientx();
wb.Timeout = 10;
wb.AllowAutoRedirect = false;
wb.Headers.Add("Upgrade-Insecure-Requests", @"1");
wb.Headers.Add("Accept-Encoding", "gzip,deflate,br");
wb.Headers.Add("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
wb.Headers.Add("User-Agent", userAgent);
wb.Headers.Add("Sec-Fetch-Site", @"same-site");
wb.Headers.Add("Sec-Fetch-Mode", @"navigate");
wb.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3");
wb.Headers.Add("Referer", "https://mms.pinduoduo.com/login/sso?platform=fuwu&redirectUrl=https%3A%2F%2Ffuwu.pinduoduo.com%2Fclint%2Fapi%2Flogin%3Fredirect%3Dhttps%253A%252F%252Ffuwu.pinduoduo.com%252Fservice-market%252Fmy-service");
if (!string.IsNullOrEmpty(cookies))
wb.Headers.Add("Cookie", cookies);
string rehead = wb.Headers.ToString();
var encode = isUtf8 ? Encoding.UTF8 : Encoding.GetEncoding("gbk");
byte[] b, postData = encode.GetBytes(data);
string html, head;
try
{
//wb.Headers.Add("Accept", "application/json");
//wb.Headers.Add("Content-Type", "application/json; charset=UTF-8");
if (string.IsNullOrEmpty(data))
{
b = wb.DownloadData(url);
}
else
{
wb.Headers.Add("ContentLength", postData.Length.ToString());
rehead = wb.Headers.ToString();
b = wb.UploadData(url, "POST", postData);
}
html = encode.GetString(b);
head = wb.ResponseHeaders.ToString();
html = wb.ResponseHeaders["Set-Cookie"];
}
catch (Exception ee)
{
html = "提交错误:" + ee.Message;
head = "";
}
return html;
}
public string GetClientId(string clientName)
{
if (string.IsNullOrEmpty(clientName))
return "";
string result = "";
switch (clientName.ToUpper())
{
case "螃蟹工具":
result = "7feaefdf527d45129219ca398c922bb0";
break;
case "奶爸助理":
result = "f4a3e3bb0253448d8272537726299621";
break;
case "途途上货助手":
result = "b1981defcd704a7d88c8a734358fb065";
break;
case "微木ERP":
result = "81fd2bbabbd54ec5bf312e05401a0343";
break;
case "蛮牛工具":
result = "911bd1c0c0a0403da88334e24ca288a2";
break;
case "微木订单":
result = "5d755c0cf97a4cbdb8aa958beea25011";
break;
case "点金工具箱":
result = "3069c1c325e44e85bcf5b6295694bdae";
break;
case "拼商宝":
result = "9558f920cf77403bb475243fe44482ad";
break;
case "凯达工具":
result = "88d6ef134c3b4a07b7a22d8bbef9b1b4";
break;
case "飓风管家":
result = "f23bda7f6d9c4b6c8751268891779660";
break;
case "飓风发货":
result = "9f7d0ce6460b46e7a3833ac2e8325519";
break;
case "运筹ERP":
result = "716a6bcc69d7441d856905eb79b892fd";
break;
}
return result;
}
}
}