この記事で紹介している方法は 2010年時点の方法です。現在はついっぷるフォトはサービス終了のため動作しませんので、ご注意ください。
public string oAuthEchoWebRequestForTwipplePhoto(
Method method, string url, string auth_provider_url,
byte[] image, string imageName, 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;
string boundary = Guid.NewGuid().ToString();
webRequest.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);
//Generate Signature
string outUrl = "";
string querystring = "";
string ret = "";
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);
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}";
//oauth_signatureをURLエンコードし、%3dを%3Dに、%2bを%2B置換して処理する場合。
string UrlEncodeSig=HttpUtility.UrlEncode(sig);
UrlEncodeSig = UrlEncodeSig.Replace("%3d", "%3D");
UrlEncodeSig = UrlEncodeSig.Replace("%2b", "%2B");
string credential = string.Format(credentialPattern, nonce, timeStamp,
this.ConsumerKey, this.Token, UrlEncodeSig);
/*
//oauth_signatureをURLエンコードしない場合
string credential = string.Format(credentialPattern,
nonce, timeStamp, this.ConsumerKey, this.Token, sig);
*/
string CredentialURL = auth_provider_url + "?" + credential;
/*
string credentialPattern = "oauth_consumer_key={0:s}&&oauth_signature_method=HMAC-SHA1"
+ "&oauth_timestamp={1:s}&oauth_nonce={2:s}&oauth_version=1.0&oauth_token={3:s}"
+ "&oauth_signature={4:s}";
string credential = string.Format(
credentialPattern, this.ConsumerKey, timeStamp, nonce, this.Token, sig);
string CredentialURL = auth_provider_url + "?" + credential;
*/
if (method == Method.POST) {
string header = string.Format("--{0}", boundary);
string footer = string.Format("--{0}--", boundary);
StringBuilder contents = new StringBuilder();
//string fileHeader = string.Format(
// "Content-Disposition: file; name=\"{0:s}\"; filename=\"{1:s}\"", "media", imageName);
contents.AppendLine(header);
contents.AppendLine(
string.Format("Content-Disposition: form-data; name=\"{0:s}\"", "auth"));
contents.AppendLine("Content-Type: text/plain; charset=US-ASCII");
contents.AppendLine("Content-Transfer-Encoding: 8bit");
contents.AppendLine();
contents.AppendLine("oauth");
contents.AppendLine(header);
contents.AppendLine(
string.Format("Content-Disposition: form-data; name=\"{0:s}\"", "verify_url"));
contents.AppendLine("Content-Type: text/plain; charset=US-ASCII");
contents.AppendLine("Content-Transfer-Encoding: 8bit");
contents.AppendLine();
contents.AppendLine(CredentialURL);
/*
contents.AppendLine(header);
contents.AppendLine(
string.Format("Content-Disposition: form-data; name=\"{0:s}\"", "username"));
contents.AppendLine("Content-Type: text/plain; charset=US-ASCII");
contents.AppendLine("Content-Transfer-Encoding: 8bit");
contents.AppendLine();
contents.AppendLine("");
contents.AppendLine(header);
contents.AppendLine(
string.Format("Content-Disposition: form-data; name=\"{0:s}\"", "password"));
contents.AppendLine("Content-Type: text/plain; charset=US-ASCII");
contents.AppendLine("Content-Transfer-Encoding: 8bit");
contents.AppendLine();
contents.AppendLine("");
*/
string fileHeader = string.Format(
"Content-Disposition: form-data; name=\"{0:s}\"; filename=\"{1:s}\"", "media", imageName);
string fileData = ContentEncoding.GetString(image);
contents.AppendLine(header);
contents.AppendLine(fileHeader);
contents.AppendLine(
string.Format("Content-Type: {0:s}; charset=ISO-8859-1", FileContentType));
contents.AppendLine("Content-Transfer-Encoding: binary");
contents.AppendLine();
contents.AppendLine(fileData);
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 PostPictureToTwipplePhoto(byte[] image, string imageName,
string ConsumerKey, string ConsumerSecret, string AccessToken, string AccessTokenSecret,
string PictureContentType)
{
string twipple_photo_api_url = "http://p.twipple.jp/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.oAuthEchoWebRequestForTwipplePhoto(oAuthTwitter.Method.POST,
twipple_photo_api_url, auth_provider_url, image, imageName, comment,
TwitpicKey, contentEncoding, PictureContentType);
}
private void button2_Click(object sender, EventArgs e)
{
string PictureContentType="application/octet-stream";
//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.PostPictureToTwipplePhoto(img, "pic.jpg",
ConsumerKey, ConsumerSecret, AccessToken, AccessTokenSecret, PictureContentType);
}
ConsumerKey, ConsumerSecret, AccessToken, AccessTokenSecretには取得したキーを代入しておきます。