c#实现 调用百度网盘api实现批量转存百度网盘分享链接
百度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代码
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代码
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.Groups.Value;
at = ShopNos1.Groups.Value;
this.Invoke(new Action(() =>
{
this.textBox1.Text = at;
}));
三、获取sekey代码
string pwd = 行行行;
string share_link = xxx;
var sid = share_link.Split('/').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.Groups.Value;
四、获取shareid,uk和fsidlist代码
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.Groups.Value;
MatchCollection ShopNos1 = Regex.Matches(result.Html, @"uk"":([\s\S]*?)}");
uk = ShopNos1.Groups.Value;
JObject jo = (JObject)JsonConvert.DeserializeObject(result.Html);
StringBuilder fsid_list = new StringBuilder(jo["list"]["fs_id"].ToString());
for (int j = 1; j < jo["list"].Count(); j++)
{
fsid_list.Append("," + jo["list"]["fs_id"]);
}
string temp= "[" + fsid_list.ToString() + "]";
fsidlist1 = System.Web.HttpUtility.UrlEncode(temp,Encoding.UTF8);
五、转存代码
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.Groups.Value;
if (error.Equals("0")) { MessageBox.Show("单链接转存成功"); }
以上是百度网盘分享链接的实现代码,封装后就可以在winform中使用了。
有什么不对的地方请交流 非常好,谢谢分享 有没有成品 六度 发表于 2022-4-18 11:04
有没有成品
有成品 正在优化中 期待成品上线 大佬等你报喜 楼主加油 期待成品上线! 存到百度网盘的文件不买会员下载速度如蜗牛就尴尬了 HALLO 发表于 2022-5-30 20:36
楼主加油 期待成品上线!
我没有vip接口,有也不能发论坛里,违规的。 获取shareid,uk和fsidlist代码 返回-9
页:
[1]
2