この記事で紹介している方法は 2010年時点の方法です。現在はYFrogのサービス終了のため動作しませんので、ご注意ください。
public string oAuthEchoWebRequestForYFrog(Method method, string url,
string auth_provider_url, string auth_realm_url, byte[] image, string imageName,
string YFrogAPIKey, Encoding ContentEncoding, string FileContentType)
{
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);
webRequest.Headers.Add("X-Auth-Service-Provider", auth_provider_url);
string oauthSignaturePattern
= "OAuth realm=\"{0:s}\", oauth_consumer_key=\"{1:s}\", oauth_signature_method=\"HMAC-SHA1\","
+ " oauth_token=\"{2:s}\", oauth_timestamp=\"{3:s}\", oauth_nonce=\"{4:s}\","
+ " oauth_version=\"1.0\", oauth_signature=\"{5:s}\"";
string authorizationHeader = string.Format(
System.Globalization.CultureInfo.InvariantCulture,
oauthSignaturePattern,
auth_realm_url,
this.ConsumerKey,
this.Token,
timeStamp,
nonce,
HttpUtility.UrlEncode(sig));
webRequest.Headers.Add("X-Verify-Credentials-Authorization", authorizationHeader);
webRequest.Method = method.ToString();
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}\"", "key"));
contents.AppendLine();
contents.AppendLine(YFrogAPIKey);
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 PostPictureToYFrog(byte[] image, string imageName,
string ConsumerKey, string ConsumerSecret, string AccessToken, string AccessTokenSecret,
string YFrogAPIKey, string PictureContentType)
{
string yfrog_api_url = "https://yfrog.com/api/xauth_upload";
string auth_provider_url = "https://api.twitter.com/1/account/verify_credentials.xml";
string auth_realm_url = "http://api.twitter.com/";
string contentEncodingStr = "iso-8859-1";
Encoding contentEncoding = Encoding.GetEncoding(contentEncodingStr);
oAuth.ConsumerKey = ConsumerKey;
oAuth.ConsumerSecret = ConsumerSecret;
oAuth.Token = AccessToken;
oAuth.TokenSecret = AccessTokenSecret;
oAuth.oAuthEchoWebRequestForYFrog(oAuthTwitter.Method.POST,
yfrog_api_url, auth_provider_url, auth_realm_url,
image, imageName, YFrogAPIKey, contentEncoding, PictureContentType);
}
private void button3_Click(object sender, EventArgs e)
{
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.PostPictureToYFrog(img, "pic.jpg",
ConsumerKey, ConsumerSecret, AccessToken, AccessTokenSecret, YFrogKey, PictureContentType);
}
ConsumerKey, ConsumerSecret, AccessToken, AccessTokenSecret, YFrogKeyには取得したキーを代入しておきます。