赞
踩
引入AwsSignatureVersion4
1、构造ImmutableCredentials
企企是使用了最简单的accesskey和sercretKey
2、构造StringContent
注意在企企的接口中add,put请求的参数是在body中
get请求的参数是在header中()
在构造公共头的时候需要注意:
只需要增加Access-Key-Id和Open-Id,没有必要增加X-Amz-Date和Authorization(AwsSignatureVersion4内部已经做了计算)
注意:参数中content的结构。在下面的例子中post,put请求传进来的是json字符串,格式参考不同请求接口。Get中的由于body中没有参数,直接设置未string.Empty.
3、构造HttpClient,由于使用了Abp框架自己的代码是Injet的HttpClient
4、发送请求,读取返回结果
以Get请求为例(get请求做了修正)
public async Task<string> GetQueueInfoAsync()
{
var credentials = new ImmutableCredentials(accessKey, secretKey, null);
//构造URL
var url = qqhubHost + queuePath;
_httpClient.DefaultRequestHeaders.Add("Access-Key-Id", accessKey);
_httpClient.DefaultRequestHeaders.Add("Open-Id", openId);
_httpClient.DefaultRequestHeaders.Add("ContentType","application /json");
//发送请求,_httpClient是injet的HttpClient实例
var response = await _httpClient.GetAsync(url, regionName, serviceName, credentials);
response.EnsureSuccessStatusCode();
var resultContent = await response.Content.ReadAsStringAsync();
return resultContent;
}
以Post请求为例
public async Task<string> AddAsync(string content)
{
var jsonContent = new
{
json = content
};
var credentials = new ImmutableCredentials(accessKey, secretKey, null);
var httpcontent = new StringContent(JsonConvert.SerializeObject(jsonContent));
httpcontent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
//内部自动加了X-Amz-Date和Authorization,这里没有必要加了,以下同此,不再赘述。
httpcontent.Headers.Add("Access-Key-Id", accessKey);
httpcontent.Headers.Add("Open-Id", openId);
var response = await _httpClient.PostAsync(qqhubHost + addPath, httpcontent, regionName, serviceName, credentials);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
以put请求为例
public async Task<string> UpdateAsync(string content)
{
var jsonContent = new
{
json = content
};
var credentials = new ImmutableCredentials(accessKey, secretKey, null);
var httpcontent = new StringContent(JsonConvert.SerializeObject(jsonContent));
httpcontent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
httpcontent.Headers.Add("Access-Key-Id", accessKey);
httpcontent.Headers.Add("Open-Id", openId);
var response = await _httpClient.PutAsync(qqhubHost + updatePath, httpcontent, regionName, serviceName, credentials);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
5、已经测试过的接口
1、根据code获取openId接口路径
2、List接口
6、注意
1、首先和企企联系获取accessKey和sercretKey
2、使用code模式获取openId:
根据闫老师和暴老师的建议,code模式下redirectUrl可以随意写,返回时在地址栏可以看到code和requestId,比normal方便。
3、根据code获取openId
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。