日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 人文社科 > 生活经验 >内容正文

生活经验

微信一次性订阅消息

發(fā)布時(shí)間:2023/11/27 生活经验 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 微信一次性订阅消息 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

微信一次性訂閱消息官方文檔:消息管理>發(fā)送一次性訂閱消息

開(kāi)發(fā)者可以通過(guò)一次性訂閱消息授權(quán)讓微信用戶授權(quán)第三方移動(dòng)應(yīng)用(接入說(shuō)明)或公眾號(hào),獲得發(fā)送一次訂閱消息給到授權(quán)微信用戶的機(jī)會(huì)。授權(quán)微信用戶可以不需要關(guān)注公眾號(hào)。微信用戶每授權(quán)一次,開(kāi)發(fā)者可獲得一次下發(fā)消息的權(quán)限。對(duì)于已關(guān)注公眾號(hào)的,消息將下發(fā)到公眾號(hào)會(huì)話;未關(guān)注公眾號(hào)的,將下發(fā)到服務(wù)通知

本篇文章主要討論公眾號(hào)

1、確認(rèn)是否有權(quán)限

已認(rèn)證的公眾號(hào)即有權(quán)限,可登陸公眾平臺(tái)在接口權(quán)限列表處查看(如下圖)。目前測(cè)試號(hào)還無(wú)法測(cè)試一次性訂閱消息

2、配置相關(guān)的參數(shù)

  • 查看AppId以及AppSecret (發(fā)送一次性訂閱消息需要ACCESS_TOKEN、獲取ACCESS_TOKEN需要使用到)
  • 配置回調(diào)域名

3、授權(quán)發(fā)送一次性訂閱消息

具體的參數(shù)看文檔這里就不詳細(xì)介紹了。

public static String getAuthorizeURL(String appId, String scene, String template_id,String redirectUri, String reserved) throws UnsupportedEncodingException {StringBuffer sbf = new StringBuffer();sbf.append(authorize_uri).append("&appid=").append(appId).append("&scene=").append(scene).append("&template_id=").append(template_id).append("&redirect_uri=").append(URLEncoder.encode(redirectUri, Charsets.UTF_8.name()).replace("+", "%20"));if (StrKit.notBlank(reserved)) {sbf.append("&reserved=").append(reserved);}sbf.append("#wechat_redirect");return sbf.toString();}

用戶同意或取消授權(quán)后會(huì)返回相關(guān)信息
如果用戶點(diǎn)擊同意或取消授權(quán),頁(yè)面將跳轉(zhuǎn)至:

redirect_url/?openid=OPENID&template_id=TEMPLATE_ID&action=ACTION&scene=SCENE

4、通過(guò)API推送訂閱模板消息給到授權(quán)微信用戶

http請(qǐng)求方式: post
https://api.weixin.qq.com/cgi-bin/message/template/subscribe?access_token=ACCESS_TOKEN

{"touser": "OPENID","template_id": "TEMPLATE_ID","url": "URL","scene": "SCENE","title": "TITLE","data": {"content": {"value": "VALUE","color": "COLOR"}}
}

具體封裝代碼如下:

/*** 發(fā)送一次性訂閱消息* * @param jsonStr*            json字符串* @return ApiResult * */public static ApiResult subscribe(String jsonStr) {String jsonResult = HttpUtils.post(subscribe + AccessTokenApi.getAccessTokenStr(), jsonStr);return new ApiResult(jsonResult);}public static ApiResult subscribe(SubscribeInfo subscribeInfo) {return new ApiResult(JsonKit.toJson(subscribeInfo));}public static ApiResult subscribe(String openId, String templateId, String url, int scene, String title,String value, String color) {SubscribeInfo subscribeInfo = new SubscribeInfo.Builder().setTouser(openId).setTemplate_id(templateId).setUrl(url).setScene(String.valueOf(scene)).setTitle(title).setData(new Data.Builder().setContent(new Content.Builder().setColor(color).setValue(value).create()).create()).create();System.out.println(JsonUtils.toJson(subscribeInfo));                return subscribe(JsonUtils.toJson(subscribeInfo));}

Builder模式構(gòu)建請(qǐng)求參數(shù)的json對(duì)象

class SubscribeInfo {private String touser;private String template_id;private String url;private String scene;private String title;private Data data;public static class Builder{private String touser;private String template_id;private String url;private String scene;private String title;private Data data;public Builder setTouser(String touser) {this.touser = touser;return this;}public Builder setTemplate_id(String template_id) {this.template_id = template_id;return this;}public Builder setUrl(String url) {this.url = url;return this;}public Builder setScene(String scene) {this.scene = scene;return this;}public Builder setTitle(String title) {this.title = title;return this;}public Builder setData(Data data) {this.data = data;return this;}public SubscribeInfo create(){    return new SubscribeInfo(this);    }}private SubscribeInfo(Builder builder) {if (StrKit.isBlank(builder.touser)) {throw new IllegalStateException("touser is null");}if (StrKit.isBlank(builder.template_id)) {throw new IllegalStateException("template_id is null");}if (StrKit.isBlank(builder.url)) {throw new IllegalStateException("url is null");}if (StrKit.isBlank(builder.scene)) {throw new IllegalStateException("scene is null");}if (StrKit.isBlank(builder.title)) {throw new IllegalStateException("title is null");}if (!StrKit.notNull(builder.data)) {throw new IllegalStateException("data is null");}this.touser = builder.touser;this.template_id = builder.template_id;this.url = builder.url;this.scene = builder.scene;this.title = builder.title;this.data = builder.data;}public String getTouser() {return touser;}public String getTemplate_id() {return template_id;}public String getUrl() {return url;}public String getScene() {return scene;}public String getTitle() {return title;}public Data getData() {return data;}
}class Data {private Content content;public static class Builder {private Content content;public Builder setContent(Content content) {this.content = content;return this;}public Data create(){    return new Data(this);    }    }private Data(Builder builder) {if (!StrKit.notNull(builder.content)) {throw new IllegalStateException("content is null");}this.content = builder.content;}public Content getContent() {return content;}}class Content {private String value;private String color;public static class  Builder{private String value;private String color;public Builder setValue(String value) {this.value = value;return this;}public Builder setColor(String color) {this.color = color;return this;}public Content create(){return new Content(this);}}private Content(Builder builder) {if (StrKit.isBlank(builder.value)) {throw new IllegalStateException("value is null");}if (StrKit.isBlank(builder.color)) {throw new IllegalStateException("color is null");}this.value = builder.value;this.color = builder.color;}public String getValue() {return value;}public String getColor() {return color;}
}

5、遺留問(wèn)題

1、授權(quán)后頁(yè)面跳轉(zhuǎn)無(wú)效redirect_url
2、發(fā)送一次性訂閱消息提示沒(méi)有權(quán)限(認(rèn)證的服務(wù)號(hào))

{"errcode":48001,"errmsg":"api unauthorized hint: [uAi6Za0855sz10!]"}

猜測(cè):應(yīng)該是官方接口存在問(wèn)題

推薦閱讀
10分鐘搭建屬于自己的ngork服務(wù)器,實(shí)現(xiàn)內(nèi)網(wǎng)穿透
極速開(kāi)發(fā)微信公眾號(hào)
IJPay讓支付觸手可及
微信、支付寶App支付

轉(zhuǎn)載于:https://www.cnblogs.com/zyw-205520/p/7349654.html

總結(jié)

以上是生活随笔為你收集整理的微信一次性订阅消息的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。