{"id":1760,"date":"2023-03-26T21:37:40","date_gmt":"2023-03-26T13:37:40","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1760"},"modified":"2023-04-23T21:25:02","modified_gmt":"2023-04-23T13:25:02","slug":"enterprise-wechat-java-api-access","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/26\/enterprise-wechat-java-api-access\/","title":{"rendered":"\u4f01\u4e1a\u5fae\u4fe1Java API\u63a5\u5165"},"content":{"rendered":"<h2>\u76f8\u5173\u8d44\u6e90<\/h2>\n<p>\u5165\u95e8\u6587\u6863\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/work.weixin.qq.com\/api\/doc#90000\/90003\/90487\">https:\/\/work.weixin.qq.com\/api\/doc#90000\/90003\/90487<\/a><br \/>\n\u670d\u52a1\u7aefAPI\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/work.weixin.qq.com\/api\/doc#90000\/90135\/90664\">https:\/\/work.weixin.qq.com\/api\/doc#90000\/90135\/90664<\/a><br \/>\n\u5ba2\u6237\u7aefAPI\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/work.weixin.qq.com\/api\/doc#90000\/90136\/90289\">https:\/\/work.weixin.qq.com\/api\/doc#90000\/90136\/90289<\/a><br \/>\n\u5f00\u53d1\u8005\u5de5\u5177\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/work.weixin.qq.com\/api\/doc#90000\/90138\/90678\">https:\/\/work.weixin.qq.com\/api\/doc#90000\/90138\/90678<\/a><br \/>\n\u63a5\u53e3\u8c03\u8bd5\u5de5\u5177\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/work.weixin.qq.com\/api\/devtools\/devtool.php\">https:\/\/work.weixin.qq.com\/api\/devtools\/devtool.php<\/a><br \/>\nWxJava &#8211; \u5fae\u4fe1\u5f00\u53d1 Java SDK\uff08\u5f00\u53d1\u5de5\u5177\u5305\uff09\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/Wechat-Group\/WxJava\">https:\/\/github.com\/Wechat-Group\/WxJava<\/a><br \/>\n\u7fa4\u673a\u5668\u4eba\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/work.weixin.qq.com\/help?person_id=1&amp;doc_id=13376\">https:\/\/work.weixin.qq.com\/help?person_id=1&#038;doc_id=13376<\/a><\/p>\n<p><!-- more --><\/p>\n<h2>\u57fa\u672c\u914d\u7f6e<\/h2>\n<pre><code class=\"language-xml\">&lt;xml&gt;\n    &lt;corpId&gt;xxx&lt;\/corpId&gt;\n    &lt;corpSecret&gt;xxx&lt;\/corpSecret&gt;\n    &lt;agentId&gt;1000002&lt;\/agentId&gt;\n    &lt;userId&gt;@all&lt;\/userId&gt;\n    &lt;departmentId&gt;1&lt;\/departmentId&gt;\n&lt;\/xml&gt;<\/code><\/pre>\n<pre><code class=\"language-java\">@Slf4j\n@Component\npublic class WxCpConfig {\n\n    private static final String TEST_CONFIG_XML = &quot;wx-cp.config.xml&quot;;\n\n    private WxCpService wxService;\n    private WxXmlCpInMemoryConfigStorage configStorage;\n\n    private static &lt;T&gt; T fromXml(Class&lt;T&gt; clazz, InputStream is) {\n        XStream xstream = XStreamInitializer.getInstance();\n        xstream.addPermission(AnyTypePermission.ANY);\n        xstream.alias(&quot;xml&quot;, clazz);\n        xstream.processAnnotations(clazz);\n        return (T) xstream.fromXML(is);\n    }\n\n    @PostConstruct\n    public void init() {\n        try (InputStream inputStream = ClassLoader.getSystemResourceAsStream(TEST_CONFIG_XML)) {\n            if (inputStream == null) {\n                throw new RuntimeException(&quot;\u6d4b\u8bd5\u914d\u7f6e\u6587\u4ef6\u3010&quot; + TEST_CONFIG_XML + &quot;\u3011\u672a\u627e\u5230\uff0c\u8bf7\u53c2\u7167test-config-sample.xml\u6587\u4ef6\u751f\u6210&quot;);\n            }\n\n            configStorage = fromXml(WxXmlCpInMemoryConfigStorage.class, inputStream);\n            wxService = new WxCpServiceImpl();\n            wxService.setWxCpConfigStorage(configStorage);\n        } catch (IOException e) {\n            log.error(e.getMessage(), e);\n        }\n    }\n\n    @XStreamAlias(&quot;xml&quot;)\n    public static class WxXmlCpInMemoryConfigStorage extends WxCpDefaultConfigImpl {\n\n        protected String userId;\n\n        protected String departmentId;\n\n        protected String tagId;\n\n        protected String externalUserId;\n\n        public String getUserId() {\n            return this.userId;\n        }\n\n        public void setUserId(String userId) {\n            this.userId = userId;\n        }\n\n        public String getDepartmentId() {\n            return this.departmentId;\n        }\n\n        public void setDepartmentId(String departmentId) {\n            this.departmentId = departmentId;\n        }\n\n        public String getTagId() {\n            return this.tagId;\n        }\n\n        public void setTagId(String tagId) {\n            this.tagId = tagId;\n        }\n\n        public String getExternalUserId() {\n            return externalUserId;\n        }\n\n        public void setExternalUserId(String externalUserId) {\n            this.externalUserId = externalUserId;\n        }\n\n        @Override\n        public String toString() {\n            return super.toString() + &quot; &gt; WxXmlCpConfigStorage{&quot; +\n                    &quot;userId=&#039;&quot; + this.userId + &#039;\\&#039;&#039; +\n                    &quot;, departmentId=&#039;&quot; + this.departmentId + &#039;\\&#039;&#039; +\n                    &quot;, tagId=&#039;&quot; + this.tagId + &#039;\\&#039;&#039; +\n                    &quot;, externalUserId=&#039;&quot; + this.externalUserId + &#039;\\&#039;&#039; +\n                    &#039;}&#039;;\n        }\n    }\n\n    public WxCpService getWxService() {\n        return wxService;\n    }\n}<\/code><\/pre>\n<h2>API\u8c03\u7528<\/h2>\n<pre><code class=\"language-java\">@Slf4j\n@Service\npublic class WxCpCoreService {\n\n    @Autowired\n    public WxCpConfig wxCpConfig;\n\n    @SuppressWarnings(&quot;unchecked&quot;)\n    public WxCpResponse sendMessage(WxCpRequest wxCpRequest) {\n        try {\n            WxCpService wxCpService = wxCpConfig.getWxService();\n            WxCpConfig.WxXmlCpInMemoryConfigStorage configStorage = (WxCpConfig.WxXmlCpInMemoryConfigStorage) wxCpService.getWxCpConfigStorage();\n\n            String before = configStorage.getAccessToken();\n            log.info(&quot;before: &quot; + before);\n            wxCpService.getAccessToken(false);\n            String after = configStorage.getAccessToken();\n            log.info(&quot;after: &quot; + after);\n\n            WxCpDepartmentService wxCpDepartmentService = wxCpService.getDepartmentService();\n            List&lt;WxCpDepart&gt; departList = wxCpDepartmentService.list(1L);\n            if (departList != null &amp;&amp; departList.size() &gt; 0) {\n                for (int i = 0; i &lt; departList.size(); i++) {\n                    log.info(&quot;Depart id: {}&quot;, departList.get(i).getId());\n                }\n            }\n\n            WxCpUserService wxCpUserService = wxCpService.getUserService();\n            List&lt;WxCpUser&gt; userList = wxCpUserService.listByDepartment(1L, true, 0);\n            if (userList != null &amp;&amp; userList.size() &gt; 0) {\n                for (int i = 0; i &lt; userList.size(); i++) {\n                    log.info(&quot;User id: {}&quot;, userList.get(i).getUserId());\n                }\n            }\n\n\/\/            WxCpMessage message = WxCpMessage\n\/\/                    .TEXT()\n\/\/                    .toParty(configStorage.getDepartmentId())\n\/\/                    .toUser(configStorage.getUserId())\n\/\/                    .content(&quot;\u6b22\u8fce\u6b22\u8fce\uff0c\u70ed\u70c8\u6b22\u8fce\\n\u6362\u884c\u6d4b\u8bd5\\n\u8d85\u94fe\u63a5:&lt;a href=\\&quot;http:\/\/www.appblog.cn\\&quot;&gt;Hello World&lt;\/a&gt;&quot;)\n\/\/                    .build();\n\n            WxCpMessage message = new WxCpMessage();\n            message.setMsgType(WxConsts.KefuMsgType.MARKDOWN);\n            message.setToParty(configStorage.getDepartmentId());\n            message.setToUser(configStorage.getUserId());\n            message.setContent(&quot;\u60a8\u7684\u4f1a\u8bae\u5ba4\u5df2\u7ecf\u9884\u5b9a\uff0c\u7a0d\u540e\u4f1a\u540c\u6b65\u5230`\u90ae\u7bb1` \\n&quot; +\n                    &quot;                &gt;**\u4e8b\u9879\u8be6\u60c5** \\n&quot; +\n                    &quot;                &gt;\u4e8b\u3000\u9879\uff1a&lt;font color=\\\\\\&quot;info\\\\\\&quot;&gt;\u5f00\u4f1a&lt;\/font&gt; \\n&quot; +\n                    &quot;                &gt;\u7ec4\u7ec7\u8005\uff1a@Joe.Ye \\n&quot; +\n                    &quot;                &gt;\u53c2\u4e0e\u8005\uff1a@Joe.Ye\u3001@AppBlog.CN\u3001@China\u3001@HangZhou\u3001@ShangHai \\n&quot; +\n                    &quot;                &gt; \\n&quot; +\n                    &quot;                &gt;\u4f1a\u8bae\u5ba4\uff1a&lt;font color=\\\\\\&quot;info\\\\\\&quot;&gt;\u676d\u5dde\u56fd\u9645\u4f1a\u8bae\u4e2d\u5fc3 1\u697c 101&lt;\/font&gt; \\n&quot; +\n                    &quot;                &gt;\u65e5\u3000\u671f\uff1a&lt;font color=\\\\\\&quot;warning\\\\\\&quot;&gt;2019\u5e7411\u670830\u65e5&lt;\/font&gt; \\n&quot; +\n                    &quot;                &gt;\u65f6\u3000\u95f4\uff1a&lt;font color=\\\\\\&quot;comment\\\\\\&quot;&gt;\u4e0a\u53489:00-11:00&lt;\/font&gt; \\n&quot; +\n                    &quot;                &gt; \\n&quot; +\n                    &quot;                &gt;\u8bf7\u51c6\u65f6\u53c2\u52a0\u4f1a\u8bae\u3002 \\n&quot; +\n                    &quot;                &gt; \\n&quot; +\n                    &quot;                &gt;\u5982\u9700\u4fee\u6539\u4f1a\u8bae\u4fe1\u606f\uff0c\u8bf7\u70b9\u51fb\uff1a[\u4fee\u6539\u4f1a\u8bae\u4fe1\u606f](https:\/\/work.weixin.qq.com)&quot;);\n\n            WxCpMessageSendResult messageSendResult = wxCpService.messageSend(message);\n            log.info(&quot;message: {}&quot;, JSON.toJSONString(message));\n            log.info(&quot;WxCpCoreService.sendMessage messageSendResult: &quot; + JSON.toJSONString(messageSendResult));\n            return new WxCpResponse();\n        } catch (Exception e) {\n            log.error(&quot;WxCpCoreService.sendMessage exception: &quot;, e);\n            return null;\n        }\n    }\n\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u76f8\u5173\u8d44\u6e90 \u5165\u95e8\u6587\u6863\uff1ahttps:\/\/work.weixin.qq.com\/api\/doc#90000\/9000 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[392],"class_list":["post-1760","post","type-post","status-publish","format-standard","hentry","category-spring-boot","tag-392"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1760","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1760"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1760\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1760"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1760"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1760"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}