/Twitpic/TwitpicClient.cs
C# | 96 lines | 78 code | 15 blank | 3 comment | 0 complexity | 31c7a22e40188cac208c5f4103d68478 MD5 | raw file
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Codeplex.OAuth;
- using System.Net;
- using System.Reactive.Linq;
- using System.IO;
-
- namespace ImageUploader.Extensions.Twitpic
- {
- public class TwitpicClient : OAuthBase
- {
- const string ApiKey = "a6092a4ce2a252852f9d808849a4d344"; // set your apikey
-
- readonly AccessToken accessToken;
-
- public TwitpicClient(string consumerKey, string consumerSecret, AccessToken accessToken)
- : base(consumerKey, consumerSecret)
- {
- this.accessToken = accessToken;
- }
-
- private WebRequest CreateRequest(string url)
- {
- const string ServiceProvider = "https://api.twitter.com/1/account/verify_credentials.json";
- const string Realm = "http://api.twitter.com/";
-
- var req = WebRequest.Create(url);
-
- // generate oauth signature and parameters
- var parameters = ConstructBasicParameters(ServiceProvider, MethodType.Get, accessToken);
- // make auth header string
- var authHeader = BuildAuthorizationHeader(new[] { new Parameter("Realm", Realm) }.Concat(parameters));
-
- // set authenticate headers
- req.Headers["X-Verify-Credentials-Authorization"] = authHeader;
- req.Headers["X-Auth-Service-Provider"] = ServiceProvider;
-
- return req;
- }
-
- public IObservable<string> UploadPicture(string filename, string message, byte[] file)
- {
- var req = CreateRequest("http://api.twitpic.com/2/upload.xml"); // choose xml or json
- req.Method = "POST";
-
- var boundaryKey = Guid.NewGuid().ToString();
- var boundary = "--" + boundaryKey;
- req.ContentType = "multipart/form-data; boundary=" + boundaryKey;
-
- return Observable.Defer(() =>
- Observable.FromAsyncPattern<Stream>(req.BeginGetRequestStream, req.EndGetRequestStream)())
- .Do(stream =>
- {
- using (stream)
- using (var sw = new StreamWriter(stream, new UTF8Encoding(false)))
- {
- sw.WriteLine(boundary);
- sw.WriteLine("Content-Disposition: form-data; name=\"key\"");
- sw.WriteLine();
- sw.WriteLine(ApiKey);
-
- sw.WriteLine(boundary);
- sw.WriteLine("Content-Disposition: form-data; name=\"message\"");
- sw.WriteLine();
- sw.WriteLine(message);
-
- sw.WriteLine(boundary);
- sw.WriteLine("Content-Disposition: form-data; name=\"media\"; filename=\"" + filename + "\"");
- sw.WriteLine("Content-Type: application/octet-stream");
- sw.WriteLine("Content-Transfer-Encoding: binary");
- sw.WriteLine();
- sw.Flush();
-
- stream.Write(file, 0, file.Length);
- stream.Flush();
-
- sw.WriteLine();
- sw.WriteLine("--" + boundaryKey + "--");
- sw.Flush();
- }
- })
- .SelectMany(_ => Observable.FromAsyncPattern<WebResponse>(req.BeginGetResponse, req.EndGetResponse)())
- .Select(res =>
- {
- using (res)
- using (var stream = res.GetResponseStream())
- using (var sr = new StreamReader(stream, Encoding.UTF8))
- {
- return sr.ReadToEnd();
- }
- });
- }
- }
- }