步步为盈
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
4.1 KiB

using BBWY.Common.Http;
using com.alibaba.openapi.client.entity;
using com.alibaba.openapi.client.http;
using com.alibaba.openapi.client.policy;
using Newtonsoft.Json.Linq;
using System;
namespace com.alibaba.openapi.client
{
public class SyncAPIClient
{
private ClientPolicy clientPolicy;
private RestApiService restApiService;
private HttpClient alibabaHttpClient;
public SyncAPIClient(String appKey, String appSecret)
{
this.clientPolicy = new ClientPolicy();
this.clientPolicy.AppKey = appKey;
this.clientPolicy.SecretKey = appSecret;
}
public SyncAPIClient(String appKey, String appSecret, RestApiService restApiService)
{
this.clientPolicy = new ClientPolicy();
this.clientPolicy.AppKey = appKey;
this.clientPolicy.SecretKey = appSecret;
this.restApiService = restApiService;
this.alibabaHttpClient = new HttpClient(clientPolicy, restApiService);
}
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 JObject NewRequest(Request request, RequestPolicy policy)
{
return alibabaHttpClient.NewRequest(request, policy);
}
public T send<T>(Request 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;
Request request = new Request();
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;
Request request = new Request();
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;
Request request = new Request();
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);
}
}
}