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;

        }

  • 相关阅读:
    正向代理/反向代理理解、Nginx概述、安装及配置详解
    项目部署问题:xftp无法连接服务器、Nginx403 Forbidden解决、nginx反向代理解决前端跨域问题
    Vue上传文件:ElementUI中的upload实现
    理解Vue的计算属性
    今天在CSDN看懂这个帖子,也是我的困惑,记录一下(过了三十的码农,你选择的是哪个,说出你的想法)
    WCF IIS上部署服务
    [转]WCF RESTful service and WebGrid in ASP.NET MVC 5
    WCF 与其它技术的比较
    Visual Studio Debug和Release的区别及obj的作用
    C# Json格式字符串
  • 原文地址:https://www.cnblogs.com/attilax/p/15197353.html
Copyright © 2011-2022 走看看