本帖最后由 Pleasant 于 2021-2-10 11:21 编辑
分享一个我的实现方式。
1. 获取二维码链接
[C#] 纯文本查看 复制代码
// 获取s_token
SessionClient.DefaultRequestHeaders.Add("Accept", "application/json, text/plain, */*");
SessionClient.DefaultRequestHeaders.Add("ContentType", "application/x-www-form-urlencoded");
SessionClient.DefaultRequestHeaders.Referrer = new Uri("https://plogin.m.jd.com/login/login?appid=300&returnurl=https%3A%2F%2Fwq.jd.com%2Fpassport%2FLoginRedirect%3Fstate%3D1101078047599%26returnurl%3Dhttps%253A%252F%252Fhome.m.jd.com%252FmyJd%252Fnewhome.action%253Fsceneval%253D2%2526ufc%253D%2526%252FmyJd%252Fhome.action&source=wq_passport");
var response = await SessionClient.GetAsync(@"https://plogin.m.jd.com/cgi-bin/mm/new_login_entrance?lang=chs&appid=300&returnurl=https:%2F%2Fwq.jd.com%2Fpassport%2FLoginRedirect%3Fstate%3D1101078047599%26returnurl%3Dhttps%253A%252F%252Fhome.m.jd.com%252FmyJd%252Fnewhome.action%253Fsceneval%253D2%2526ufc%253D%2526%252FmyJd%252Fhome.action&source=wq_passport");
if (response.StatusCode != HttpStatusCode.OK)
{
Lbl_QRStatus.Text = "二维码加载失败,请重试或采用Cookie登录";
return;
}
var responseText = await response.Content.ReadAsStringAsync();
var responseJo = (JObject)JsonConvert.DeserializeObject(responseText);
SToken = responseJo["s_token"].ToString();
// 获取token和okl_token
string tokenAPI = string.Format("https://plogin.m.jd.com/cgi-bin/m/tmauthreflogurl?s_token={0}&v={1}&remember=true", SToken, Utils.GetTimeStampLong());
var nvc = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("lang", "chs"),
new KeyValuePair<string, string>("appid", "300"),
new KeyValuePair<string, string>("returnurl", "https%3A%2F%2Fwqlogin2.jd.com%2Fpassport%2FLoginRedirect%3Fstate%3D1100399130787%26returnurl%3D%252F%252Fhome.m.jd.com%252FmyJd%252Fnewhome.action%253Fsceneval%253D2%2526ufc%253D%2526%252FmyJd%252Fhome.action"),
new KeyValuePair<string, string>("source", "wq_passport")
};
var message = new HttpRequestMessage(HttpMethod.Post, tokenAPI) { Content = new FormUrlEncodedContent(nvc) };
response = await SessionClient.SendAsync(message);
responseText = await response.Content.ReadAsStringAsync();
responseJo = (JObject)JsonConvert.DeserializeObject(responseText);
if (response.StatusCode != HttpStatusCode.OK || responseJo["errcode"].ToString() != "0")
{
Lbl_QRStatus.Text = "二维码加载失败...";
return;
}
Token = responseJo["token"].ToString();
var collection = CookieContainer.GetCookies(new Uri("https://plogin.m.jd.com"));
foreach (Cookie cookie in collection)
{
if (cookie.Name == "okl_token")
OKLToken = cookie.Value;
}
if (string.IsNullOrWhiteSpace(OKLToken))
{
Lbl_QRStatus.Text = "二维码加载失败...";
return;
}
// 构建二维码URL
string qrUrl = string.Format("https://plogin.m.jd.com/cgi-bin/m/tmauth?client_type=m&appid=300&token={0}", Token);
2. 轮训结果
[C#] 纯文本查看 复制代码
if (string.IsNullOrWhiteSpace(Token)) return;
if (string.IsNullOrWhiteSpace(OKLToken)) return;
// 对查询API进行轮询
string checkAPI = string.Format("https://plogin.m.jd.com/cgi-bin/m/tmauthchecktoken?&token={0}&ou_state=0&okl_token={1}", Token, OKLToken);
var nvc = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("lang", "chs"),
new KeyValuePair<string, string>("appid", "300"),
new KeyValuePair<string, string>("returnurl", "https%3A%2F%2Fwqlogin2.jd.com%2Fpassport%2FLoginRedirect%3Fstate%3D1100399130787%26returnurl%3D%252F%252Fhome.m.jd.com%252FmyJd%252Fnewhome.action%253Fsceneval%253D2%2526ufc%253D%2526%252FmyJd%252Fhome.action"),
new KeyValuePair<string, string>("source", "wq_passport")
};
var message = new HttpRequestMessage(HttpMethod.Post, checkAPI) { Content = new FormUrlEncodedContent(nvc) };
var response = SessionClient.SendAsync(message).Result;
var responseText = response.Content.ReadAsStringAsync().Result;
var responseJo = (JObject)JsonConvert.DeserializeObject(responseText);
// 如果扫码成功,那么得到的CookieContainer就是所要的,否则更新信息到界面上
if (responseJo["errcode"].ToString() == "0")
{
// 设置一下关键Cookie为永不过期
var cookies = CookieContainer.GetCookies(new Uri("https://api.m.jd.com"));
foreach (Cookie cookie in cookies)
{
cookie.Expires = DateTime.Now.AddMonths(1);
}
TM_FetchStatus.Enabled = false;
this.DialogResult = DialogResult.OK;
return;
}
else
{
Lbl_QRStatus.Text = responseJo["message"].ToString();
} |