zhaojs
2023-09-27 74098f1401afe40f961d1d167bb18dd0a71c4d59
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using com.alibaba.openapi.client.entity;
using com.alibaba.openapi.client.http;
using com.alibaba.openapi.client.policy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
 
namespace com.alibaba.openapi.client
{
    public class SyncAPIClient
    {
        private ClientPolicy clientPolicy;
 
        public SyncAPIClient(String appKey, String appSecret)
        {
            this.clientPolicy = new ClientPolicy();
            this.clientPolicy.AppKey = appKey;
            this.clientPolicy.SecretKey = appSecret;
        }
 
        public SyncAPIClient(String appKey, String appSecret, String gatewayHost)
        {
            this.clientPolicy = new ClientPolicy();
            this.clientPolicy.AppKey = appKey;
            this.clientPolicy.SecretKey = appSecret;
            this.clientPolicy.ServerHost = gatewayHost;
        }
 
        public SyncAPIClient(ClientPolicy clientPolicy)
        {
            this.clientPolicy = clientPolicy;
        }
 
        public T send<T>(AlibabaRequest request, RequestPolicy policy)
        {
            HttpClient httpClient = new HttpClient(clientPolicy);
            T result = httpClient.request<T>(request, policy);
            return result;
        }
 
        public Res execute<Res>(GatewayAPIRequest gatewayAPIRequest, String accessToken)
        {
            HttpClient httpClient = new HttpClient(clientPolicy);
            RequestPolicy policy = new RequestPolicy();
            policy.UseHttps = true;
            AlibabaRequest request = new AlibabaRequest();
            request.ApiId = gatewayAPIRequest.ApiId;
            request.RequestEntity = gatewayAPIRequest;
            request.AccessToken = accessToken;
            Res result = httpClient.request<Res>(request, policy);
            return result;
        }
 
        public AuthorizationToken getToken(String code)
        {
 
            APIId apiId = new APIId();
            apiId.Name = "getToken";
            apiId.NamespaceValue = "system.oauth2";
            apiId.Version = 1;
 
            AlibabaRequest request = new AlibabaRequest();
            request.ApiId = apiId;
 
            request.AddtionalParams["code"] = code;
            request.AddtionalParams["grant_type"] = "authorization_code";
            request.AddtionalParams["need_refresh_token"] = true;
            request.AddtionalParams["client_id"] = clientPolicy.AppKey;
            request.AddtionalParams["client_secret"] = clientPolicy.SecretKey;
            request.AddtionalParams["redirect_uri"] = "default";
            RequestPolicy oauthPolicy = new RequestPolicy();
            oauthPolicy.UseHttps = true;
 
            return this.send<AuthorizationToken>(request, oauthPolicy);
        }
 
        public AuthorizationToken refreshToken(String refreshToken)
        {
 
            APIId apiId = new APIId();
            apiId.Name = "getToken";
            apiId.NamespaceValue = "system.oauth2";
            apiId.Version = 1;
        
            AlibabaRequest request = new AlibabaRequest();
            request.ApiId = apiId;
 
            request.AddtionalParams["refreshToken"] = refreshToken;
            request.AddtionalParams["grant_type"] = "refresh_token";
            request.AddtionalParams["client_id"] = clientPolicy.AppKey;
            request.AddtionalParams["client_secret"] = clientPolicy.SecretKey;
            request.AddtionalParams["redirect_uri"] = "default";
            RequestPolicy oauthPolicy = new RequestPolicy();
            oauthPolicy.UseHttps = true;
            return this.send<AuthorizationToken>(request, oauthPolicy);
 
 
        }
    }
}