AppStoreOAuth.cs 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #region Apache License Version 2.0
  2. /*----------------------------------------------------------------
  3. Copyright 2019 Jeffrey Su & Suzhou Senparc Network Technology Co.,Ltd.
  4. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
  5. except in compliance with the License. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software distributed under the
  8. License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  9. either express or implied. See the License for the specific language governing permissions
  10. and limitations under the License.
  11. Detail: https://github.com/JeffreySu/WeiXinMPSDK/blob/master/license.md
  12. ----------------------------------------------------------------*/
  13. #endregion Apache License Version 2.0
  14. /*----------------------------------------------------------------
  15. Copyright (C) 2019 Senparc
  16. 文件名:AppStoreOAuth.cs
  17. 文件功能描述:AppStoreOAuth
  18. 创建标识:Senparc - 20150319
  19. ----------------------------------------------------------------*/
  20. using Senparc.CO2NET.Extensions;
  21. using Senparc.Weixin.CommonAPIs;
  22. using Senparc.Weixin.HttpUtility;
  23. using Senparc.Weixin.MP.CommonAPIs;
  24. namespace Senparc.Weixin.MP.AppStore
  25. {
  26. public static class AppStoreOAuth
  27. {
  28. private static string Domain
  29. {
  30. get
  31. {
  32. return Config.IsDebug ? "http://localhost:12222" : "http://www.weiweihi.com";//用于自动切换本地单元测试的请求地址
  33. }
  34. }
  35. /// <summary>
  36. /// 获取验证地址
  37. /// </summary>
  38. /// <param name="weixinId">正在使用此APP的weixinId</param>
  39. /// <param name="clientId">此App的唯一标识。可以在此APP的【开发接入】页面的【OAuth认证 设置】页面看到</param>
  40. /// <param name="redirectUrl"></param>
  41. /// <param name="state"></param>
  42. /// <param name="responseType"></param>
  43. /// <returns></returns>
  44. public static string GetAuthorizeUrl(int weixinId, string clientId, string redirectUrl, string state, string responseType = "code")
  45. {
  46. var url = string.Format(Domain + "/OAuth2/Authorize?weixinId={0}&clientId={1}&RedirectUri={2}&response_type=code&state={3}",
  47. weixinId.ToString("d").AsUrlData(), clientId.AsUrlData(), redirectUrl.AsUrlData(), state.AsUrlData());
  48. /* 这一步发送之后,客户会得到授权页面,无论同意或拒绝,都会返回redirectUrl页面。
  49. * 如果用户同意授权,页面将跳转至 redirect_uri/?code=CODE&state=STATE。这里的code用于换取access_token(和通用接口的access_token不通用)
  50. * 若用户禁止授权,则重定向后不会带上code参数,仅会带上state参数redirect_uri?state=STATE
  51. */
  52. return url;
  53. }
  54. /// <summary>
  55. /// 获取AccessToken
  56. /// </summary>
  57. /// <param name="clientId">此App的唯一标识。可以在此APP的【开发接入】页面的【OAuth认证 设置】页面看到</param>
  58. /// <param name="clientSecret">对应此App的唯一标识的密码。可以在此APP的【开发接入】页面的【OAuth认证 设置】页面看到</param>
  59. /// <param name="code">code作为换取access_token的票据,每次用户授权带上的code将不一样,code只能使用一次,5分钟未被使用自动过期。</param>
  60. /// <param name="grantType"></param>
  61. /// <returns></returns>
  62. public static OAuthAccessTokenResult GetAccessToken(string clientId, string clientSecret, string code, string grantType = "authorization_code")
  63. {
  64. var url = string.Format(Domain + "/OAuth2/AccessToken?clientId={0}&clientSecret={1}&code={2}&grantType={3}",
  65. clientId.AsUrlData(), clientSecret.AsUrlData(), code.AsUrlData(), grantType.AsUrlData());
  66. return CommonJsonSend.Send<OAuthAccessTokenResult>(null, url, null, CommonJsonSendType.GET);
  67. }
  68. }
  69. }