百度OAuth 请先阅读这个文档
http://developer.baidu.com/wiki/index.php?title=docs/oauth
首先根据一下文档获取code
http://developer.baidu.com/wiki/index.php?title=docs/oauth/authorization在百度网盘开放平台申请百度网盘应用
https://pan.baidu.com/union/home
申请 AppKey和SecretKey
一、获取code代码
[C#] 纯文本查看 复制代码 string get_code_url = "https://openapi.baidu.com/oauth/2.0/authorize?response_type=code&client_id=xxxx&redirect_uri=oob&scope=netdisk";
ChromeOptions options = new ChromeOptions();
options.PageLoadStrategy = PageLoadStrategy.Normal;
options.AddArgument("disable-infobars");
options.AddArgument("user-agent='Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3880.400 QQBrowser/10.8.4554.400'");
options.AddArgument("Accept='text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8'");
IWebDriver webDriver = new ChromeDriver(options); ;
webDriver.Navigate().GoToUrl(get_code_url);
二、获取access_token代码
[C#] 纯文本查看 复制代码 client_id = this.textBox2.Text;
client_secret = xxx;
string code = xxx;
string get_token_url = "https://openapi.baidu.com/oauth/2.0/token?grant_type=authorization_code";
string postData = string.Format("code={0}&client_id={1}&client_secret={2}&redirect_uri={3}", code, client_id, client_secret, "oob");
HttpHelper http = new HttpHelper();
HttpItem item = new HttpItem()
{
URL = get_token_url,
Method = "post",
ContentType = "application/x-www-form-urlencoded",
Referer = get_token_url,
Postdata = postData,
};
HttpResult result = http.GetHtml(item);
MatchCollection ShopNos = Regex.Matches(result.Html, @"refresh_token"":""([\s\S]*?)""");
MatchCollection ShopNos1 = Regex.Matches(result.Html, @"access_token"":""([\s\S]*?)""");
rt = ShopNos[0].Groups[1].Value;
at = ShopNos1[0].Groups[1].Value;
this.Invoke(new Action(() =>
{
this.textBox1.Text = at;
}));
三、获取sekey代码
[C#] 纯文本查看 复制代码
string pwd = 行行行;
string share_link = xxx;
var sid = share_link.Split('/')[share_link.Split('/').Length - 1].Trim();
surl = sid.Substring(1, sid.Length - 1);
var t1 = ConvertDateTimeToInt(DateTime.Now);
string url1 = string.Format("https://pan.baidu.com/rest/2.0/xpan/share?method=verify&surl={0}", surl);
string postData = string.Format("pwd={0}", pwd);
HttpHelper http = new HttpHelper();
HttpItem itemhttp = new HttpItem()
{
URL = url1,
Method = "post",
ContentType = "multipart/form-data; charset=UTF-8",
Referer = "pan.baidu.com",
Postdata = postData,
};
HttpResult result = http.GetHtml(itemhttp);
MatchCollection ShopNos = Regex.Matches(result.Html, @"randsk"":""([\s\S]*?)""");
sekey = ShopNos[0].Groups[1].Value;
四、获取shareid,uk和fsidlist代码
[Asm] 纯文本查看 复制代码
string url = "https://pan.baidu.com/rest/2.0/xpan/share?method=list";
string postData = string.Format("shorturl={0}&page={1}&num={2}&root={3}&fid={4}&sekey={5}", surl,1,100,1,0,sekey);
HttpHelper http = new HttpHelper();
HttpItem itemhttp = new HttpItem()
{
URL = url,
Method = "post",
ContentType = "application/x-www-form-urlencoded",
Referer = "pan.baidu.com",
Postdata = postData,
};
HttpResult result = http.GetHtml(itemhttp);
MatchCollection ShopNos = Regex.Matches(result.Html, @"share_id"":([\s\S]*?),");
shareid = ShopNos[0].Groups[1].Value;
MatchCollection ShopNos1 = Regex.Matches(result.Html, @"uk"":([\s\S]*?)}");
uk = ShopNos1[0].Groups[1].Value;
JObject jo = (JObject)JsonConvert.DeserializeObject(result.Html);
StringBuilder fsid_list = new StringBuilder(jo["list"][0]["fs_id"].ToString());
for (int j = 1; j < jo["list"].Count(); j++)
{
fsid_list.Append("," + jo["list"][j]["fs_id"]);
}
string temp= "[" + fsid_list.ToString() + "]";
fsidlist1 = System.Web.HttpUtility.UrlEncode(temp,Encoding.UTF8);
五、转存代码
[Asm] 纯文本查看 复制代码
string url1 = $"http://pan.baidu.com/rest/2.0/xpan/share?method=transfer&access_token={at}&shareid={shareid}&from={uk}";
string postData = $"sekey={sekey}&fsidlist={fsidlist1}&path={path}";
HttpHelper http = new HttpHelper();
HttpItem itemhttp = new HttpItem()
{
URL = url1,
Method = "post",
ContentType = "application/x-www-form-urlencoded",
Referer = "pan.baidu.com",
Postdata = postData,
};
HttpResult result = http.GetHtml(itemhttp);
MatchCollection ShopNos = Regex.Matches(result.Html, @"errno"":([\s\S]*?),");
var error = ShopNos[0].Groups[1].Value;
if (error.Equals("0")) { MessageBox.Show("单链接转存成功"); }
以上是百度网盘分享链接的实现代码,封装后就可以在winform中使用了。
有什么不对的地方请交流 |