-
Notifications
You must be signed in to change notification settings - Fork 0
/
Twitter.cs
96 lines (81 loc) · 3.92 KB
/
Twitter.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
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json.Linq;
namespace Teto {
/// <summary>
/// A Twitter client.
/// </summary>
public class Twitter {
/// <summary>
/// The OAuth credentials this client is authorized to.
/// </summary>
private OAuth oAuth;
/// <summary>
/// Construct a Twitter client.
/// </summary>
/// <param name="oAuth">The OAuth credentials this client should be authorized to.</param>
public Twitter(OAuth oAuth) {
this.oAuth = oAuth;
}
/// <summary>
/// Make a form-encoded request.
/// </summary>
/// <param name="method">The HTTP method to use.</param>
/// <param name="url">The URL to request to.</param>
/// <param name="parameters">The parameters to send.</param>
/// <returns>The server's response.</returns>
public Webbe.Response RequestForm(string method, string url, Webbe.Parameter[] parameters) {
string oAuthHeader = oAuth.GenerateHeader(method, url, Webbe.Parameter.ToDictionary(parameters));
Webbe w = new Webbe();
w.Headers["Authorization"] = oAuthHeader;
return w.UploadForm(method, url, parameters);
}
/// <summary>
/// Make a multipart-encoded request.
/// </summary>
/// <param name="method">The HTTP method to use.</param>
/// <param name="url">The URL to request to.</param>
/// <param name="parameters">The parameters to send.</param>
/// <returns>The server's response.</returns>
public Webbe.Response RequestMultipart(string method, string url, Webbe.Parameter[] parameters) {
string oAuthHeader = oAuth.GenerateHeader(method, url, new Dictionary<string, string>());
Webbe w = new Webbe();
w.Headers["Authorization"] = oAuthHeader;
return w.UploadMultipart(method, url, parameters);
}
/// <summary>
/// Make a text tweet.
/// </summary>
/// <param name="text">The text to tweet.</param>
/// <returns>The server's response.</returns>
public Webbe.Response Tweet(string text) {
List<Webbe.Parameter> parameters = new List<Webbe.Parameter>();
parameters.Add(new Webbe.FormParameter("status", text));
return RequestForm("POST", "https://api.twitter.com/1.1/statuses/update.json", parameters.ToArray());
}
/// <summary>
/// Make an image tweet.
/// </summary>
/// <param name="text">The text to tweet.</param>
/// <param name="path">The path to the image to attach.</param>
/// <returns>The server's response (specifically, the response to the tweet action).</returns>
public Webbe.Response TweetImage(string text, string path) {
JObject mediaObj = JObject.Parse(UploadFile(path).DataString);
string mediaId = mediaObj["media_id"].ToObject<string>();
List<Webbe.Parameter> parameters = new List<Webbe.Parameter>();
parameters.Add(new Webbe.FormParameter("status", text));
parameters.Add(new Webbe.FormParameter("media_ids", mediaId));
return RequestForm("POST", "https://api.twitter.com/1.1/statuses/update.json", parameters.ToArray());
}
/// <summary>
/// Upload a file to Twitter for later use in a tweet.
/// </summary>
/// <param name="path">The path to the image to upload.</param>
/// <returns>The server's response.</returns>
private Webbe.Response UploadFile(string path) {
List<Webbe.Parameter> parameters = new List<Webbe.Parameter>();
parameters.Add(new Webbe.FileParameter("media", Path.GetFileName(path), File.ReadAllBytes(path)));
return RequestMultipart("POST", "https://upload.twitter.com/1.1/media/upload.json", parameters.ToArray());
}
}
}