1、服务认证,RestSharp定义了一个认证授权的接口 IAuthenticator ,有NtlmAuthenticator、HttpBasicAuthenticator、OAuth1Authenticator、OAuth2Authenticator几种,基本上可以满足要求了,腾讯社区开放平台使用OAuth2,腾讯社区开放平台额外增加了一个OpenId的参数,我们从OAuth2Authenticator的基类继承实现一个:
{
private readonly string openId;
private readonly string consumerKey;
:base(accessToken)
{
this.openId = openId;
this.consumerKey = consumerkey;
}
{
request.AddParameter("access_token", AccessToken, ParameterType.GetOrPost);
request.AddParameter("openid", openId, ParameterType.GetOrPost);
request.AddParameter("oauth_consumer_key", consumerKey, ParameterType.GetOrPost);
}
{
var request = _requestHelper.CreateOpenIDRequest(accessToken);
var response = Execute(request);
var openid = GetUserOpenId(response.Content);
return openid;
}
{
{
throw new QzoneException(response);
}
return response;
}
{
var request = new RestRequest(Method.GET);
request.Resource = "oauth2.0/me?access_token={accesstoken}";
request.AddParameter("accesstoken", accesstoken, ParameterType.UrlSegment);
return request;
}
/// 发表一条微博信息(纯文本)到腾讯微博平台上
/// </summary>
/// <param name="content">表示要发表的微博内容。必须为UTF-8编码,最长为140个汉字,也就是420字节。
/// 如果微博内容中有URL,后台会自动将该URL转换为短URL,每个URL折算成11个字节。</param>
/// <param name="clientip">用户ip,以分析用户所在地</param>
/// <param name="jing">用户所在地理位置的经度。为实数,最多支持10位有效数字。有效范围:-180.0到+180.0,+表示东经,默认为0.0</param>
/// <param name="wei">用户所在地理位置的纬度。为实数,最多支持10位有效数字。有效范围:-90.0到+90.0,+表示北纬,默认为0.0。</param>
/// <param name="syncflag">标识是否将发布的微博同步到QQ空间(0:同步; 1:不同步;),默认为0.</param>
/// <returns></returns>
internal AddWeiboResult AddWeibo(string content, string clientip = "", string jing = "", string wei = "", int syncflag = 0)
{
var request = _requestHelper.CreateAddWeiboRequest(content, clientip,jing,wei,syncflag);
var payload = Deserialize<AddWeiboResult>(response.Content);
return payload;
}
{
var request = new RestRequest(Method.POST);
request.RequestFormat = DataFormat.Json;
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.Resource = "t/add_t";
request.AddParameter("content", content);
if (!string.IsNullOrEmpty(clientip))
{
request.AddParameter("clientip", clientip);
}
if (!string.IsNullOrEmpty(jing))
{
request.AddParameter("jing", jing);
}
if (!string.IsNullOrEmpty(wei))
{
request.AddParameter("wei", wei);
}
request.AddParameter("syncflag", syncflag);
return request;
}
{
var request = new RestRequest(Method.POST);
request.RequestFormat = DataFormat.Json;
var boundary = string.Concat("--", Util.GenerateRndNonce());
request.AddHeader("Content-Type", string.Concat("multipart/form-data; boundary=", boundary));
request.Resource = "t/add_pic_t";
request.AddParameter("content", content);
if (!string.IsNullOrEmpty(clientip))
{
request.AddParameter("clientip", clientip);
}
if (!string.IsNullOrEmpty(jing))
{
request.AddParameter("jing", jing);
}
if (!string.IsNullOrEmpty(wei))
{
request.AddParameter("wei", wei);
}
request.AddParameter("syncflag", syncflag);
request.AddFile("pic", bytes, fileName);
return request;
}