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


//根據微信提供的接口,請求得到openid和session_id
public class UserInfoUtils {
 
  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 {
 
  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賦值了。
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM