Beste Forum leden,
Tot voor kort gebruikte ik onderstaande code voor plaatsen van een tweet bericht + foto.
Door een api versie wijziging van API 1 naar API 1.1 bij twitter werkt deze code sinds enkele dagen niet meer. Wie kan mij hier mee opweg helpen ?
Tot voor kort gebruikte ik onderstaande code voor plaatsen van een tweet bericht + foto.
Door een api versie wijziging van API 1 naar API 1.1 bij twitter werkt deze code sinds enkele dagen niet meer. Wie kan mij hier mee opweg helpen ?
code:
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
| public partial class WebForm6 : System.Web.UI.Page
{
string twitterUrl1 = "http://api.twitter.com/1/statuses/update.xml?status=";
string twitterUrl2 = "https://upload.twitter.com/1/statuses/update_with_media.xml";
string imageFile = HttpContext.Current.Server.MapPath("/img/1659.jpg"); //Sample image
string message;
protected void Page_Load(object sender, EventArgs e)
{
message = "Mijn test bericht";
Twitter();
}
private void Twitter()
{
var oauth = new OAuth.Manager();
oauth["consumer_key"] = ConfigurationManager.AppSettings["ConsumerKey"];
oauth["consumer_secret"] = ConfigurationManager.AppSettings["ConsumerSecret"];
oauth["token"] = ConfigurationManager.AppSettings["AccessToken"];
oauth["token_secret"] = ConfigurationManager.AppSettings["AccessTokenSecret"];
var url = GetTwitterUpdateUrl();
var authzHeader = oauth.GenerateAuthzHeader(url, "POST");
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.PreAuthenticate = true;
request.AllowWriteStreamBuffering = true;
request.Headers.Add("Authorization", authzHeader);
if (imageFile != null)
{
string boundary = "======" + Guid.NewGuid().ToString().Substring(18).Replace("-", "") + "======";
var separator = "--" + boundary;
var footer = "\r\n" + separator + "--\r\n";
string shortFileName = Path.GetFileName(imageFile);
string fileContentType = GetMimeType(shortFileName);
string fileHeader = string.Format("Content-Disposition: file; " +
"name=\"media\"; filename=\"{0}\"",
shortFileName);
var encoding = System.Text.Encoding.GetEncoding("iso-8859-1");
var contents = new System.Text.StringBuilder();
contents.AppendLine(separator);
contents.AppendLine("Content-Disposition: form-data; name=\"status\"");
contents.AppendLine();
contents.AppendLine(message);
contents.AppendLine(separator);
contents.AppendLine(fileHeader);
contents.AppendLine(string.Format("Content-Type: {0}", fileContentType));
contents.AppendLine();
request.ServicePoint.Expect100Continue = false;
request.ContentType = "multipart/form-data; boundary=" + boundary;
// actually send the request
using (var s = request.GetRequestStream())
{
byte[] bytes = encoding.GetBytes(contents.ToString());
s.Write(bytes, 0, bytes.Length);
bytes = File.ReadAllBytes(imageFile);
s.Write(bytes, 0, bytes.Length);
bytes = encoding.GetBytes(footer);
s.Write(bytes, 0, bytes.Length);
}
}
using (var response = (HttpWebResponse)request.GetResponse())
{
if (response.StatusCode != HttpStatusCode.OK)
{
Boolean status = false;
}
else
{
Boolean status = true;
}
}
}
private string GetTwitterUpdateUrl()
{
return (imageFile == null) ?
twitterUrl1 + message : twitterUrl2;
}
private static string GetMimeType(String filename)
{
var extension = System.IO.Path.GetExtension(filename).ToLower();
var regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(extension);
string result =
((regKey != null) && (regKey.GetValue("Content Type") != null))
? regKey.GetValue("Content Type").ToString()
: "image/unknown";
return result;
}
} |