開發百度小程序后,接下來,人們最想做的是讓百度更多的錄入自家內容。因為小程序資源被索引后,才可能在搜索結果中展現。
百度也提供了小程序的自然搜索提交入口。一共有兩種方式:
第一種是用已有的H5網站資源替換,這種的錄入速度應該非常快速,百度小程序平台將優先對 H5站點下的 TOP 流量 URL進行替換處理。
第二種提交新資源,接下來我們主要講這第二種情況。
-----------------------------------------------------------------------------------------------------------
提交新資源又分為 “天級收錄”和“周級收錄”以及“自動同步”。資源提交的內容為小程序頁面的path路徑+參數。(官網地址入口 )天級提交一般48小時內有反饋,周級提交需要周級處理后反饋。
“天級收錄”和“周級收錄” 都又2種提交資源的方式,第一種是txt資源上傳(Sitemap),整理小程序頁面的所有Path路徑+參數,每行一條記錄。第二種方式為API提交。(官網教程入口)
根據官方提供的API文檔說明,我們會知道如下幾點:
第一:api的請求接口地址,用Post提交
https://openapi.baidu.com/rest/2.0/smartapp/access/submitsitemap/api
第二、參數說明
| 參數名 | 類型 | 是否必須 | 描述 | 示例 |
|---|---|---|---|---|
| access_token | string | 是 | 權限校驗Token,獲取方式見開發者服務權限說明 。 | |
| type | int | 是 | 通過TYPE字段選擇上傳接口。 · 0:周級提交,一周左右生效; · 1:天級提交,2~3天生效; · 2:小時級提交,1小時內生效 |
0 |
| url_list | string | 是 | 小程序path集合,多個path用逗號分隔。 · 天級提交配額根據提交活躍度和資源質量進行調節,具體以平台顯示為准; · 周級提交配額每日上限為5W條,每次提交上限為3000條。 |
/pages/index1?id=1,/pages/index2 |
第三,返回結果說明:
1、正確的結果:
{ "errno": 0, "msg": "success" }
2、若出現錯誤,則會返回錯誤提示:錯誤碼說明:
| 錯誤碼 | 錯誤描述 |
|---|---|
| 0 | 成功 |
| -1024 | 業務異常 |
| 500 | 對不起,服務器出錯了,請稍候再試 |
| 2002 | 小程序不存在/選擇的收錄級別不合法/url數量不合法 |
| 30001 | 參數有誤 |
| 30013 | 數量超上限 |
| 47005 | 文件上傳失敗 |
| 60005 | 尚未綁定熊掌ID,請先綁定熊掌ID |
官方提供幾種語言的demo代碼,比如 java,python,php,接下來我用c#代碼實現相關的功能:
第1步:是獲取access_token值:
string App_Key = "GUsp9GbxpfZNpnp1DjR1GrG2zGwGLhq2"; string App_Secret = "百度小程序的app_secret值"; string url = "https://openapi.baidu.com/oauth/2.0/token"; string param = $"grant_type=client_credentials&client_id={App_Key}&client_secret={App_Secret}&scope=smartapp_snsapi_base"; string ret = Utils.HttpPost(url, param); this.lblAccessToken.Text = ret;
Utils.HttpPost里的方法如下:
/// <summary> /// HTTP POST方式請求數據 /// </summary> /// <param name="url">URL.</param> /// <param name="param">POST的數據</param> /// <returns></returns> public static string HttpPost(string url, string param) { HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.Accept = "*/*"; request.Timeout = 15000; request.AllowAutoRedirect = false; StreamWriter requestStream = null; WebResponse response = null; string responseStr = null; try { requestStream = new StreamWriter(request.GetRequestStream()); requestStream.Write(param); requestStream.Close(); response = request.GetResponse(); if (response != null) { StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8); responseStr = reader.ReadToEnd(); reader.Close(); } } catch (Exception) { throw; } finally { request = null; requestStream = null; response = null; } return responseStr; }
access_token獲取成功后,開始我們的url資源提交了。根據自家小程序頁面path以及參數,提交相應的資源。多條記錄用逗號隔開。
/// <summary> /// 每次發一批文章給百度小程序。 /// </summary> /// <param name="alist">alist的條數不要超過每次的上限。</param> /// <returns></returns> private string SendToBaiDu(int type, List<Model.article_id> alist) { string ret = ""; string ACCESS_TOKEN = "24.9ebc57a8f3b00375b8bb0ee59892ce52.2592000.1584192924.282335-18403980"; string postUrl = "https://openapi.baidu.com/rest/2.0/smartapp/access/submitsitemap/api"; string param = "access_token=" + ACCESS_TOKEN; StringBuilder sb = new StringBuilder(""); sb.Append("&type="+ type); sb.Append("&url_list="); for (int i = 0; i < alist.Count(); i++) { if (i != alist.Count() - 1) { sb.Append("pages/detail/detail?query=" + alist[i].id + ","); } else { sb.Append("pages/detail/detail?query=" + alist[i].id); } } param += sb.ToString(); ret = Utils.HttpPost(postUrl, param); return ret; }
到此就成功提交了,但是要記得每天的上限以及每次提交記錄的上線。
花絮:
在開發調試過程中,遇到一個非常奇怪的現象,不管用什么方式提交資源,百度接口總會返回“47005文件上傳失敗”。一直以為是自己的代碼問題,排查了好久沒有解決。決定是官方論壇發帖求助,(入口),結果令人驚奇的發現,這個根據就不是我代碼的bug,而是百度官方的接口返回有問題,等了大天,百度官方才修復了這個bug 。 所以,調用第3方接口,遇到詭異現象,有可能真的是第3方的問題。

本文為沐雪原創文章,歡迎轉載,轉載請標注來源。謝謝。
參考文獻:
