Ejemplo n.º 1
0
 /// <summary>
 /// 公众号可以使用AppID和AppSecret调用本接口来获取access_token。AppID和AppSecret可在微信公众平台官网-开发者中心页中获得(需要已经成为开发者,且帐号没有异常状态)。注意调用所有微信接口时均需使用https协议。
 /// </summary>
 /// <param name="grant_type">获取access_token填写client_credential</param>
 /// <param name="appid">第三方用户唯一凭证</param>
 /// <param name="secret">第三方用户唯一凭证密钥,即appsecret</param>
 /// <returns>正常情况下,微信会返回下述JSON数据包给公众号{"access_token":"ACCESS_TOKEN","expires_in":7200},错误时微信会返回错误码等信息,JSON数据包示例如下(该示例为AppID无效错误):{"errcode":40013,"errmsg":"invalid appid"}</returns>
 public BaseMsg getAccessToken(string grant_type, string appid, string secret)
 {
     try
     {
         String url = string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type={0}&appid={1}&secret={2}", grant_type, appid, secret);
         RequestSimulator Request = new RequestSimulator();
         String jsonStr = Request.Get(url);
         ErrorCode errorCode = MsgFilter.filter(jsonStr);
         if (errorCode.errcode!=0) {
             return errorCode;
         }
         AccessToken accessToken = JsonHelper.parse<AccessToken>(jsonStr);
         return accessToken;
     }
     catch (Exception ex) {
         throw ex;
     }
 }
Ejemplo n.º 2
0
 public BaseMsg getWxUserByOauth(string access_token, string openid) {
     try
     {
         String url = string.Format("https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}&lang=zh_CN", access_token, openid);
         RequestSimulator Request = new RequestSimulator();
         String jsonStr = Request.Get(url);
         ErrorCode errorCode = MsgFilter.filter(jsonStr);
         if (errorCode.errcode != 0)
         {
             return errorCode;
         }
         WxUser obj = JsonHelper.parse<WxUser>(jsonStr);
         return obj;
     }
     catch (Exception ex)
     {
         throw ex;
     }
 
 }