-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSinaApiService.cs
520 lines (445 loc) · 23.3 KB
/
SinaApiService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Xml;
namespace SinaAPI
{
public class SinaApiService : oAuthSina,ISinaApiService
{
//private oAuthSina _oauth;
public bool oAuth(string userid, string passwd, oAuthSina _oauth)
{
try
{
if (_oauth.Token == "" || _oauth.Token == null)
{
string authLink = _oauth.AuthorizationSinaGet();
authLink += "&userId=" + userid + "&passwd=" + passwd + "&action=submit&oauth_callback=none";
string html = _oauth.WebRequest(oAuthSina.Method.POST, authLink, null);
string pin = ParseHtml(html);
_oauth.Verifier = pin;
_oauth.AccessTokenGet(_oauth.Token);
return true;
}
else
{
_oauth.AccessTokenGet(_oauth.Token);
return true;
}
}
catch
{
return false;
}
}
/*最新公共微博*/
public string public_timeline(string userid, string passwd, string format)
{
oAuthSina _oauth = new oAuthSina();
if (oAuth(userid, passwd, _oauth))
{
string url = "http://api.t.sina.com.cn/statuses/public_timeline." + format;
return _oauth.oAuthWebRequest(oAuthSina.Method.GET, url, String.Empty);
}
else
return null;
}
/*最新关注人微博 OK*/
public string friend_timeline(string format,string apiKey,string apiKeySecret,string apiToken,string apiTokenSecret)
{
oAuthSina _oauth = new oAuthSina();
_oauth.ConsumerKey = apiKey;
_oauth.ConsumerSecret = apiKeySecret;
_oauth.Token = apiToken;
_oauth.TokenSecret = apiTokenSecret;
string url = "http://api.t.sina.com.cn/statuses/friends_timeline." + format;
return _oauth.oAuthWebRequest(oAuthSina.Method.GET, url, String.Empty);
}
/*用户发表微薄列表 OK*/
public string user_timeline(string userid, string format,string apiKey,string apiKeySecret,string apiToken,string apiTokenSecret)
{
oAuthSina _oauth = new oAuthSina();
_oauth.ConsumerKey = apiKey;
_oauth.ConsumerSecret = apiKeySecret;
_oauth.Token = apiToken;
_oauth.TokenSecret = apiTokenSecret;
string url = "http://api.t.sina.com.cn/statuses/user_timeline." + format + "?user_id=" + userid; ;
return _oauth.oAuthWebRequest(oAuthSina.Method.GET, url, String.Empty);
}
/*最新n条@我的微博*/
public string mentions(string format, string apiKey, string apiKeySecret, string apiToken, string apiTokenSecret)
{
oAuthSina _oauth = new oAuthSina();
_oauth.ConsumerKey = apiKey;
_oauth.ConsumerSecret = apiKeySecret;
_oauth.Token = apiToken;
_oauth.TokenSecret = apiTokenSecret;
string url = "http://api.t.sina.com.cn/statuses/mentions." + format;
return _oauth.oAuthWebRequest(oAuthSina.Method.GET, url, String.Empty);
}
/*最新评论*/
public string comments_timeline(string format, string apiKey, string apiKeySecret, string apiToken, string apiTokenSecret)
{
oAuthSina _oauth = new oAuthSina();
_oauth.ConsumerKey = apiKey;
_oauth.ConsumerSecret = apiKeySecret;
_oauth.Token = apiToken;
_oauth.TokenSecret = apiTokenSecret;
string url = "http://api.t.sina.com.cn/statuses/comments_timeline." + format;
return _oauth.oAuthWebRequest(oAuthSina.Method.GET, url, String.Empty);
}
/*发出的评论*/
public string comments_by_me(string userid, string passwd, string format)
{
oAuthSina _oauth = new oAuthSina();
if (oAuth(userid, passwd, _oauth))
{
string url = "http://api.t.sina.com.cn/statuses/comments_by_me." + format;
return _oauth.oAuthWebRequest(oAuthSina.Method.GET, url, String.Empty);
}
else
return null;
}
/*批量获取一组微博的评论数及转发数*/
public string counts(string userid, string passwd, string format, string ids)
{
oAuthSina _oauth = new oAuthSina();
if (oAuth(userid, passwd, _oauth))
{
string url = "http://api.t.sina.com.cn/statuses/counts." + format + "?ids=" + ids;
return _oauth.oAuthWebRequest(oAuthSina.Method.GET, url, String.Empty);
}
else
return null;
}
/**********************************************************************************************
*************************************微博访问接口*********************************************
**********************************************************************************************
**********************************************************************************************/
/*获取单条ID的微博信息*/
public string statuses_show(string userid, string passwd, string format, string id)
{
oAuthSina _oauth = new oAuthSina();
if (oAuth(userid, passwd, _oauth))
{
string url = "http://api.t.sina.com.cn/statuses/show/" + id + "." + format;
return _oauth.oAuthWebRequest(oAuthSina.Method.GET, url, String.Empty);
}
else
return null;
}
/*获取单条ID的微博信息*/
public string statuses_id(string userid, string passwd, string id, string uid)
{
oAuthSina _oauth = new oAuthSina();
if (oAuth(userid, passwd, _oauth))
{
string url = "http://api.t.sina.com.cn/" + uid + "/statuses/" + id;
return _oauth.oAuthWebRequest(oAuthSina.Method.GET, url, String.Empty);
}
else
return null;
}
/*发布一条微博信息*/
public string statuses_update(string format,string status, string apiKey, string apiKeySecret, string apiToken, string apiTokenSecret)
{
oAuthSina _oauth = new oAuthSina();
_oauth.ConsumerKey = apiKey;
_oauth.ConsumerSecret = apiKeySecret;
_oauth.Token = apiToken;
_oauth.TokenSecret = apiTokenSecret;
string url = "http://api.t.sina.com.cn/statuses/update." + format;
//System.Text.Encoding encoding = System.Text.Encoding.UTF8;
//byte[] bytesToPost = encoding.
//return _oauth.oAuthWebRequest(oAuthSina.Method.POST, url, "status=" + _oauth.UrlEncode(status));
return _oauth.oAuthWebRequest(oAuthSina.Method.POST, url, "status=" + UrlEncodePost(status));
//if (oAuth(userid, passwd, _oauth))
//{
// string url = "http://api.t.sina.com.cn/statuses/update." + format + "?";
// return _oauth.oAuthWebRequest(oAuthSina.Method.POST, url, "status=" + _oauth.UrlEncode(status));
//}
//else
// return null;
}
public string PostWebRequestCustomize(string userid, string passwd, string format,string status, string apiKey)
{
string usernamePassword = userid + ":" + passwd;
string url = "http://api.t.sina.com.cn/statuses/update."+format;
string news_title = status;
//int news_id = 696365;
//string t_news = string.Format("{0},http://news.cnblogs.com/n/{1}/", news_title, news_id);
string t_news = news_title;
string data = "source=" + apiKey + "&status=" + System.Web.HttpUtility.UrlEncode(t_news);
System.Net.WebRequest webRequest = System.Net.WebRequest.Create(url);
System.Net.HttpWebRequest httpRequest = webRequest as System.Net.HttpWebRequest;
System.Net.CredentialCache myCache = new System.Net.CredentialCache();
myCache.Add(new Uri(url), "Basic", new System.Net.NetworkCredential(userid, passwd));
httpRequest.Credentials = myCache;
httpRequest.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new System.Text.ASCIIEncoding().GetBytes(usernamePassword)));
httpRequest.Method = "POST";
httpRequest.ContentType = "application/x-www-form-urlencoded";
System.Text.Encoding encoding = System.Text.Encoding.ASCII;
byte[] bytesToPost = encoding.GetBytes(data);
httpRequest.ContentLength = bytesToPost.Length;
System.IO.Stream requestStream = httpRequest.GetRequestStream();
requestStream.Write(bytesToPost, 0, bytesToPost.Length);
requestStream.Close();
string responseContent;
System.Net.WebResponse wr = httpRequest.GetResponse();
System.IO.Stream receiveStream = wr.GetResponseStream();
using (System.IO.StreamReader reader = new System.IO.StreamReader(receiveStream, System.Text.Encoding.UTF8))
{
responseContent = reader.ReadToEnd();
}
return responseContent;
}
/*上传图片并且发布一条微博信息*/
public string statuses_upload(string userid, string passwd, string format, string status,
string apiKey, string apiKeySecret, string apiToken, string apiTokenSecret,
string filename,byte[] imageStream)
{
oAuthSina _oauth = new oAuthSina();
_oauth.ConsumerKey = apiKey;
_oauth.ConsumerSecret = apiKeySecret;
_oauth.Token = apiToken;
_oauth.TokenSecret = apiTokenSecret;
string url = "http://api.t.sina.com.cn/statuses/upload." + format;
return "";
//return _oauth.oAuthWebRequest(oAuthSina.Method.POST, url, "status=" + UrlEncodePost(status) ,userid,passwd,filename,imageStream);
}
/*上传图片并且发布一条微博信息 Basic&oAuth*/
public string status_uploadbyoauth(string userid, string passwd, string format, string status,
string apiKey, string apiKeySecret, string apiToken, string apiTokenSecret,
string filename, byte[] imageStream)
{
oAuthSina _oauth = new oAuthSina();
_oauth.ConsumerKey = apiKey;
_oauth.ConsumerSecret = apiKeySecret;
_oauth.Token = apiToken;
_oauth.TokenSecret = apiTokenSecret;
string url = "http://api.t.sina.com.cn/statuses/upload." + format;
return _oauth.oAuthWebRequest(oAuthSina.Method.POST, url, String.Empty,userid,passwd,filename,imageStream,status);
}
public string status_destroy(string twitterID, string format, string apiKey, string apiKeySecret, string apiToken, string apiTokenSecret)
{
oAuthSina _oauth = new oAuthSina();
_oauth.ConsumerKey = apiKey;
_oauth.ConsumerSecret = apiKeySecret;
_oauth.Token = apiToken;
_oauth.TokenSecret = apiTokenSecret;
string url = "http://api.t.sina.com.cn/statuses/destroy/" + twitterID.ToString() + "." + format;
return _oauth.oAuthWebRequest(oAuthSina.Method.POST, url, String.Empty);
}
/* 单条评论列表*/
public string status_comment(string id, string commentcontent, string format, string apiKey, string apiKeySecret, string apiToken, string apiTokenSecret)
{
oAuthSina _oauth = new oAuthSina();
_oauth.ConsumerKey = apiKey;
_oauth.ConsumerSecret = apiKeySecret;
_oauth.Token = apiToken;
_oauth.TokenSecret = apiTokenSecret;
string url = "http://api.t.sina.com.cn/statuses/comment." + format + "?id=" + id +"&comment=" + commentcontent;
return _oauth.oAuthWebRequest(oAuthSina.Method.POST, url, String.Empty);
}
/* 删除单条评论*/
public string status_comment_destroy(string userid, string passwd, string commentID, string format, string apiKey, string apiKeySecret, string apiToken, string apiTokenSecret)
{
oAuthSina _oauth = new oAuthSina();
_oauth.ConsumerKey = apiKey;
_oauth.ConsumerSecret = apiKeySecret;
_oauth.Token = apiToken;
_oauth.TokenSecret = apiTokenSecret;
string url = "http://api.t.sina.com.cn/statuses/comment_destroy/" + commentID.ToString() + "." + format;
return _oauth.oAuthWebRequest(oAuthSina.Method.POST, url, String.Empty);
}
/*转发一条微博信息*/
public string status_repost(string id, string commentcontent,int isComment, string format, string apiKey, string apiKeySecret, string apiToken, string apiTokenSecret)
{
oAuthSina _oauth = new oAuthSina();
_oauth.ConsumerKey = apiKey;
_oauth.ConsumerSecret = apiKeySecret;
_oauth.Token = apiToken;
_oauth.TokenSecret = apiTokenSecret;
string url = "";
if (commentcontent.Length > 0)
{
url = "http://api.t.sina.com.cn/statuses/repost." + format + "?id=" + id + "&status=" + commentcontent + "&is_comment=" + isComment;
}
else
{
url = "http://api.t.sina.com.cn/statuses/repost." + format + "?id=" + id;
}
return _oauth.oAuthWebRequest(oAuthSina.Method.POST, url, String.Empty);
}
/*回复微博评论*/
public string status_replycomment(string twitterid, string commentcontent, string commentid, string format, string apiKey, string apiKeySecret, string apiToken, string apiTokenSecret)
{
oAuthSina _oauth = new oAuthSina();
_oauth.ConsumerKey = apiKey;
_oauth.ConsumerSecret = apiKeySecret;
_oauth.Token = apiToken;
_oauth.TokenSecret = apiTokenSecret;
string url = "http://api.t.sina.com.cn/statuses/reply." + format + "?cid=" + commentid + "&comment=" + commentcontent + "&id=" + twitterid;
return _oauth.oAuthWebRequest(oAuthSina.Method.POST, url, String.Empty);
}
/**********************************************************************************************
*************************************账号接口*ok********************************************
**********************************************************************************************
**********************************************************************************************/
//Login
public string VerifyCredentials(string format, string apiKey, string apiKeySecret, string apiToken, string apiTokenSecret)
{
oAuthSina _oauth = new oAuthSina();
_oauth.ConsumerKey = apiKey;
_oauth.ConsumerSecret = apiKeySecret;
_oauth.Token = apiToken;
_oauth.TokenSecret = apiTokenSecret;
string url = "http://api.t.sina.com.cn/account/verify_credentials." + format;
return _oauth.oAuthWebRequest(oAuthSina.Method.GET, url, String.Empty);
}
public List<string> GetToken(string userid, string passwd)
{
List<string> TokenList = new List<string>();
oAuthSina _oauth = new oAuthSina();
if (oAuth(userid, passwd, _oauth))
{
TokenList.Add(_oauth.Token);
TokenList.Add(_oauth.TokenSecret);
return TokenList;
}
else
return null;
}
/**********************************************************************************************
******************************************收藏接口**OK*******************************************
**********************************************************************************************
**********************************************************************************************/
public string GetFavorites(string format, string apiKey, string apiKeySecret, string apiToken, string apiTokenSecret)
{
oAuthSina _oauth = new oAuthSina();
_oauth.ConsumerKey = apiKey;
_oauth.ConsumerSecret = apiKeySecret;
_oauth.Token = apiToken;
_oauth.TokenSecret = apiTokenSecret;
string url = "http://api.t.sina.com.cn/favorites." + format;
return _oauth.oAuthWebRequest(oAuthSina.Method.GET, url, String.Empty);
}
#region ISinaApiService Members
public string direct_messages(string format, string apiKey, string apiKeySecret, string apiToken, string apiTokenSecret)
{
oAuthSina _oauth = new oAuthSina();
_oauth.ConsumerKey = apiKey;
_oauth.ConsumerSecret = apiKeySecret;
_oauth.Token = apiToken;
_oauth.TokenSecret = apiTokenSecret;
string url = "http://api.t.sina.com.cn/direct_messages." + format;
return _oauth.oAuthWebRequest(oAuthSina.Method.GET, url, String.Empty);
}
public string unread_message(string sinceid, string format, string apiKey, string apiKeySecret, string apiToken, string apiTokenSecret)
{
oAuthSina _oauth = new oAuthSina();
_oauth.ConsumerKey = apiKey;
_oauth.ConsumerSecret = apiKeySecret;
_oauth.Token = apiToken;
_oauth.TokenSecret = apiTokenSecret;
string url = "http://api.t.sina.com.cn/statuses/unread." + format + "?source=" + apiKey + "&with_new_status=1" + "&since_id=" + sinceid;
return _oauth.oAuthWebRequest(oAuthSina.Method.GET, url, String.Empty);
}
public string create_favorite(string id, string format, string apiKey, string apiKeySecret, string apiToken, string apiTokenSecret)
{
oAuthSina _oauth = new oAuthSina();
_oauth.ConsumerKey = apiKey;
_oauth.ConsumerSecret = apiKeySecret;
_oauth.Token = apiToken;
_oauth.TokenSecret = apiTokenSecret;
string url = "http://api.t.sina.com.cn/favorites/create." + format + "?id=" + id;
return _oauth.oAuthWebRequest(oAuthSina.Method.POST, url, String.Empty);
}
public string delete_favorite(string id, string format, string apiKey, string apiKeySecret, string apiToken, string apiTokenSecret)
{
oAuthSina _oauth = new oAuthSina();
_oauth.ConsumerKey = apiKey;
_oauth.ConsumerSecret = apiKeySecret;
_oauth.Token = apiToken;
_oauth.TokenSecret = apiTokenSecret;
string url = "http://api.t.sina.com.cn/favorites/destroy." + format + "?id=" + id;
return _oauth.oAuthWebRequest(oAuthSina.Method.POST, url, String.Empty);
}
public string GetEmotions(string type, string language, string format, string apiKey, string apiKeySecret, string apiToken, string apiTokenSecret)
{
oAuthSina _oauth = new oAuthSina();
_oauth.ConsumerKey = apiKey;
_oauth.ConsumerSecret = apiKeySecret;
_oauth.Token = apiToken;
_oauth.TokenSecret = apiTokenSecret;
string url = "http://api.t.sina.com.cn/emotions." + format;
return _oauth.oAuthWebRequest(oAuthSina.Method.GET, url, String.Empty);
}
public string create_friendships(string userid, string format, string apiKey, string apiKeySecret, string apiToken, string apiTokenSecret)
{
oAuthSina _oauth = new oAuthSina();
_oauth.ConsumerKey = apiKey;
_oauth.ConsumerSecret = apiKeySecret;
_oauth.Token = apiToken;
_oauth.TokenSecret = apiTokenSecret;
string url = "http://api.t.sina.com.cn/friendships/create." + format + "?user_id=" + userid;
return _oauth.oAuthWebRequest(oAuthSina.Method.POST, url, String.Empty);
}
public string GetUserProfile(string userid, string format, string apiKey, string apiKeySecret, string apiToken, string apiTokenSecret)
{
oAuthSina _oauth = new oAuthSina();
_oauth.ConsumerKey = apiKey;
_oauth.ConsumerSecret = apiKeySecret;
_oauth.Token = apiToken;
_oauth.TokenSecret = apiTokenSecret;
string url = "http://api.t.sina.com.cn/users/show." + format + "?user_id=" + userid;
return _oauth.oAuthWebRequest(oAuthSina.Method.GET, url, String.Empty);
}
public string GetCommentsTimelineByID(string id, string format, string apiKey, string apiKeySecret, string apiToken, string apiTokenSecret)
{
oAuthSina _oauth = new oAuthSina();
_oauth.ConsumerKey = apiKey;
_oauth.ConsumerSecret = apiKeySecret;
_oauth.Token = apiToken;
_oauth.TokenSecret = apiTokenSecret;
string url = "http://api.t.sina.com.cn/statuses/comments." + format + "?id=" + id;
return _oauth.oAuthWebRequest(oAuthSina.Method.GET, url, String.Empty);
}
public string reset_count(string type, string format, string apiKey, string apiKeySecret, string apiToken, string apiTokenSecret)
{
oAuthSina _oauth = new oAuthSina();
_oauth.ConsumerKey = apiKey;
_oauth.ConsumerSecret = apiKeySecret;
_oauth.Token = apiToken;
_oauth.TokenSecret = apiTokenSecret;
string url = "http://api.t.sina.com.cn/statuses/reset_count." + format + "?type=" + type;
return _oauth.oAuthWebRequest(oAuthSina.Method.POST, url, String.Empty);
}
#endregion
#region ISinaApiService Members
public string get_followers(string id,int count, string format, string apiKey, string apiKeySecret, string apiToken, string apiTokenSecret)
{
oAuthSina _oauth = new oAuthSina();
_oauth.ConsumerKey = apiKey;
_oauth.ConsumerSecret = apiKeySecret;
_oauth.Token = apiToken;
_oauth.TokenSecret = apiTokenSecret;
string url = "http://api.t.sina.com.cn/statuses/followers." + format + "?id=" + id + "&count=" + count;
return _oauth.oAuthWebRequest(oAuthSina.Method.GET, url, String.Empty);
}
#endregion
public string show_relationship(string userid, string targetid, string format, string apiKey, string apiKeySecret, string apiToken, string apiTokenSecret)
{
oAuthSina _oauth = new oAuthSina();
_oauth.ConsumerKey = apiKey;
_oauth.ConsumerSecret = apiKeySecret;
_oauth.Token = apiToken;
_oauth.TokenSecret = apiTokenSecret;
string url = "http://api.t.sina.com.cn/friendships/show." + format + "?source_id=" + userid +"&target_id=" + targetid;
return _oauth.oAuthWebRequest(oAuthSina.Method.GET, url, String.Empty);
}
}
}