微信開發——測試號申請,介面配置,JS介面安全域名,自定義選單
假設你已經申請啦微信公眾號。
1、申請測試賬號:
先申請公眾號後,點選進入公從號的管理頁面;找到“開發者工具”,找到“公眾平臺測試賬號”,點選“進入”。
2、介面配置資訊設定
必須要外網哦,也就是微信伺服器要能訪問到你填寫到url。
Url到內容有講究,這裡已java為例項,我的url對應的是一個springmvc的控制器方法,你可以寫servlet或者jsp,頁面填寫的Token要跟控制器的Token一致。
import java.io.IOException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Arrays; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.yfkj.xfcamp.controller.base.BaseController; /** * 微信Token * @author 胡漢三 * * 2017年4月7日 上午8:39:17 */ @Controller @RequestMapping("/wechat/token") public class WeChatToken extends BaseController{ public static final String TOKEN = "yfkj_xfcamp_token"; /** * 微信Token驗證 * @param signature 微信加密簽名 * @param timestamp 時間戳 * @param nonce 隨機數 * @param echostr 隨機字串 * @return * @throws NoSuchAlgorithmException * @throws IOException */ @RequestMapping("get") public void getToken(String signature,String timestamp,String nonce,String echostr) throws NoSuchAlgorithmException, IOException{ // 將token、timestamp、nonce三個引數進行字典序排序 System.out.println("signature:"+signature); System.out.println("timestamp:"+timestamp); System.out.println("nonce:"+nonce); System.out.println("echostr:"+echostr); System.out.println("TOKEN:"+TOKEN); String[] params = new String[] { TOKEN, timestamp, nonce }; Arrays.sort(params); // 將三個引數字串拼接成一個字串進行sha1加密 String clearText = params[0] + params[1] + params[2]; String algorithm = "SHA-1"; String sign = new String( org.apache.commons.codec.binary.Hex.encodeHex(MessageDigest.getInstance(algorithm).digest((clearText).getBytes()), true)); // 開發者獲得加密後的字串可與signature對比,標識該請求來源於微信 if (signature.equals(sign)) { response.getWriter().print(echostr); } } }
弄好後,在頁面填寫url跟token點選“提交”會有提示配置成功。
(提交之前)
(提交之後)
3、JS介面安全域名
在公眾號管理頁面,點選“公眾號設定”,點選“功能設定”tab頁,找到“JS介面安全域名”,點選“設定”
在設定頁面中,注意檢視設定要求,需要下載MP_verify_EZhQkscUv44pvLNO.txt檔案到Web伺服器到根目錄下。
設定完畢點選儲存,這樣就JS介面安全域名就設定完畢啦。
4、獲取accesstoken
Accesstoken有有效時間限制的,如果過期啦要重新獲取,而且請求獲取accesstoken的介面呼叫次數也有限制。所以我們必須要把每次accesstoken
參考:http://blog.csdn.net/hzw2312/article/details/69524466
5、建立選單
找到“自定義選單”下的“自定義選單建立介面”:
注意:
建立前請檢視微信的選單建立定義,只能建立三個一級選單,二級菜單隻能有5個。
具體引數看文件就知道啦。我們這裡直接使用頁面最下方的工具——“使用網頁除錯工具除錯該介面”:
輸入你的
點選“檢查問題”,如果沒有問題的話,提示:Request successfulaccess_token:... Body:{ "button": [ { "name": "旅遊導覽", "sub_button": [ { "type": "view", "name": "掃一掃", "url": "http://camp.yfscms.com/wechat/order/parkingPage.wx" }, { "type": "view", "name": "位置服務", "url": "http://camp.yfscms.com/wechat/location/page.wx" }, { "type": "view", "name": "投訴建議", "url": "http://camp.yfscms.com/wechat/order/suggestPage.wx" }, { "type": "view", "name": "民意調查", "url": "http://camp.yfscms.com/wechat/qaList/page.htm" } ] }, { "name": "旅遊服務", "sub_button": [ { "type": "view", "name": "導遊預約", "url": "http://camp.yfscms.com/wechat/guide/list.wx" }, { "type": "view", "name": "車位預定", "url": "http://camp.yfscms.com/wechat/order/parkingPage.wx" }, { "type": "view", "name": "團隊預定", "url": "http://camp.yfscms.com/wechat/order/teamPage.wx" }, { "type": "view", "name": "活動申報", "url": "http://camp.yfscms.com/wechat/order/activityPage.wx" } ] }, { "name": "集中營", "sub_button": [ { "type": "view", "name": "集中營介紹", "url": "http://camp.yfscms.com/wechat/camp/campInfo.wx" }, { "type": "view", "name": "英烈介紹", "url": "http://camp.yfscms.com/wechat/camp/heroList.wx" }, { "type": "view", "name": "聯絡我們", "url": "http://camp.yfscms.com/wechat/camp/contact.wx" } ] } ] }
如果返回介面中的errcode不等於0,可以檢視“全域性返回碼說明”——
至此選單建立完畢,可以掃一掃你的測試號,看看選單是否完成,注意選單的個數限制問題,微信選單個數是有限制的!具體看官方的文件!