Exemple #1
0
 /*
  * 自动群发的图文消息
  */
 public static ArticleTextArr makeNews(Items[] items, String imgId) throws IOException {
   ArticleTextArr atArr = new ArticleTextArr();
   // String filePath = "../../111.jpg";
   String accessToken = WeixinUtil.getExitAccessToken().getToken();
   // String thumb_media_id =
   // "http://mmbiz.qpic.cn/mmbiz/joqDZc3wChsj5vGhCguzn52oMmZvtic1Kf20ZagTXicsT4A39DhmFuXx7JvV6ELMBuxSVGic2PxJcq7UC9eCemicAA/0";
   // String imgId =
   // "http://mmbiz.qpic.cn/mmbiz/joqDZc3wChsx5G4ySPOtibK0fL5iaCYicNYMpmLvZ1V8zlpPdJhZ70RDQwzMzm4ZGSLBRc2px3hQx6hicNOg5c7TzQ/0";
   List<ArticleText> articleList = new ArrayList<ArticleText>();
   for (int i = 0; i < 3; i++) {
     if (i > 3) break;
     ArticleText at = new ArticleText();
     at.setAuthor("小二");
     at.setContent(items[i].getContent());
     // String uploadImgId = SpamUtil.uploadImg(items[i].getThumbnail(), accessToken);
     at.setThumb_media_id(imgId);
     // at.setThumb_media_id(thumb_media_id );
     at.setTitle(items[0].getHeader());
     at.setContent_source_url(items[i].getFullTextUrl());
     at.setShow_cover_pic(1);
     articleList.add(at);
   }
   atArr.setArticles(articleList);
   return atArr;
 }
Exemple #2
0
  /**
   * 获取网页授权凭证
   *
   * @param appId 公众号的唯一标识
   * @param appSecret 公众号的秘钥
   * @param code
   * @return WeixinAouth2Token
   */
  public static WeixinOauth2Token getOauth2AccessToken(
      String appId, String appSecret, String code) {

    WeixinOauth2Token wat = null;
    String requestUrl =
        "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
    requestUrl = requestUrl.replace("APPID", appId);
    requestUrl = requestUrl.replace("SECRET", appSecret);
    requestUrl = requestUrl.replace("CODE", code);

    JSONObject jsonObject = WeixinUtil.httpRequest(requestUrl, "GET", null);
    if (null != jsonObject) {
      try {
        wat = new WeixinOauth2Token();
        wat.setAccessToken(jsonObject.getString("access_token"));
        wat.setExpiresIn(jsonObject.getInt("expires_in"));
        wat.setOpenId(jsonObject.getString("openid"));
        wat.setRefreshToken(jsonObject.getString("refresh_token"));
        wat.setScope(jsonObject.getString("scope"));
        log.info(
            "获取网页授权凭证成功 access_token:{} errorMsg:{}",
            jsonObject.getString("access_token"),
            jsonObject.getString("openid"));
      } catch (Exception e) {
        wat = null;
        int errorCode = jsonObject.getInt("errcode");
        String errorMsg = jsonObject.getString("errmsg");
        log.error("获取网页授权凭证失败 errorCode:{} openid:{}", errorCode, errorMsg);
      }
    }

    return wat;
  }
Exemple #3
0
  /**
   * 通过网页凭证获取用户信息
   *
   * @param access_token 网页凭证
   * @param Openid 用户在公众号的唯一标识
   * @return SNSUserInfo
   */
  public static SNSUserInfo getSNSUserInfo(String access_token, String openid) {
    SNSUserInfo sui = null;
    String url =
        "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";
    url = url.replace("ACCESS_TOKEN", access_token);
    url = url.replace("OPENID", openid);

    JSONObject jso = WeixinUtil.httpRequest(url, "GET", null);

    if (jso != null) {
      try {
        sui = new SNSUserInfo();
        sui.setCity(jso.getString("city"));
        sui.setCountry(jso.getString("country"));
        String headimgurl = jso.getString("headimgurl");
        // 默认头像是0,即640*640像素,改成96像素
        headimgurl = headimgurl.substring(0, headimgurl.length() - 1) + "96";
        sui.setHeadimgurl(headimgurl);
        sui.setNickName(jso.getString("nickname"));
        sui.setOpenId(jso.getString("openid"));
        sui.setPrivilege(jso.getString("privilege"));
        sui.setProvince(jso.getString("province"));
        if (jso.getString("sex").equals("1")) {
          sui.setSex("男");
        } else if (jso.getString("sex").equals("2")) {
          sui.setSex("女");
        } else {
          sui.setSex("未知");
        }

        //				sui.setUnionId(jso.getString("unionid"));
      } catch (Exception e) {
        sui = new SNSUserInfo();
        sui.setCity(e.getMessage().toString());
        sui.setCountry("empty--");
        sui.setHeadimgurl(e.getStackTrace().toString());
        sui.setNickName(e.getStackTrace().toString());
        sui.setOpenId("empty--");
        sui.setPrivilege("empty--");
        sui.setProvince("empty--");
        sui.setSex("empty--");
        //				sui.setUnionId("empty--");
        log.info("通过网页授权获取用户信息失败!");
      }
    } else {
      sui = new SNSUserInfo();
      sui.setCity("empty");
      sui.setCountry("empty");
      sui.setHeadimgurl("empty");
      sui.setNickName("empty");
      sui.setOpenId("empty");
      sui.setPrivilege("empty");
      sui.setProvince("empty");
      sui.setSex("empty");
      //			sui.setUnionId("empty");
    }
    return sui;
  }
  /** @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType("application/json;charset=UTF-8");
    String code = request.getParameter("code");

    //	    PrintStream ps=new PrintStream("/opt/scf/logs/admin/1log.txt");
    //        System.setOut(ps);

    String url =
        "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx8cc813db338acc33&secret=8c86bbf68906e0ddd6777e084c727d3e&code="
            + code
            + "&grant_type=authorization_code";
    JSONObject jsonObject = WeixinUtil.httpRequest(url, "GET", null);
    PrintWriter out = response.getWriter();
    out.write(jsonObject.toString());
    out.close();
  }