#region Apache License Version 2.0 /*---------------------------------------------------------------- Copyright 2019 Jeffrey Su & Suzhou Senparc Network Technology Co.,Ltd. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Detail: https://github.com/JeffreySu/WeiXinMPSDK/blob/master/license.md ----------------------------------------------------------------*/ #endregion Apache License Version 2.0 /*---------------------------------------------------------------- Copyright(C) 2017 Senparc 文件名:OAuthAPI.cs 文件功能描述:OAuth 创建标识:Senparc - 20150211 修改标识:Senparc - 20150303 修改描述:整理接口 修改标识:Senparc - 20160719 修改描述:增加其接口的异步方法 ----------------------------------------------------------------*/ /* 官方文档:http://mp.weixin.qq.com/wiki/index.php?title=%E7%BD%91%E9%A1%B5%E6%8E%88%E6%9D%83%E8%8E%B7%E5%8F%96%E7%94%A8%E6%88%B7%E5%9F%BA%E6%9C%AC%E4%BF%A1%E6%81%AF#.E7.AC.AC.E4.B8.80.E6.AD.A5.EF.BC.9A.E7.94.A8.E6.88.B7.E5.90.8C.E6.84.8F.E6.8E.88.E6.9D.83.EF.BC.8C.E8.8E.B7.E5.8F.96code */ using System.Threading.Tasks; using Senparc.CO2NET.Extensions; using Senparc.NeuChar; using Senparc.Weixin.CommonAPIs; using Senparc.Weixin.Entities; using Senparc.Weixin.HttpUtility; using Senparc.Weixin.MP.AdvancedAPIs.OAuth; using Senparc.Weixin.MP.CommonAPIs; namespace Senparc.Weixin.MP.AdvancedAPIs { public static class OAuthApi { #region 同步方法 /*此接口不提供异步方法*/ /// /// 获取验证地址 /// /// 公众号的唯一标识 /// 授权后重定向的回调链接地址,请使用urlencode对链接进行处理 /// 重定向后会带上state参数,开发者可以填写a-zA-Z0-9的参数值,最多128字节 /// 应用授权作用域,snsapi_base (不弹出授权页面,直接跳转,只能获取用户openid),snsapi_userinfo (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且,即使在未关注的情况下,只要用户授权,也能获取其信息) /// 返回类型,请填写code(或保留默认) /// 加上后可以解决40029-invalid code的问题(测试中) /// [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "OAuthApi.GetAuthorizeUrl", true)] public static string GetAuthorizeUrl(string appId, string redirectUrl, string state, OAuthScope scope, string responseType = "code", bool addConnectRedirect = true) { var url = string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type={2}&scope={3}&state={4}{5}#wechat_redirect", appId.AsUrlData(), redirectUrl.AsUrlData(), responseType.AsUrlData(), scope.ToString("g").AsUrlData(), state.AsUrlData(), addConnectRedirect ? "&connect_redirect=1" : ""); /* 这一步发送之后,客户会得到授权页面,无论同意或拒绝,都会返回redirectUrl页面。 * 如果用户同意授权,页面将跳转至 redirect_uri/?code=CODE&state=STATE。这里的code用于换取access_token(和通用接口的access_token不通用) * 若用户禁止授权,则重定向后不会带上code参数,仅会带上state参数redirect_uri?state=STATE */ return url; } /// /// 获取AccessToken(OAuth专用) /// /// 公众号的唯一标识 /// 公众号的appsecret /// code作为换取access_token的票据,每次用户授权带上的code将不一样,code只能使用一次,5分钟未被使用自动过期。 /// 填写为authorization_code(请保持默认参数) /// [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "OAuthApi.GetAccessToken", true)] public static OAuthAccessTokenResult GetAccessToken(string appId, string secret, string code, string grantType = "authorization_code") { var url = string.Format(Config.ApiMpHost + "/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type={3}", appId.AsUrlData(), secret.AsUrlData(), code.AsUrlData(), grantType.AsUrlData()); return CommonJsonSend.Send(null, url, null, CommonJsonSendType.GET); } /// /// 刷新(OAuth专用)access_token(如果需要) /// /// 公众号的唯一标识 /// 填写通过access_token获取到的refresh_token参数 /// 填写refresh_token /// [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "OAuthApi.RefreshToken", true)] public static RefreshTokenResult RefreshToken(string appId, string refreshToken, string grantType = "refresh_token") { var url = string.Format(Config.ApiMpHost + "/sns/oauth2/refresh_token?appid={0}&grant_type={1}&refresh_token={2}", appId.AsUrlData(), grantType.AsUrlData(), refreshToken.AsUrlData()); return CommonJsonSend.Send(null, url, null, CommonJsonSendType.GET); } /// /// 获取用户基本信息 /// /// 调用接口凭证(OAuth专用) /// 普通用户的标识,对当前公众号唯一 /// 返回国家地区语言版本,zh_CN 简体,zh_TW 繁体,en 英语 /// [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "OAuthApi.GetUserInfo", true)] public static OAuthUserInfo GetUserInfo(string oauthAccessToken, string openId, Language lang = Language.zh_CN) { var url = string.Format(Config.ApiMpHost + "/sns/userinfo?access_token={0}&openid={1}&lang={2}", oauthAccessToken.AsUrlData(), openId.AsUrlData(), lang.ToString("g").AsUrlData()); return CommonJsonSend.Send(null, url, null, CommonJsonSendType.GET); } /// /// 检验授权凭证(access_token)是否有效(OAuth专用) /// /// 调用接口凭证(OAuth专用) /// 用户的唯一标识 /// [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "OAuthApi.Auth", true)] public static WxJsonResult Auth(string oauthAccessToken, string openId) { var url = string.Format(Config.ApiMpHost + "/sns/auth?access_token={0}&openid={1}", oauthAccessToken.AsUrlData(), openId.AsUrlData()); return CommonJsonSend.Send(null, url, null, CommonJsonSendType.GET); } #endregion #if !NET35 && !NET40 #region 异步方法 /// /// 【异步方法】获取AccessToken(OAuth专用) /// /// /// /// code作为换取access_token的票据,每次用户授权带上的code将不一样,code只能使用一次,5分钟未被使用自动过期。 /// /// [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "OAuthApi.GetAccessTokenAsync", true)] public static async Task GetAccessTokenAsync(string appId, string secret, string code, string grantType = "authorization_code") { var url = string.Format(Config.ApiMpHost + "/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type={3}", appId.AsUrlData(), secret.AsUrlData(), code.AsUrlData(), grantType.AsUrlData()); return await Senparc.Weixin.CommonAPIs.CommonJsonSend.SendAsync(null, url, null, CommonJsonSendType.GET); } /// ///【异步方法】刷新(OAuth专用)access_token(如果需要) /// /// /// 填写通过access_token获取到的refresh_token参数 /// /// [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "OAuthApi.RefreshTokenAsync", true)] public static async Task RefreshTokenAsync(string appId, string refreshToken, string grantType = "refresh_token") { var url = string.Format(Config.ApiMpHost + "/sns/oauth2/refresh_token?appid={0}&grant_type={1}&refresh_token={2}", appId.AsUrlData(), grantType.AsUrlData(), refreshToken.AsUrlData()); return await Senparc.Weixin.CommonAPIs.CommonJsonSend.SendAsync(null, url, null, CommonJsonSendType.GET); } /// ///【异步方法】 获取用户基本信息 /// /// 调用接口凭证(OAuth专用) /// 普通用户的标识,对当前公众号唯一 /// 返回国家地区语言版本,zh_CN 简体,zh_TW 繁体,en 英语 /// [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "OAuthApi.GetUserInfoAsync", true)] public static async Task GetUserInfoAsync(string oauthAccessToken, string openId, Language lang = Language.zh_CN) { var url = string.Format(Config.ApiMpHost + "/sns/userinfo?access_token={0}&openid={1}&lang={2}", oauthAccessToken.AsUrlData(), openId.AsUrlData(), lang.ToString("g").AsUrlData()); return await Senparc.Weixin.CommonAPIs.CommonJsonSend.SendAsync(null, url, null, CommonJsonSendType.GET); } /// /// 【异步方法】检验授权凭证(access_token)是否有效(OAuth专用) /// /// 调用接口凭证(OAuth专用) /// 用户的唯一标识 /// [ApiBind(NeuChar.PlatformType.WeChat_OfficialAccount, "OAuthApi.AuthAsync", true)] public static async Task AuthAsync(string oauthAccessToken, string openId) { var url = string.Format(Config.ApiMpHost + "/sns/auth?access_token={0}&openid={1}", oauthAccessToken.AsUrlData(), openId.AsUrlData()); return await Senparc.Weixin.CommonAPIs.CommonJsonSend.SendAsync(null, url, null, CommonJsonSendType.GET); } #endregion #endif } }