zoukankan      html  css  js  c++  java
  • Atitit 微信开发文档总结获取token和菜单管理功能 /bookmarksHtmlEverythingIndexPrj/src/com/attilax/wechatToto/wechatMen

    Atitit 微信开发文档总结获取token和菜单管理功能

     

    /bookmarksHtmlEverythingIndexPrj/src/com/attilax/wechatToto/wechatMenuScript.java

     

    package com.attilax.wechatToto;

     

    import java.io.File;

    import java.io.FileNotFoundException;

    import java.text.SimpleDateFormat;

     

    import org.apache.commons.io.FileUtils;

    import org.apache.commons.net.nntp.NewGroupsOrNewsQuery;

    import org.apache.http.auth.AuthenticationException;

     

    import com.alibaba.fastjson.JSONObject;

     

    public class wechatMenuScript {

     

    // private static final

     

    public static void main(String[] args) throws Exception {

     

    String token;

     

    try {

    token = WechatUtil.getTokenFromFile("g:\\token.txt");

    WechatUtil.tokenIsOk(token);

    } catch (FileNotFoundException | AuthenticationException e) {

     

    WXAuthUtil.APPID = " aaa" + "";

    WXAuthUtil.APPSECRET = "aaaa";

     

    JSONObject jsonObject = WXAuthUtil.getAccessToken();

    System.out.println(jsonObject);

    jsonObject.put("gettime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date()));// );

    FileUtils.write(new File("g:\\token.txt"), jsonObject.toJSONString());

    // String access_token= jsonObject.getString("access_token")

    token = jsonObject.getString("access_token");

    }

     

    // System.out.println(WXAuthUtil.getAccessToken());

     

    // String

    // token="20_NzEqY5k1bj64tguXl3oZb-QkVy4WcGowYf7R8ERXcu3raN2lVYv_xz_njX_ecp4T81VBceqPr0feNxE5rfs3xb_pe_loPik4AXLv30dQlSediaLj6_bq9y5ak4Rg3_59mtmikF15badSVCozSYPiAJANTK";

    //

    // {"access_token":"20_NzEqY5k1bj64tguXl3oZb-QkVy4WcGowYf7R8ERXcu3raN2lVYv_xz_njX_ecp4T81VBceqPr0feNxE5rfs3xb_pe_loPik4AXLv30dQlSediaLj6_bq9y5ak4Rg3_59mtmikF15badSVCozSYPiAJANTK","expires_in":7200}

    JSONObject MENU = new MenuSeviceImpl().getMenu(token);

    FileUtils.write(new File("g:\\wechat\\preTotoIuwen\\menu.json"), MENU.toJSONString(MENU, true));

    System.out.println(MENU);

    }

     

    }

     

     

     

    public static String getTokenFromFile(String string) throws IOException {

     

    String t=FileUtils.readFileToString(new File("g:\\token.txt"));

    JSONObject jsonObject =JSONObject.parseObject(t);

    return jsonObject.getString("access_token");

    }

     

     

    public static boolean tokenIsOk(String token) throws AuthenticationException {

    System.out.println("WechatUtil.tokenIsOk");

    System.out.println( new MenuSeviceImpl().getMenu(token));

    return true;

    }

     

     

        public static JSONObject getAccessToken() throws Exception {

         String appid =APPID;

         String appsecret = APPSECRET;

          

     

            System.out.println("eric.appid:"+appid);

            System.out.println("eric.secret:"+appsecret);

            log.debug("eric.appid:"+appid);

            log.debug("eric.secret:"+appsecret);

            

            String requestUrl = access_token_url.replace("APPID", appid).replace("APPSECRET", appsecret);

            log.info(requestUrl);

            JSONObject jsonObject = HttpUtil.httpRequest(requestUrl, "GET", null);

            // 如果请求成功

            if (null != jsonObject) {

               if(jsonObject.getString("access_token")==null)

                    throw new RuntimeException(jsonObject.toJSONString());

                    // 获取token失败

                  //  log.error("获取token失败 errcode:{} errmsg:{}", jsonObject.getInteger("errcode"), jsonObject.getString("errmsg"));

                

            }

            return jsonObject;

    }

     

     

     

    /**implements MenuService

     * 对订阅号的菜单的操作

     */

    @SuppressWarnings("SpringJavaAutowiringInspection")

     

    public class MenuSeviceImpl  {

     

        //@Autowired

    //    private ISysConfigService iSysConfigService;

     

        private static Logger log = LoggerFactory.getLogger(MenuSeviceImpl.class);

     

        // 菜单创建(POST) 限1000(次/天)

        public static String menu_create_url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN";

     

        // 菜单查询(POST) 限10000(次/天)

        public static String menu_get_url = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=ACCESS_TOKEN";

     

        // 菜单删除(POST) 限1000(次/天)

        public static String menu_delete_url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=ACCESS_TOKEN";

     

        /**

         * 查询菜单

         * @param accessToken 有效的access_token

         * @return

         * @throws AuthenticationException

         */

        public JSONObject getMenu(String accessToken) throws AuthenticationException {

     

            // 拼装创建菜单的url

            String url = menu_get_url.replace("ACCESS_TOKEN", accessToken);

            // 调用接口查询菜单

            JSONObject jsonObject = HttpUtil.httpRequest(url, "GET", null);

            

            if(jsonObject.getInteger("errcode")!=null)

             throw new AuthenticationException(jsonObject.toJSONString());

     

            return jsonObject;

        }

  • 相关阅读:
    挑战练习13.8 用于RecyclerView 的空视图
    挑战练习13.7 复数字符串资源
    挑战练习13.6 删除crime 记录
    挑战练习12.3 更多对话框
    20. Valid Parentheses
    挑战练习11.6 添加Jump to First按钮和Jump to Last按钮
    内存 分析
    HOOK64 32转换
    MD5
    HOOk快捷键
  • 原文地址:https://www.cnblogs.com/attilax/p/15197367.html
Copyright © 2011-2022 走看看