この記事で紹介している方法は 2010年時点の方法です。現在はYFrogのサービス終了のため動作しませんので、ご注意ください。
public string oAuthEchoWebRequestForYFrogOld(
Method method, string url, string auth_provider_url, byte[] image, string imageName,
string postMessage, Encoding ContentEncoding, string FileContentType, string UserName)
{
HttpWebRequest webRequest = null;
string responseData = "";
webRequest = System.Net.WebRequest.Create(url) as HttpWebRequest;
webRequest.Method = method.ToString();
webRequest.ServicePoint.Expect100Continue = false;
var boundary = Guid.NewGuid().ToString();
webRequest.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);
string outUrl = "";
string querystring = "";
string ret = "";
//Generate Signature
Uri uri = new Uri(auth_provider_url);
string nonce = this.GenerateNonce();
string timeStamp = this.GenerateTimeStamp();
string sig = this.GenerateSignature(uri,
this.ConsumerKey,
this.ConsumerSecret,
this.Token,
this.TokenSecret,
"GET",
timeStamp,
nonce,
out outUrl,
out querystring);
if (method == Method.POST) {
string header = string.Format("--{0}", boundary);
string footer = string.Format("--{0}--", boundary);
StringBuilder contents = new StringBuilder();
contents.AppendLine(header);
string fileHeader = string.Format(
"Content-Disposition: file; name=\"{0:s}\"; filename=\"{1:s}\"", "media", imageName);
string fileData = ContentEncoding.GetString(image);
contents.AppendLine(fileHeader);
contents.AppendLine(string.Format("Content-Type: {0:s}", FileContentType));
contents.AppendLine();
contents.AppendLine(fileData);
contents.AppendLine(header);
contents.AppendLine(string.Format("Content-Disposition: form-data; name=\"{0:s}\"",
"username"));
contents.AppendLine();
contents.AppendLine(UserName);
string credentialPattern = "oauth_version=1.0&oauth_nonce={0:s}"
+ "&oauth_timestamp={1:s}&oauth_consumer_key={2:s}&oauth_token={3:s}"
+ "&oauth_signature_method=HMAC-SHA1&oauth_signature={4:s}";
string credential = string.Format(credentialPattern,
nonce, timeStamp, this.ConsumerKey, this.Token, HttpUtility.UrlEncode(sig));
string CredentialURL = auth_provider_url + "?" + credential;
contents.AppendLine(header);
contents.AppendLine(
string.Format("Content-Disposition: form-data; name=\"{0:s}\"", "verify_url"));
contents.AppendLine();
contents.AppendLine(CredentialURL);
contents.AppendLine(header);
contents.AppendLine(string.Format(
"Content-Disposition: form-data; name=\"{0:s}\"", "auth"));
contents.AppendLine();
contents.AppendLine("oauth");
contents.AppendLine(header);
contents.AppendLine(string.Format(
"Content-Disposition: form-data; name=\"{0:s}\"", "message"));
contents.AppendLine();
contents.AppendLine(postMessage);
contents.AppendLine(footer);
byte[] bytes = ContentEncoding.GetBytes(contents.ToString());
webRequest.ContentLength = bytes.Length;
Stream requestWriter = webRequest.GetRequestStream();
try {
requestWriter.Write(bytes, 0, bytes.Length);
}
catch {
throw;
}
finally {
requestWriter.Close();
requestWriter = null;
}
}
responseData = WebResponseGet(webRequest);
webRequest = null;
return responseData;
}
public void PostPictureToYFrogOld(byte[] image, string imageName, string comment,
string ConsumerKey, string ConsumerSecret, string AccessToken, string AccessTokenSecret,
string PictureContentType, string UserName)
{
string yfrog_api_url = "http://yfrog.com/api/upload";
string auth_provider_url = "https://api.twitter.com/1/account/verify_credentials.xml";
string contentEncodingStr = "iso-8859-1";
Encoding contentEncoding = Encoding.GetEncoding(contentEncodingStr);
oAuth.ConsumerKey = ConsumerKey;
oAuth.ConsumerSecret = ConsumerSecret;
oAuth.Token = AccessToken;
oAuth.TokenSecret = AccessTokenSecret;
oAuth.oAuthEchoWebRequestForYFrogOld(oAuthTwitter.Method.POST,
yfrog_api_url, auth_provider_url, image, imageName, comment,
contentEncoding, PictureContentType, UserName);
}
private void button4_Click(object sender, EventArgs e)
{
string UserName="Twiter_ScreenName";
string PictureContentType="image/jpeg"; //または "image/png"
string apppath = Path.GetDirectoryName(Application.ExecutablePath);
FileStream fs = new FileStream(apppath + @"\img\pic.jpg", FileMode.Open);
byte[] img = new byte[fs.Length];
int r = fs.Read(img, 0, (int)fs.Length);
TwitterUtils tu = new TwitterUtils();
tu.PostPictureToYFrogOld(img, "pic.jpg", "test",
ConsumerKey, ConsumerSecret, AccessToken, AccessTokenSecret,
PictureContentType, UserName);
}
ConsumerKey, ConsumerSecret, AccessToken, AccessTokenSecretには取得したキーを代入しておきます。