微信开放平台调用获取预授权码错误,40066 invalid url?
发布于 5 年前 作者 huping 14645 次浏览 来自 问答

我在获取到微信推送的component_verify_ticket之后,按照如下文档的说明,调用API来获取预授权码:https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/api/pre_auth_code.html

对应的调用代码如下:

private static final String WECHAT_URL = "https://api.weixin.qq.com/cgi-bin/component/";
public PreauthCodeInfo getPreauthCode(String componentAccessToken, String appId) {
  var requestMap = Map.of("component_appid", appId);
  try {
    var requestString = mapper.writeValueAsString(requestMap);
    var body = RequestBody.create(requestString, MediaType.parse("application/json"));
    var urlString = UriComponentsBuilder.fromUriString(WECHAT_URL + "api_create_preauthcode")
        .queryParam("component_access_token", componentAccessToken)
        .toUriString();
    var request = new Request.Builder().url(urlString).post(body).build();
    var response = client.newCall(request).execute();
    var responseString = response.body().string();
    return mapper.readValue(responseString, PreauthCodeInfo.class);
  } catch (IOException e) {
    logger.error("error in getting preauth code: ", e);
    throw new RuntimeException(e);
  }
}

返回的结果如下:
{"errcode":40066,"errmsg":"invalid url rid: 5f9623b0-3a5e636c-481a8ee8"}
回到顶部