Client.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. // This file is auto-generated, don't edit it. Thanks.
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Threading.Tasks;
  7. using Tea;
  8. using Tea.Utils;
  9. using Alipay.EasySDK.Base.OAuth.Models;
  10. namespace Alipay.EasySDK.Base.OAuth
  11. {
  12. public class Client
  13. {
  14. protected Alipay.EasySDK.Kernel.Client _kernel;
  15. public Client(Alipay.EasySDK.Kernel.Client kernel)
  16. {
  17. this._kernel = kernel;
  18. }
  19. public AlipaySystemOauthTokenResponse GetToken(string code)
  20. {
  21. Dictionary<string, object> runtime_ = new Dictionary<string, object>
  22. {
  23. {"connectTimeout", 15000},
  24. {"readTimeout", 15000},
  25. {"retry", new Dictionary<string, int?>
  26. {
  27. {"maxAttempts", 0},
  28. }},
  29. };
  30. TeaRequest _lastRequest = null;
  31. Exception _lastException = null;
  32. long _now = System.DateTime.Now.Millisecond;
  33. int _retryTimes = 0;
  34. while (TeaCore.AllowRetry((IDictionary) runtime_["retry"], _retryTimes, _now))
  35. {
  36. if (_retryTimes > 0)
  37. {
  38. int backoffTime = TeaCore.GetBackoffTime((IDictionary)runtime_["backoff"], _retryTimes);
  39. if (backoffTime > 0)
  40. {
  41. TeaCore.Sleep(backoffTime);
  42. }
  43. }
  44. _retryTimes = _retryTimes + 1;
  45. try
  46. {
  47. TeaRequest request_ = new TeaRequest();
  48. Dictionary<string, string> systemParams = new Dictionary<string, string>
  49. {
  50. {"method", "alipay.system.oauth.token"},
  51. {"app_id", this._kernel.GetConfig("appId")},
  52. {"timestamp", this._kernel.GetTimestamp()},
  53. {"format", "json"},
  54. {"version", "1.0"},
  55. {"alipay_sdk", this._kernel.GetSdkVersion()},
  56. {"charset", "UTF-8"},
  57. {"sign_type", this._kernel.GetConfig("signType")},
  58. {"app_cert_sn", this._kernel.GetMerchantCertSN()},
  59. {"alipay_root_cert_sn", this._kernel.GetAlipayRootCertSN()},
  60. };
  61. Dictionary<string, object> bizParams = new Dictionary<string, object>(){};
  62. Dictionary<string, string> textParams = new Dictionary<string, string>
  63. {
  64. {"grant_type", "authorization_code"},
  65. {"code", code},
  66. };
  67. request_.Protocol = this._kernel.GetConfig("protocol");
  68. request_.Method = "POST";
  69. request_.Pathname = "/gateway.do";
  70. request_.Headers = new Dictionary<string, string>
  71. {
  72. {"host", this._kernel.GetConfig("gatewayHost")},
  73. {"content-type", "application/x-www-form-urlencoded;charset=utf-8"},
  74. };
  75. request_.Query = this._kernel.SortMap(TeaConverter.merge<string>
  76. (
  77. new Dictionary<string, string>()
  78. {
  79. {"sign", this._kernel.Sign(systemParams, bizParams, textParams, this._kernel.GetConfig("merchantPrivateKey"))},
  80. },
  81. systemParams,
  82. textParams
  83. ));
  84. request_.Body = TeaCore.BytesReadable(this._kernel.ToUrlEncodedRequestBody(bizParams));
  85. _lastRequest = request_;
  86. TeaResponse response_ = TeaCore.DoAction(request_, runtime_);
  87. Dictionary<string, object> respMap = this._kernel.ReadAsJson(response_, "alipay.system.oauth.token");
  88. if (this._kernel.IsCertMode())
  89. {
  90. if (this._kernel.Verify(respMap, this._kernel.ExtractAlipayPublicKey(this._kernel.GetAlipayCertSN(respMap))))
  91. {
  92. return TeaModel.ToObject<AlipaySystemOauthTokenResponse>(this._kernel.ToRespModel(respMap));
  93. }
  94. }
  95. else
  96. {
  97. if (this._kernel.Verify(respMap, this._kernel.GetConfig("alipayPublicKey")))
  98. {
  99. return TeaModel.ToObject<AlipaySystemOauthTokenResponse>(this._kernel.ToRespModel(respMap));
  100. }
  101. }
  102. throw new TeaException(new Dictionary<string, string>
  103. {
  104. {"message", "验签失败,请检查支付宝公钥设置是否正确。"},
  105. });
  106. }
  107. catch (Exception e)
  108. {
  109. if (TeaCore.IsRetryable(e))
  110. {
  111. _lastException = e;
  112. continue;
  113. }
  114. throw e;
  115. }
  116. }
  117. throw new TeaUnretryableException(_lastRequest, _lastException);
  118. }
  119. public async Task<AlipaySystemOauthTokenResponse> GetTokenAsync(string code)
  120. {
  121. Dictionary<string, object> runtime_ = new Dictionary<string, object>
  122. {
  123. {"connectTimeout", 15000},
  124. {"readTimeout", 15000},
  125. {"retry", new Dictionary<string, int?>
  126. {
  127. {"maxAttempts", 0},
  128. }},
  129. };
  130. TeaRequest _lastRequest = null;
  131. Exception _lastException = null;
  132. long _now = System.DateTime.Now.Millisecond;
  133. int _retryTimes = 0;
  134. while (TeaCore.AllowRetry((IDictionary) runtime_["retry"], _retryTimes, _now))
  135. {
  136. if (_retryTimes > 0)
  137. {
  138. int backoffTime = TeaCore.GetBackoffTime((IDictionary)runtime_["backoff"], _retryTimes);
  139. if (backoffTime > 0)
  140. {
  141. TeaCore.Sleep(backoffTime);
  142. }
  143. }
  144. _retryTimes = _retryTimes + 1;
  145. try
  146. {
  147. TeaRequest request_ = new TeaRequest();
  148. Dictionary<string, string> systemParams = new Dictionary<string, string>
  149. {
  150. {"method", "alipay.system.oauth.token"},
  151. {"app_id", this._kernel.GetConfig("appId")},
  152. {"timestamp", this._kernel.GetTimestamp()},
  153. {"format", "json"},
  154. {"version", "1.0"},
  155. {"alipay_sdk", this._kernel.GetSdkVersion()},
  156. {"charset", "UTF-8"},
  157. {"sign_type", this._kernel.GetConfig("signType")},
  158. {"app_cert_sn", this._kernel.GetMerchantCertSN()},
  159. {"alipay_root_cert_sn", this._kernel.GetAlipayRootCertSN()},
  160. };
  161. Dictionary<string, object> bizParams = new Dictionary<string, object>(){};
  162. Dictionary<string, string> textParams = new Dictionary<string, string>
  163. {
  164. {"grant_type", "authorization_code"},
  165. {"code", code},
  166. };
  167. request_.Protocol = this._kernel.GetConfig("protocol");
  168. request_.Method = "POST";
  169. request_.Pathname = "/gateway.do";
  170. request_.Headers = new Dictionary<string, string>
  171. {
  172. {"host", this._kernel.GetConfig("gatewayHost")},
  173. {"content-type", "application/x-www-form-urlencoded;charset=utf-8"},
  174. };
  175. request_.Query = this._kernel.SortMap(TeaConverter.merge<string>
  176. (
  177. new Dictionary<string, string>()
  178. {
  179. {"sign", this._kernel.Sign(systemParams, bizParams, textParams, this._kernel.GetConfig("merchantPrivateKey"))},
  180. },
  181. systemParams,
  182. textParams
  183. ));
  184. request_.Body = TeaCore.BytesReadable(this._kernel.ToUrlEncodedRequestBody(bizParams));
  185. _lastRequest = request_;
  186. TeaResponse response_ = await TeaCore.DoActionAsync(request_, runtime_);
  187. Dictionary<string, object> respMap = await this._kernel.ReadAsJsonAsync(response_, "alipay.system.oauth.token");
  188. if (this._kernel.IsCertMode())
  189. {
  190. if (this._kernel.Verify(respMap, this._kernel.ExtractAlipayPublicKey(this._kernel.GetAlipayCertSN(respMap))))
  191. {
  192. return TeaModel.ToObject<AlipaySystemOauthTokenResponse>(this._kernel.ToRespModel(respMap));
  193. }
  194. }
  195. else
  196. {
  197. if (this._kernel.Verify(respMap, this._kernel.GetConfig("alipayPublicKey")))
  198. {
  199. return TeaModel.ToObject<AlipaySystemOauthTokenResponse>(this._kernel.ToRespModel(respMap));
  200. }
  201. }
  202. throw new TeaException(new Dictionary<string, string>
  203. {
  204. {"message", "验签失败,请检查支付宝公钥设置是否正确。"},
  205. });
  206. }
  207. catch (Exception e)
  208. {
  209. if (TeaCore.IsRetryable(e))
  210. {
  211. _lastException = e;
  212. continue;
  213. }
  214. throw e;
  215. }
  216. }
  217. throw new TeaUnretryableException(_lastRequest, _lastException);
  218. }
  219. public AlipaySystemOauthTokenResponse RefreshToken(string refreshToken)
  220. {
  221. Dictionary<string, object> runtime_ = new Dictionary<string, object>
  222. {
  223. {"connectTimeout", 15000},
  224. {"readTimeout", 15000},
  225. {"retry", new Dictionary<string, int?>
  226. {
  227. {"maxAttempts", 0},
  228. }},
  229. };
  230. TeaRequest _lastRequest = null;
  231. Exception _lastException = null;
  232. long _now = System.DateTime.Now.Millisecond;
  233. int _retryTimes = 0;
  234. while (TeaCore.AllowRetry((IDictionary) runtime_["retry"], _retryTimes, _now))
  235. {
  236. if (_retryTimes > 0)
  237. {
  238. int backoffTime = TeaCore.GetBackoffTime((IDictionary)runtime_["backoff"], _retryTimes);
  239. if (backoffTime > 0)
  240. {
  241. TeaCore.Sleep(backoffTime);
  242. }
  243. }
  244. _retryTimes = _retryTimes + 1;
  245. try
  246. {
  247. TeaRequest request_ = new TeaRequest();
  248. Dictionary<string, string> systemParams = new Dictionary<string, string>
  249. {
  250. {"method", "alipay.system.oauth.token"},
  251. {"app_id", this._kernel.GetConfig("appId")},
  252. {"timestamp", this._kernel.GetTimestamp()},
  253. {"format", "json"},
  254. {"version", "1.0"},
  255. {"alipay_sdk", this._kernel.GetSdkVersion()},
  256. {"charset", "UTF-8"},
  257. {"sign_type", this._kernel.GetConfig("signType")},
  258. {"app_cert_sn", this._kernel.GetMerchantCertSN()},
  259. {"alipay_root_cert_sn", this._kernel.GetAlipayRootCertSN()},
  260. };
  261. Dictionary<string, object> bizParams = new Dictionary<string, object>(){};
  262. Dictionary<string, string> textParams = new Dictionary<string, string>
  263. {
  264. {"grant_type", "refresh_token"},
  265. {"refresh_token", refreshToken},
  266. };
  267. request_.Protocol = this._kernel.GetConfig("protocol");
  268. request_.Method = "POST";
  269. request_.Pathname = "/gateway.do";
  270. request_.Headers = new Dictionary<string, string>
  271. {
  272. {"host", this._kernel.GetConfig("gatewayHost")},
  273. {"content-type", "application/x-www-form-urlencoded;charset=utf-8"},
  274. };
  275. request_.Query = this._kernel.SortMap(TeaConverter.merge<string>
  276. (
  277. new Dictionary<string, string>()
  278. {
  279. {"sign", this._kernel.Sign(systemParams, bizParams, textParams, this._kernel.GetConfig("merchantPrivateKey"))},
  280. },
  281. systemParams,
  282. textParams
  283. ));
  284. request_.Body = TeaCore.BytesReadable(this._kernel.ToUrlEncodedRequestBody(bizParams));
  285. _lastRequest = request_;
  286. TeaResponse response_ = TeaCore.DoAction(request_, runtime_);
  287. Dictionary<string, object> respMap = this._kernel.ReadAsJson(response_, "alipay.system.oauth.token");
  288. if (this._kernel.IsCertMode())
  289. {
  290. if (this._kernel.Verify(respMap, this._kernel.ExtractAlipayPublicKey(this._kernel.GetAlipayCertSN(respMap))))
  291. {
  292. return TeaModel.ToObject<AlipaySystemOauthTokenResponse>(this._kernel.ToRespModel(respMap));
  293. }
  294. }
  295. else
  296. {
  297. if (this._kernel.Verify(respMap, this._kernel.GetConfig("alipayPublicKey")))
  298. {
  299. return TeaModel.ToObject<AlipaySystemOauthTokenResponse>(this._kernel.ToRespModel(respMap));
  300. }
  301. }
  302. throw new TeaException(new Dictionary<string, string>
  303. {
  304. {"message", "验签失败,请检查支付宝公钥设置是否正确。"},
  305. });
  306. }
  307. catch (Exception e)
  308. {
  309. if (TeaCore.IsRetryable(e))
  310. {
  311. _lastException = e;
  312. continue;
  313. }
  314. throw e;
  315. }
  316. }
  317. throw new TeaUnretryableException(_lastRequest, _lastException);
  318. }
  319. public async Task<AlipaySystemOauthTokenResponse> RefreshTokenAsync(string refreshToken)
  320. {
  321. Dictionary<string, object> runtime_ = new Dictionary<string, object>
  322. {
  323. {"connectTimeout", 15000},
  324. {"readTimeout", 15000},
  325. {"retry", new Dictionary<string, int?>
  326. {
  327. {"maxAttempts", 0},
  328. }},
  329. };
  330. TeaRequest _lastRequest = null;
  331. Exception _lastException = null;
  332. long _now = System.DateTime.Now.Millisecond;
  333. int _retryTimes = 0;
  334. while (TeaCore.AllowRetry((IDictionary) runtime_["retry"], _retryTimes, _now))
  335. {
  336. if (_retryTimes > 0)
  337. {
  338. int backoffTime = TeaCore.GetBackoffTime((IDictionary)runtime_["backoff"], _retryTimes);
  339. if (backoffTime > 0)
  340. {
  341. TeaCore.Sleep(backoffTime);
  342. }
  343. }
  344. _retryTimes = _retryTimes + 1;
  345. try
  346. {
  347. TeaRequest request_ = new TeaRequest();
  348. Dictionary<string, string> systemParams = new Dictionary<string, string>
  349. {
  350. {"method", "alipay.system.oauth.token"},
  351. {"app_id", this._kernel.GetConfig("appId")},
  352. {"timestamp", this._kernel.GetTimestamp()},
  353. {"format", "json"},
  354. {"version", "1.0"},
  355. {"alipay_sdk", this._kernel.GetSdkVersion()},
  356. {"charset", "UTF-8"},
  357. {"sign_type", this._kernel.GetConfig("signType")},
  358. {"app_cert_sn", this._kernel.GetMerchantCertSN()},
  359. {"alipay_root_cert_sn", this._kernel.GetAlipayRootCertSN()},
  360. };
  361. Dictionary<string, object> bizParams = new Dictionary<string, object>(){};
  362. Dictionary<string, string> textParams = new Dictionary<string, string>
  363. {
  364. {"grant_type", "refresh_token"},
  365. {"refresh_token", refreshToken},
  366. };
  367. request_.Protocol = this._kernel.GetConfig("protocol");
  368. request_.Method = "POST";
  369. request_.Pathname = "/gateway.do";
  370. request_.Headers = new Dictionary<string, string>
  371. {
  372. {"host", this._kernel.GetConfig("gatewayHost")},
  373. {"content-type", "application/x-www-form-urlencoded;charset=utf-8"},
  374. };
  375. request_.Query = this._kernel.SortMap(TeaConverter.merge<string>
  376. (
  377. new Dictionary<string, string>()
  378. {
  379. {"sign", this._kernel.Sign(systemParams, bizParams, textParams, this._kernel.GetConfig("merchantPrivateKey"))},
  380. },
  381. systemParams,
  382. textParams
  383. ));
  384. request_.Body = TeaCore.BytesReadable(this._kernel.ToUrlEncodedRequestBody(bizParams));
  385. _lastRequest = request_;
  386. TeaResponse response_ = await TeaCore.DoActionAsync(request_, runtime_);
  387. Dictionary<string, object> respMap = await this._kernel.ReadAsJsonAsync(response_, "alipay.system.oauth.token");
  388. if (this._kernel.IsCertMode())
  389. {
  390. if (this._kernel.Verify(respMap, this._kernel.ExtractAlipayPublicKey(this._kernel.GetAlipayCertSN(respMap))))
  391. {
  392. return TeaModel.ToObject<AlipaySystemOauthTokenResponse>(this._kernel.ToRespModel(respMap));
  393. }
  394. }
  395. else
  396. {
  397. if (this._kernel.Verify(respMap, this._kernel.GetConfig("alipayPublicKey")))
  398. {
  399. return TeaModel.ToObject<AlipaySystemOauthTokenResponse>(this._kernel.ToRespModel(respMap));
  400. }
  401. }
  402. throw new TeaException(new Dictionary<string, string>
  403. {
  404. {"message", "验签失败,请检查支付宝公钥设置是否正确。"},
  405. });
  406. }
  407. catch (Exception e)
  408. {
  409. if (TeaCore.IsRetryable(e))
  410. {
  411. _lastException = e;
  412. continue;
  413. }
  414. throw e;
  415. }
  416. }
  417. throw new TeaUnretryableException(_lastRequest, _lastException);
  418. }
  419. /// <summary>
  420. /// ISV代商户代用,指定appAuthToken
  421. /// </summary>
  422. /// <param name="appAuthToken">代调用token</param>
  423. /// <returns>本客户端,便于链式调用</returns>
  424. public Client Agent(string appAuthToken)
  425. {
  426. _kernel.InjectTextParam("app_auth_token", appAuthToken);
  427. return this;
  428. }
  429. /// <summary>
  430. /// 用户授权调用,指定authToken
  431. /// </summary>
  432. /// <param name="authToken">用户授权token</param>
  433. /// <returns>本客户端,便于链式调用</returns>
  434. public Client Auth(string authToken)
  435. {
  436. _kernel.InjectTextParam("auth_token", authToken);
  437. return this;
  438. }
  439. /// <summary>
  440. /// 设置异步通知回调地址,此处设置将在本调用中覆盖Config中的全局配置
  441. /// </summary>
  442. /// <param name="url">异步通知回调地址,例如:https://www.test.com/callback </param>
  443. /// <returns>本客户端,便于链式调用</returns>
  444. public Client AsyncNotify(string url)
  445. {
  446. _kernel.InjectTextParam("notify_url", url);
  447. return this;
  448. }
  449. /// <summary>
  450. /// 将本次调用强制路由到后端系统的测试地址上,常用于线下环境内外联调,沙箱与线上环境设置无效
  451. /// </summary>
  452. /// <param name="testUrl">后端系统测试地址</param>
  453. /// <returns>本客户端,便于链式调用</returns>
  454. public Client Route(string testUrl)
  455. {
  456. _kernel.InjectTextParam("ws_service_url", testUrl);
  457. return this;
  458. }
  459. /// <summary>
  460. /// 设置API入参中没有的其他可选业务请求参数(biz_content下的字段)
  461. /// </summary>
  462. /// <param name="key">业务请求参数名称(biz_content下的字段名,比如timeout_express)</param>
  463. /// <param name="value">
  464. /// 业务请求参数的值,一个可以序列化成JSON的对象
  465. /// 如果该字段是一个字符串类型(String、Price、Date在SDK中都是字符串),请使用string储存
  466. /// 如果该字段是一个数值型类型(比如:Number),请使用long储存
  467. /// 如果该字段是一个复杂类型,请使用嵌套的Dictionary指定各下级字段的值
  468. /// 如果该字段是一个数组,请使用List储存各个值
  469. /// 对于更复杂的情况,也支持Dictionary和List的各种组合嵌套,比如参数是值是个List,List中的每种类型是一个复杂对象
  470. /// </param>
  471. /// <returns>本客户端,便于链式调用</returns>
  472. public Client Optional(string key, object value)
  473. {
  474. _kernel.InjectBizParam(key, value);
  475. return this;
  476. }
  477. /// <summary>
  478. /// 批量设置API入参中没有的其他可选业务请求参数(biz_content下的字段)
  479. /// optional方法的批量版本
  480. /// </summary>
  481. /// <param name="optionalArgs">可选参数集合,每个参数由key和value组成,key和value的格式请参见optional方法的注释</param>
  482. /// <returns>本客户端,便于链式调用</returns>
  483. public Client BatchOptional(Dictionary<string, object> optionalArgs)
  484. {
  485. foreach (var pair in optionalArgs)
  486. {
  487. _kernel.InjectBizParam(pair.Key, pair.Value);
  488. }
  489. return this;
  490. }
  491. }
  492. }