吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 5400|回复: 10
收起左侧

[其他转载] c#实现 调用百度网盘api实现批量转存百度网盘分享链接

[复制链接]
bjszz 发表于 2022-4-13 09:34
百度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中使用了。
有什么不对的地方请交流

免费评分

参与人数 5吾爱币 +10 热心值 +4 收起 理由
jxhuangwei + 1 谢谢@Thanks!
鸡蛋羹 + 1 + 1 我很赞同!
kiviki + 1 + 1 谢谢@Thanks!
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
清江堤畔 + 1 谢谢@Thanks!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

sgh2zlx 发表于 2022-4-13 11:48
非常好,谢谢分享
六度 发表于 2022-4-18 11:04
 楼主| bjszz 发表于 2022-4-18 11:26
lightlee 发表于 2022-4-18 23:54
期待成品上线
鸡蛋羹 发表于 2022-4-19 14:51
大佬等你报喜
HALLO 发表于 2022-5-30 20:36
楼主加油 期待成品上线!
wjl 发表于 2022-5-30 21:11
存到百度网盘的文件不买会员下载速度如蜗牛就尴尬了
InternetDog 发表于 2022-8-10 09:53
HALLO 发表于 2022-5-30 20:36
楼主加油 期待成品上线!

我没有vip接口,有也不能发论坛里,违规的。
你猫临死前 发表于 2022-10-23 11:34
获取shareid,uk和fsidlist代码 返回-9
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-23 23:02

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表