1. 程式人生 > >小程序獲取openid 出現null,{"errcode":40163,"errmsg":"code been used, hints: [ req_id: WNUzlA0105th41 ]"}

小程序獲取openid 出現null,{"errcode":40163,"errmsg":"code been used, hints: [ req_id: WNUzlA0105th41 ]"}

str 但是 清除緩存 ssi get 除了 user pri mat

//根據微信提供的接口,請求得到openid和session_id public class UserInfoUtils {   private String getKeyURL="https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code";   public String getKey(String appid, String secret, String code) {     getKeyURL=String.format(getKeyURL,appid,secret,code);     String respone = MyHttpsUtil.httpsRequest(getKeyURL, "GET", null);     return respone;   } } 相同code,在請求微信接口時,只能請求一次,再次請求就會返回openid為null的錯誤:{"errcode":40163,"errmsg":"code been used, hints: [ req_id: WNUzlA0105th41 ]"} 測試時在微信開發工具上清除緩存後,重新請求就可以了。 但是測試時還有一個問題: public class UserInfoUtils {   private static
String getKeyURL="https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code";   public static String getKey(String appid, String secret, String code) {   getKeyURL=String.format(getKeyURL,appid,secret,code);   String respone = MyHttpsUtil.httpsRequest(getKeyURL, "GET", null);   return respone;   } } 就是我在URL和方法上都加了static關鍵字,調用靜態方法時,就算清除了緩存,code也不一樣了,但還是會報錯:{"errcode":40163,"errmsg":"code been used, hints: [ req_id: WNUzlA0105th41 ]"}。 解決方法:將static關鍵字去掉就ok了,因為靜態變量會保存getKeyURL,之後調用方法獲得的值是上次轉換後的值,%s被轉換成具體的值,之後調用format方法就不能給URL賦值了。

小程序獲取openid 出現null,{"errcode":40163,"errmsg":"code been used, hints: [ req_id: WNUzlA0105th41 ]"}