新浪分享API应用的开发
2012-07-23 20:47
281 查看
入园一年多,第一次发文章,权当为自己记录...
Metro很多方法和以前不同,许多方法都要加上async与await,http连接也一样如此...
1 . Oauth2认证是必不可少的
详情就不细说,可以看新浪上给出的文档,很清晰了..http://open.weibo.com/wiki/Oauth2
client_id和client_secret同样自己在新浪申请,没什么可说的啦~
具体方法如下:
这样得到了access_token...最好把它存在本地,这样下次就不用再登录了...
2.拿发布一条新微博来看
请求参数
可以看出必须的只有一个status,加上是OAuth认证,所以还有一个刚刚的access_token,方法如下:
这样就OK了...返回true就发不成功了....
3.另外提一下,发图片时要用MultipartFormDataContent 发送多种类型的格式..StreamContent 则可以用来发送图片流..
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.拿发布一条新微博来看
请求参数
必选 | 类型及范围 | 说明 | |
---|---|---|---|
source | false | string | 采用OAuth授权方式不需要此参数,其他授权方式为必填参数,数值为应用的AppKey。 |
access_token | false | string | 采用OAuth授权方式为必填参数,其他授权方式不需要此参数,OAuth授权后获得。 |
status | true | string | 要发布的微博文本内容,必须做URLencode,内容不超过140个汉字。 |
lat | false | float | 纬度,有效范围:-90.0到+90.0,+表示北纬,默认为0.0。 |
long | false | float | 经度,有效范围:-180.0到+180.0,+表示东经,默认为0.0。 |
annotations | false | string | 元数据,主要是为了方便第三方应用记录一些适合于自己使用的信息,每条微博可以包含一个或者多个元数据,必须以json字串的形式提交,字串长度不超过512个字符,具体内容可以自定。 |
/// <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 则可以用来发送图片流..
相关文章推荐
- Android应用开发-小巫CSDN博客客户端之集成友盟社会化分享组件
- C#开发微信门户及应用(32)--微信支付接入和API封装使用
- 学习php微信开发应用接入API接口笔记
- 基于微软Office Communicator 2007 Automation API开发应用
- iOS学习资料分享 -- 苹果官方iPhone应用高级开发课程(16集)
- 【.Net码农】【淘宝API】淘宝API开发系列--读取实体类的属性的备注作为表头的应用
- 【精品教程】Android应用开发入门教程(经典版)文档分享
- 【高德地图API】那些年我们一起开发的APP—即LBS应用模式分享
- PHP+新浪微博开放平台+新浪云平台(SAE)开发微博应用基础
- 应用开发中涉及到的css和php笔记分享
- (转)创业干货分享:初创公司移动应用开发工具库(集合)
- 淘宝API应用开发小试
- 【Android应用开发详解】第01期:第三方授权认证(一)实现第三方授权登录、分享以及获取用户资料
- 谷歌地图开发经验分享系列之二:Google Map API基础对象操作和用法(Map类和LatLng类)
- 【Android应用开发详解】第01期:第三方授权认证(一)实现第三方授权登录、分享以及获取用户资料
- Android开发中一个简单实用的调试应用技巧分享
- 分享11个超棒的移动应用(mobile apps)开发解决方案
- linux - 使用curl实现新浪天气API应用
- 如何合理设计HybridApp: 移动应用跨平台开发经验分享
- 创业干货分享:初创公司移动应用开发工具库(集合)