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);
|
|
|
}
|
}
|
}
|