Client.cs 24 KB

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