Magicodes.WeiChat——使用OAuth 2.0获取微信用户信息

使用Magicodes.WeiChat,可以很方便的获取到微信用户的信息。在使用OAuth 2.0之前,你先需要做以下操作:

1)在开发者中心修改【网页授权获取用户基本信息】,在弹出的界面输入自己的根域名。比如:weichat.chinacloudsites.cn

如下图所示:

2)配置菜单或者链接(如果使用特性“WeChatOAuth”,本步骤可以略过,这里只是介绍下原理,具体请参考步骤3的说明)。Magicodes.WeiChat在控制器WeiChatController中进行处理,配置路径更加简短和简单,如:http://weichat.chinacloudsites.cn/WeiChat/OAuth?redirectUrl=http://weichat.chinacloudsites.cn/WeChat/Test。其中,redirectUrl即为跳转页面地址。Magicodes.WeiChat会生成微信OAuth链接,极大的减少错误的可能性。生成链接示例如下:https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx941100f605a8c3bd&redirect_uri=http%3A%2F%2Fweichat.chinacloudsites.cn%2FWeChat%2FTest&response_type=code&scope=snsapi_userinfo&state=weichat#wechat_redirect

3)在目标页面即可获取微信用户信息,获取代码如下所示:

// GET: WeChat/Test

[AllowAnonymous]

public ActionResult Index(string code)

{

if (string.IsNullOrEmpty(code))

{

return Content("您拒绝了授权!");

}

//通过,用code换取access_token

var result = OAuthApi.GetAccessToken(WeixinHelper.appId, WeixinHelper.appSecret, code);

if (result.errcode != ReturnCode.请求成功)

{

return Content("错误:" + result.errmsg);

}

//已关注,可以得到详细信息

var userInfo = OAuthApi.GetUserInfo(result.access_token, result.openid);

return View(userInfo);

}

以上流程比较繁琐,Magicodes.WeiChat封装了一个筛选器,以便更简便的获取到用户的信息。如以下代码所示:

    [AllowAnonymous]
    public class TestController : Controller
    {
        // GET: WeChat/Test
        [AllowAnonymous]
        [WeChatOAuth]
        public ActionResult Index()
        {
            return View(WeiChatApplicationContext.Current.WeiChatUser);
        }
    }

然后,我们来看Index视图:

@model WeiChat_User
@{
    ViewBag.Title = "Index";
    Layout = "~/Areas/WeChat/Views/Shared/_Layout.cshtml";
}
<h2>您的微信信息如下:</h2>
<h2>昵称:@Model.NickName</h2>
<h2>城市:@Model.City</h2>

显示效果如下:

注意:必须在Action上添加特性“WeChatOAuth”,然后就可以在代码中通过WeiChatApplicationContext.Current.WeiChatUser拿到微信用户信息了。

微信用户信息内容如下:

public class WeiChat_User

{

/// <summary>

/// 用户的标识,对当前公众号唯一

/// </summary>

[Key]

[Display(Name = "OpenId")]

public string OpenId { get; set; }

/// <summary>

/// 用户是否订阅该公众号标识

/// </summary>

[Display(Name = "是否订阅")]

public bool Subscribe { get; set; }

/// <summary>

/// 用户的昵称

/// </summary>

[Display(Name = "昵称")]

public string NickName { get; set; }

/// <summary>

/// 用户的性别,值为1时是男性,值为2时是女性,值为0时是未知

/// </summary>

[Display(Name = "性别")]

public int Sex { get; set; }

/// <summary>

/// 用户所在城市

/// </summary>

[Display(Name = "所在城市")]

public string City { get; set; }

/// <summary>

/// 用户所在国家

/// </summary>

[Display(Name = "所在国家")]

public string Country { get; set; }

/// <summary>

/// 用户所在省份

/// </summary>

[Display(Name = "所在省份")]

public string Province { get; set; }

/// <summary>

/// 用户的语言,简体中文为zh_CN

/// </summary>

[Display(Name = "语言")]

public string Language { get; set; }

/// <summary>

/// 用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空。若用户更换头像,原有头像URL将失效。

/// </summary>

[Display(Name = "头像")]

public string HeadImgUrl { get; set; }

/// <summary>

/// 用户关注时间,为时间戳。如果用户曾多次关注,则取最后关注时间

/// </summary>

[Display(Name = "关注时间")]

public DateTime SubscribeTime { get; set; }

/// <summary>

/// 只有在用户将公众号绑定到微信开放平台帐号后,才会出现该字段。详见:获取用户个人信息(UnionID机制)

/// </summary>

public string UnionId { get; set; }

/// <summary>

/// 公众号运营者对粉丝的备注,公众号运营者可在微信公众平台用户管理界面对粉丝添加备注

/// </summary>

[Display(Name = "备注")]

public string Remark { get; set; }

/// <summary>

/// 用户所在的分组ID

/// </summary>

public int GroupId { get; set; }

}

时间: 2025-01-02 18:49:22

Magicodes.WeiChat——使用OAuth 2.0获取微信用户信息的相关文章

小白学react之网页获取微信用户信息

通过上一篇<小白学react之EJS模版实战>我们学习了如何通过EJS模版生成我们高定制化的index.html文件. 本篇我们将会继续延续我们的alt-tutorial项目的实战计划,去获取微信扫码用户的信息,并将头像显示在我们页面的右上角上. 最终实战效果将如下所示. 首先根据我们的网站url生成二维码,比如我们可以通过浏览器的FeHelper来生成: 然后用户通过微信扫码: 最后用户确定授权后获取到用户的基本信息,并将头像显示在右上角: 1. 内网穿透准备 我们获取微信用户信息的过程中,

Android 微信登录昵称乱码问题,及获取微信用户信息

微信第三方登录时昵称,username乱码问题解决方法如下: /** * 获取微信用户信息 * @param access_token 调用凭证 * @param openid 普通用户的标识,对当前开发者帐号唯一 * @return */ public static String getWeixinUserinfo(String access_token, String openid){ String URLs = ChildendConstant.WEIXIN_USERINFO;//微信登录

python flask获取微信用户信息报404,nginx问题

在学习flask与微信公众号时问题,发现测试自动回复/wechat8008时正常,而测试获取微信用户信息/wechat8008/index时出现404.查询资料后收发是nginx配置问题. 在location后面加上=是严格匹配,url后面必须完全一至,不加等号则是只要url是以该信息开头就匹配成功.所以去掉等号,重启nginx正常. 将 location = /wechat8008 {改为 location /wechat8008 { 重启nginx sudo /usr/local/nginx

微信公众平台开发—利用OAuth2.0获取微信用户基本信息

1.首先在某微信平台下配置OAuth2.0授权回调页面: 2.通过appid构造url获取微信回传code值(appid可在微信平台下找到) 1).微信不弹出授权页面url: A.code回传到页面wxProcess2.aspx,不带参数 [csharp] view plain copy Response.Redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&

基于ThinkPHP框架小程序获取微信用户信息并将存进数据库

场景描述:在微信小程序中,我们可能用到很多种登陆方式,例如用手机作为标识登陆亦或者用微信信息作为标识登陆(但这写都要看你的项目需要),在这里我说一下如何使用微信信息作为标识登陆. 编程思路:分三步走,第一步微信信息获取发送给后台->第二步解密微信信息验证数据库->登陆成功保存缓存并且更新token 小程序前端处理 1 //调用登录接口,获取 code 2 wx.login({ 3 success: function (res) { 4 //微信js_code 5 that.setData({

php获取微信用户信息(没测试过)

<?php /** * 通过$appid.$appsecret获得基础支持的接口唯一凭证access_token,返回值为array类型 */ function get_access_token_base($appid , $appsecret ) { $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret";

SpringCloud : 接入 微信公众号平台(四)、获取微信用户信息接口

代码参考: import com.phpdragon.wechat.proxy.config.WeChatConfig; import com.phpdragon.wechat.proxy.dto.mp.user.GetOauthUserInfoDto; import com.phpdragon.wechat.proxy.dto.mp.user.GetOpenidDto; import com.phpdragon.wechat.proxy.dto.mp.user.GetUserInfoDto;

获取微信用户信息出现乱码

在绑定用户微信时,需要从微信获取用户信息,此处容易出现乱码. JSONObject jo = this.getAccessTokenOpenid(code); StringBuilder sb = new StringBuilder("https://api.weixin.qq.com/sns/userinfo?access_token="); sb.append(jo.getString("access_token")); sb.append("&

微信公众平台获取微信用户信息

1: 进入开发者管理中心 ---网页服务-----网页账号------网页授权获取用户基本信息  修改  改成自己网站的根域名 格式 如下图                             2:建立一个连接 3: 具体获取数据的代码 //获取用户信息的函数 $accesstoken $openudfunction usertoken($accesstoken,$openid){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "https