1. 程式人生 > >C# 呼叫微信公眾號介面獲取會員資訊示例

C# 呼叫微信公眾號介面獲取會員資訊示例

公眾號獲取使用者資訊,比較簡單指定Post請求,立刻獲得。

1.使用者資訊定義

    /// <summary>
    /// 授權之後獲取使用者基本資訊
    /// </summary>
    public class OAuthUser
    {
        public string openid { get; set; }
        public string nickname { get; set; }
        public int sex { get; set; }
        public string province { get; set; }
        public string city { get; set; }
        public string country { get; set; }
        public string headimgurl { get; set; }
        /// <summary>
        /// 使用者特權資訊,json 陣列
        /// </summary>
        public JArray privilege { get; set; }
        public string unionid { get; set; }
    }

2.執行Post獲取資訊

/// <summary>
///獲取基本資訊
/// </summary>
public OAuthUser GetUserInfo(string openid)
{
    //1.傳送get請求
    string url = string.Format("https://api.weixin.qq.com/cgi-bin/user/info?access_token={0}&openid={1}&lang=zh_CN",
    this.config.Access_Token,
    openid);
    string result = NetHelper.Get(url);
    if (result.Contains("errcode"))
        throw new Exception("獲取使用者資訊失敗:" + result + " | 請求url=" + url);
    //2.解析結果
    OAuthUser user = JsonConvert.DeserializeObject<OAuthUser>(result);
    if (user == null)
        throw new Exception("反序列化結果失敗,返回內容:" + result);
    return user;
}


更多: