Senparc.Weixin 授权获取微信信息

在微信开发相关之前,需要申请自己的开发者APPID 和APPSECRET 

申请账号及相关 :https://developers.weixin.qq.com/miniprogram/dev/framework/quickstart/getstart.html#%E7%94%B3%E8%AF%B7%E5%B8%90%E5%8F%B7 

在使用Senparc.Weixin 授权获取用户信息之前需要获取 code

官方参考:

https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html

网页授权流程分为四步:

1 第一步:用户同意授权,获取code

2 第二步:通过code换取网页授权access_token

3 第三步:刷新access_token(如果需要)

4 第四步:拉取用户信息(需scope为 snsapi_userinfo)

5 附:检验授权凭证(access_token)是否有效

 

这里是直接拿到appid,密钥,和code之后的操作,使用Senparc.Weixin具体步骤如下:

第一步: 在NuGet 管理获取 Senparc.XXX 相关接口

第二步:

string AppID = ".......";
string AppSecret ="..........";
string code = "..........";

 //1.通过code换取网页授权access_token的URL连接
OAuthAccessTokenResult tokenResult = null;
OAuthUserInfo wxuserInfo = null;
tokenResult = OAuthApi.GetAccessToken(AppID, AppSecret, code);

//获取微信服务器用户信息
wxuserInfo = OAuthApi.GetUserInfo(tokenResult.access_token, tokenResult.openid);

string city = wxuserInfo.city;
string province = wxuserInfo.province;
string country = wxuserInfo.country;
string headimgurl = wxuserInfo.headimgurl;
string nickname = wxuserInfo.nickname;
string openid = wxuserInfo.openid;
string sex = wxuserInfo.sex.ToString();

用到的using:

using Senparc.Weixin.MP.AdvancedAPIs;
using Senparc.Weixin.MP.AdvancedAPIs.OAuth;
using Senparc.Weixin.MP.Containers;
using Senparc.Weixin.MP.Helpers;

相关参考:

https://www.cnblogs.com/huage-1234/p/10142285.html

https://www.cnblogs.com/admol/articles/4286487.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值