您的位置:首页 > 其它

新浪分享API应用的开发

2012-07-23 20:47 281 查看
入园一年多,第一次发文章,权当为自己记录...

Metro很多方法和以前不同,许多方法都要加上async与await,http连接也一样如此...

1 . Oauth2认证是必不可少的

详情就不细说,可以看新浪上给出的文档,很清晰了..http://open.weibo.com/wiki/Oauth2

client_id和client_secret同样自己在新浪申请,没什么可说的啦~

具体方法如下:

/// <summary>
/// OAuth2.0授权
/// </summary>
static  private async Task<bool> OAuth(string username, string password)
{
bool islogin=false;
dialog_url = "https://api.weibo.com/oauth2/access_token";
//发起POST连接
HttpClient client = new HttpClient();
//准备POST的数据
var postData = new List<KeyValuePair<string, string>>();
postData.Add(new KeyValuePair<string, string>("client_id", App_Key));
postData.Add(new KeyValuePair<string, string>("client_secret", App_Secret));
postData.Add(new KeyValuePair<string, string>("grant_type", "password"));
postData.Add(new KeyValuePair<string, string>("username", username));
postData.Add(new KeyValuePair<string, string>("password", password));
HttpContent httpcontent = new FormUrlEncodedContent(postData);
HttpResponseMessage response = await client.PostAsync(dialog_url, httpcontent);
//返回的信息
string responseBody = await response.Content.ReadAsStringAsync();
//匹配返回信息
Match regex_back = regex.Match(responseBody);
string value;
while (regex_back.Success)
{
value = regex_back.Value.Substring(1, regex_back.Value.Length - 2);
switch (value)
{
case "access_token":
regex_back = regex_back.NextMatch();
_access_token = regex_back.Value.Substring(1, regex_back.Value.Length - 2);
continue;
case "uid":
regex_back = regex_back.NextMatch();
uid = regex_back.Value.Substring(1, regex_back.Value.Length - 2);
islogin=true;
break;
}
regex_back = regex_back.NextMatch();
}
      return islogin;
}


这样得到了access_token...最好把它存在本地,这样下次就不用再登录了...

2.拿发布一条新微博来看

请求参数

必选类型及范围说明
sourcefalsestring采用OAuth授权方式不需要此参数,其他授权方式为必填参数,数值为应用的AppKey。
access_tokenfalsestring采用OAuth授权方式为必填参数,其他授权方式不需要此参数,OAuth授权后获得。
statustruestring要发布的微博文本内容,必须做URLencode,内容不超过140个汉字。
latfalsefloat纬度,有效范围:-90.0到+90.0,+表示北纬,默认为0.0。
longfalsefloat经度,有效范围:-180.0到+180.0,+表示东经,默认为0.0。
annotationsfalsestring元数据,主要是为了方便第三方应用记录一些适合于自己使用的信息,每条微博可以包含一个或者多个元数据,必须以json字串的形式提交,字串长度不超过512个字符,具体内容可以自定。
可以看出必须的只有一个status,加上是OAuth认证,所以还有一个刚刚的access_token,方法如下:

/// <summary>
/// 发表一条微博
/// </summary>
/// <param name="status">微博内容</param>
static public async Task<bool> Update(string status)
{
dialog_url = "https://api.weibo.com/2/statuses/update.json";
HttpClient client = new HttpClient();
////准备POST的数据
var postData = new List<KeyValuePair<string, string>>();
postData.Add(new KeyValuePair<string, string>("access_token", _access_token));
postData.Add(new KeyValuePair<string, string>("status", status));
HttpContent dataContent = new FormUrlEncodedContent(postData);

//发起POST连接
HttpResponseMessage response = await client.PostAsync(dialog_url, dataContent);
//返回的信息
responseBody = await response.Content.ReadAsStringAsync();
Match regex_back = regex.Match(responseBody);
string value;
while (regex_back.Success)
{
value = regex_back.Value.Substring(1, regex_back.Value.Length - 2);
switch (value)
{
case "created_at":
return true;
}
}
return false;
}


这样就OK了...返回true就发不成功了....

3.另外提一下,发图片时要用MultipartFormDataContent 发送多种类型的格式..StreamContent 则可以用来发送图片流..
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐