日期:2014-05-16 浏览次数:21032 次
接着上次的理论,我们这次来研究用代码实现“网页授权获取用户基本信息”,首先我们需要一个链接指向微信的授权页面,在微信开发平台中已经说了,这个链接必须在微信客户端中打开,那么我们就干脆使用文本消息来完成吧,也就是说当我们发送“授权”两个字的时候,微信给我们一个链接,我们点击这个链接然后进入“授权页面”。首先改造一下我们OnLoad函数:
if (wx.MsgType == "text" && wx.Content == "你好")
{
res = sendTextMessage(wx, "你好,欢迎使用北京永杰友信科技有限公司公共微信平台!");
}
else if (wx.MsgType == "text" && wx.Content == "授权")
{
string MyAppid = "wx043225275885dafd";
string RedirectUri = "http://wx.4ugood.net/OAuthRedirectUri.aspx";
string URL = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + MyAppid + "&redirect_uri=" + RedirectUri + "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect" ;
string Str = "<a href='" + URL + "'>授权页面</a>";
res = sendTextMessage(wx, Str);
}
else if (wx.MsgType == "voice")
{
res = sendTextMessage(wx, wx.Recognition);
}
else if (wx.MsgType == "location")
{
res = sendTextMessage(wx, "您发送的位置是:" + wx.Label + ";纬度是:" + wx.Location_X + ";经度是:" + wx.Location_Y + ";缩放比例为:" + wx.Scale);
}
else
{
res = sendTextMessage(wx, "你好,未能识别消息!");
}
public class OAuth_Token
{
public OAuth_Token()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
//access_token 网页授权接口调用凭证,注意:此access_token与基础支持的access_token不同
//expires_in access_token接口调用凭证超时时间,单位(秒)
//refresh_token 用户刷新access_token
//openid 用户唯一标识,请注意,在未关注公众号时,用户访问公众号的网页,也会产生一个用户和公众号唯一的OpenID
//scope 用户授权的作用域,使用逗号(,)分隔
public string access_token { get; set; }
public string expires_in { get; set; }
public string refresh_token { get; set; }
public string openid { get; set; }
public string scope { get; set; }
}
public class OAuthUser
{
public OAuthUser()
{ }
#region 数据库字段
private string _openID;
private string _searchText;
private string _nickname;
private string _sex;
private string _province;
private string _city;
private string _country;
private string _headimgUrl;
private string _privilege;
#endregion
#region 字段属性
/// <summary>
/// 用户的唯一标识
/// </summary>
public string openid
{
set { _openID = value; }
get { return _openID; }
}
/// <summary>
///
/// </summary>
public string SearchText
{
set { _searchText = value; }
get { return _searchText; }
}
/// <summary>
/// 用户昵称
/// </summary>
public string nickname
{
set { _nickname = value; }
get { return _nickname; }
}
/// <summary>
/// 用户的性别,值为1时是男性,值为2时是女性,值为0时是未知
/// </summary>
public string sex
{
set { _sex = value; }
get { return _sex; }
}
/// <summary>
/// 用户个人资料填写的省份
/// </summary>
public string province
{
set { _province = value; }
get { return _province; }
}
/// <summary>
/// 普通用户个人资料填写的城市