收藏
回答

Android移动应用调用一次性订阅消息出错

问题模块 框架类型 问题类型 API/组件名称 终端类型 微信版本 基础库版本
API和组件 小程序 Bug https://api.weixin.qq.com/cgi-bin/message/template/subscribe 客户端 6.6.7 implementation 'com.tencent.mm.opensdk:wechat-sdk-android-with-mta:+'

希望在微信里给APP的用户发消息

- 当前 Bug 的表现(可附上截图)


首先获取用户授权



成功后在手机上通过接口

 https://api.weixin.qq.com/cgi-bin/message/template/subscribe?access_token=13_yr-U5T9Z7sD9wK8t-b6CLlF37ptdzeeqWrqlM1_7ShilbZvJzofZyY97bqa30rg0M2MiJMHeJk0euVfexnN2M0rFCdREVo3S7S5bFKWcWzU

向微信API post数据

{"touser":"oKn3lvkut8w-H0oM2c-3Rhsqx70o",

"template_id":"k5dLFam6GtlfptCMkbR02eqIeO22oNqn8SaL1bSSuc4",

"url":"https:\/\/mp.weixin.qq.com\/s\/Ojd9nmggEPeLHHK1tJKGrQ",

"scene":1,

"title":"Hello world",

"data":{"content":{"value":"data","color":"#ff0000"}}

}


结果报错

{"errcode":40001,"errmsg":"invalid credential, access_token is invalid or not latest hint: [o8Y_mA0765vr47!]"}



不知道是什么原因?

其中openid和access_token通过接口

 String uri = String.format("https://api.weixin.qq.com/sns/auth?access_token=%s&openid=%s", weixinAccessToken, openId);

验证通过。为什么还会报错?



代码

        String verifyUri = String.format("https://api.weixin.qq.com/sns/auth?access_token=%s&openid=%s", weixinAccessToken, openId);
String subscribeUri = "https://api.weixin.qq.com/cgi-bin/message/template/subscribe?access_token=" + weixinAccessToken;

HttpsURLConnection urlConnection = null;

try {
// verify access token

           Log.d(TAG, "verify uri = " + verifyUri);
URL verifyUrL = new URL(verifyUri);
urlConnection = (HttpsURLConnection) verifyUrL.openConnection();

{
InputStream in1 = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader r = new BufferedReader(new InputStreamReader(in1));
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
total.append(line).append('\n');
}

Log.d(TAG, "verify resp: " + total.toString());
}


// subcribe wexin message
           Log.d(TAG, "subscribe uri = " + subscribeUri);
URL subscribeUrl = new URL(subscribeUri);


urlConnection = (HttpsURLConnection) subscribeUrl.openConnection();
//            urlConnection.setSSLSocketFactory(httpsClient.getSslContext().getSocketFactory());
           urlConnection.setReadTimeout(10000);
urlConnection.setConnectTimeout(15000);
urlConnection.setRequestMethod("POST");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.setUseCaches(false);
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setRequestProperty("Host", "android.schoolportal.gr");
urlConnection.connect();


// Send POST output.
           DataOutputStream printout = new DataOutputStream(urlConnection.getOutputStream());
Log.d(TAG, "post json data: " + postJson.toString());
printout.writeBytes(postJson.toString());
printout.flush();
printout.close();


InputStream in = new BufferedInputStream(urlConnection.getInputStream());

BufferedReader r = new BufferedReader(new InputStreamReader(in));
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
total.append(line).append('\n');
}

Log.d(TAG, "subscribe resp: " + total.toString());

} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}



logcat:


08-28 17:29:19.761 16784-16784/cn.com.hotcent.eyecare D/WXEntryActivity: handleIntent = true

08-28 17:29:19.761 16784-16991/cn.com.hotcent.eyecare D/HttpSubscribeWeixinMsgTask: verify uri = https://api.weixin.qq.com/sns/auth?access_token=13_K7D7F9nRL_fjy3Df9afmKW6XYCPv24d6FL-JJgojaek2qi3r7F37m2NaLOKfRaC47SUMfRIIIR9AzYND35-hdTWeSZVRIpfXi4JiGO4aWXg&openid=oKn3lvkut8w-H0oM2c-3Rhsqx70o

08-28 17:29:19.762 16784-16784/cn.com.hotcent.eyecare V/MicroMsg.SDK.WXApiImplV10.ActivityLifecycleCb: cn.com.hotcent.eyecare.wxapi.WXEntryActivity  onActivityResumed

08-28 17:29:19.802 16784-16784/cn.com.hotcent.eyecare D/ActivityThreadInjector: clearCachedDrawables.

08-28 17:29:20.291 16784-16991/cn.com.hotcent.eyecare I/RestUtilImpl: Approving certificate for api.weixin.qq.com

08-28 17:29:20.430 16784-16991/cn.com.hotcent.eyecare D/HttpSubscribeWeixinMsgTask: verify resp: {"errcode":0,"errmsg":"ok"}

    subscribe uri = https://api.weixin.qq.com/cgi-bin/message/template/subscribe?access_token=13_K7D7F9nRL_fjy3Df9afmKW6XYCPv24d6FL-JJgojaek2qi3r7F37m2NaLOKfRaC47SUMfRIIIR9AzYND35-hdTWeSZVRIpfXi4JiGO4aWXg

08-28 17:29:20.477 16784-16991/cn.com.hotcent.eyecare D/HttpSubscribeWeixinMsgTask: post json data: {"touser":"oKn3lvkut8w-H0oM2c-3Rhsqx70o","template_id":"k5dLFam6GtlfptCMkbR02eqIeO22oNqn8SaL1bSSuc4","url":"https:\/\/mp.weixin.qq.com\/s\/Ojd9nmggEPeLHHK1tJKGrQ","scene":1,"title":"Hello world","data":{"content":{"value":"data","color":"#ff0000"}}}

08-28 17:29:20.545 16784-16991/cn.com.hotcent.eyecare D/HttpSubscribeWeixinMsgTask: subscribe resp: {"errcode":40001,"errmsg":"invalid credential, access_token is invalid or not latest hint: [8mb_aa0559vr47!]"}

08-28 17:29:20.562 16784-16784/cn.com.hotcent.eyecare V/MicroMsg.SDK.WXApiImplV10.ActivityLifecycleCb: WXStat trigger onForeground


最后一次编辑于  2018-08-28  (未经腾讯允许,不得转载)
邀请回答
复制链接收藏投诉关注问题回答

2 个回答

  • 娇华
    娇华
    2018-08-28

    你好,错误信息已经提示:凭证无效,access_token无效或不是最新的

    2018-08-28
    赞同
    回复
  • 陈量
    陈量
    2018-08-28

    当然是最新的,刚刚微信登录以后获取的凭证。而且

    https://api.weixin.qq.com/sns/auth?接口验证通过
    2018-08-28
    赞同
    回复