微信網頁授權獲取code值回調兩次的問題
1.說是域名原因,目前未測試,沒有正確的域名
- 問題描述:在調用微信網頁授權獲取openid值時,先獲取的code值,但是code值的接口 會走兩次回調。而code在6分鍾內只能用一次,所以處出現code失效的問題,問題顯示錯誤碼:{‘errcode’:40029,’errmsg’:’invalid code, hints: [ req_id: 0407ns44 ]’}
- 解決辦法: 出現這個問題是因為域名的問題,本人先使用的花生殼的內網穿透,但是花生殼的免費域名應用的是第三方代理域名,所以在向微信服務器發送請求的時候,微信回調時,會認為你的域名請求不一致,會回調兩次,重定向你的服務器兩次,只需更改正式域名即可。就會回調一次。(網上說的返回值結束二次回調,和301重定向 都是坑人的,折騰一天還是域名問題
2.說需要一個參數 &connect_redirect=1,這個是解決40029的錯誤
1 //實際使用生成url的代碼 <br>string UrlUserInfo = OAuthApi.GetAuthorizeUrl(AppId, 2 "http://2a20h48668.imwork.net/weixin/UserInfoCallback?returnUrl=" + returnUrl.UrlEncode(), 3 state, OAuthScope.snsapi_userinfo); 4 // 摘要: 5 // 獲取驗證地址的API,以及參數說明 6 // 7 // 參數: 8 // appId: 9 // 公眾號的唯一標識 10 // 11 // redirectUrl: 12 // 授權后重定向的回調鏈接地址,請使用urlencode對鏈接進行處理 13 // 14 // state: 15 // 重定向后會帶上state參數,開發者可以填寫a-zA-Z0-9的參數值,最多128字節 16 // 17 // scope: 18 // 應用授權作用域,snsapi_base (不彈出授權頁面,直接跳轉,只能獲取用戶openid),snsapi_userinfo (彈出授權頁面,可通過openid拿到昵稱、性別、所在地。並且,即使在未關注的情況下,只要用戶授權,也能獲取其信息) 19 // 20 // responseType: 21 // 返回類型,請填寫code(或保留默認) 22 // 23 // addConnectRedirect: 24 // 加上后可以解決40029-invalid code的問題(測試中) 25 public static string GetAuthorizeUrl(string appId, string redirectUrl, string state, OAuthScope scope, string responseType = "code", bool addConnectRedirect = true);
最終網址結果
https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxd84d9cb4875236c9&redirect_uri=http%3A%2F%2F2a20h48668.imwork.net%2Fweixin%2FUserInfoCallback%3FreturnUrl%3D%252FWeixinJSSDK%252Findex&response_type=code&scope=snsapi_userinfo&state=JeffreySu-954&connect_redirect=1#wechat_redirect
3.訪問的地址是
1 /// <summary> 2 /// OAuthScope.snsapi_userinfo方式回調 3 /// </summary> 4 /// <param name="code"></param> 5 /// <param name="state"></param> 6 /// <param name="returnUrl">用戶最初嘗試進入的頁面</param> 7 /// <returns></returns> 8 public ActionResult UserInfoCallback(string code, string state, string returnUrl) 9 { 10 if (string.IsNullOrEmpty(code)) 11 { 12 return Content("您拒絕了授權!"); 13 } 14 var orginState = data.getState(); 15 16 if (state != orginState) 17 { 18 //這里的state其實是會暴露給客戶端的,驗證能力很弱,這里只是演示一下, 19 //建議用完之后就清空,將其一次性使用 20 //實際上可以存任何想傳遞的數據,比如用戶ID,並且需要結合例如下面的Session["OAuthAccessToken"]進行驗證 21 return Content("驗證失敗!請從正規途徑進入!"); 22 } 23 24 OAuthAccessTokenResult result = null; 25 26 //通過,用code換取access_token 27 try 28 { 29 result = OAuthApi.GetAccessToken(AppId, AppSecret, code); 30 } 31 catch (Exception ex) 32 { 33 return Content(ex.Message); 34 } 35 if (result.errcode != ReturnCode.請求成功) 36 { 37 return Content("錯誤:" + result.errmsg); 38 } 39 //下面2個數據也可以自己封裝成一個類,儲存在數據庫中(建議結合緩存) 40 //如果可以確保安全,可以將access_token存入用戶的cookie中,每一個人的access_token是不一樣的 41 HttpContext.Session.SetString("OAuthAccessTokenStartTime", DateTime.Now.ToString()); 42 HttpContext.Session.SetString("OAuthAccessToken", result.ToJson()); 43 44 //因為第一步選擇的是OAuthScope.snsapi_userinfo,這里可以進一步獲取用戶詳細信息 45 try 46 { 47 if (!string.IsNullOrEmpty(returnUrl)) 48 { 49 return Redirect(returnUrl); 50 } 51 52 OAuthUserInfo userInfo = OAuthApi.GetUserInfo(result.access_token, result.openid); 53 return View(userInfo); 54 } 55 catch (ErrorJsonResultException ex) 56 { 57 return Content(ex.Message); 58 } 59 }
//建議將result存入數據庫中,確保值訪問一次