您的位置 首页 php

C#实现 调用百度网盘API实现批量转存百度网盘

百度OAuth 请先阅读这个文档

首先根据一下文档获取code
百度网盘 开放平台申请百度网盘应用

申请 AppKey和SecretKey
一、获取code代码

 string get_code_url = "#34;;
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 = "#34;;          
           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. textBox 1.Text = at;
           }));  

三、获取sekey代码

 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("{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代码

  string  url = "#34;;
            string postData = string.Format(" short url={0}&page={1}#={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);  

五、转存代码

 string url1 = $"{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 中使用了。

欢迎点赞+转发+关注!大家的支持是我分享最大的动力!!!

文章来源:智云一二三科技

文章标题:C#实现 调用百度网盘API实现批量转存百度网盘

文章地址:https://www.zhihuclub.com/153185.shtml

关于作者: 智云科技

热门文章

网站地图