Skip to content

Instantly share code, notes, and snippets.

@GOROman
Last active March 3, 2018 16:29
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GOROman/8216327 to your computer and use it in GitHub Desktop.
Save GOROman/8216327 to your computer and use it in GitHub Desktop.
Unityから画像付きツイート(update_with_media)をする。WWWクラスでは multipart/form-data が使えないのでTCPを直接叩く
// 断片
TcpClient tc = new TcpClient();
tc.Connect("api.twitter.com", 80);
using (NetworkStream ns = tc.GetStream())
{
System.IO.StreamWriter sw = new System.IO.StreamWriter(ns);
System.IO.StreamReader sr = new System.IO.StreamReader(ns);
string req = "POST /1.1/statuses/update_with_media.json HTTP/1.1\r\n";
req += "Accept: */*\r\n";
req += "User-Agent: Unity\r\n";
req += "Content-Type: multipart/form-data, boundary=\""+BOUNDARY+"\"\r\n";
req += "Authorization: "+auth;
req += "\r\n";
req += "Connection: close\r\n";
req += "Host: api.twitter.com\r\n";
req += "Content-Length: "+contents.Length.ToString() +"\r\n\r\n";
req += contents;
sw.Write(req);
Debug.Log( req );
sw.Flush();
Debug.Log ( sr.ReadToEnd() );
}
tc.Close();
@GOROman
Copy link
Author

GOROman commented Jan 2, 2014

バウンダリは何でもいいっぽい

    string BOUNDARY = "_KARAAGE_KARAAGE_KARAAGE_KARAAGE";

@GOROman
Copy link
Author

GOROman commented Jan 2, 2014

データ部分。PNGはBASE64化したけどRAWでもいいはず。

@GOROman
Copy link
Author

GOROman commented Jan 2, 2014

    string contents ="--"+BOUNDARY+"\r\n";

    contents += "Content-Disposition: form-data; name=\"status\"\r\n";
    contents += "\r\n";
    contents += "Tweet from Unity!!\r\n";
    contents += "--"+BOUNDARY+"\r\n";
    contents += "Content-Disposition: form-data; name=\"media[]\"; filename=\"TEST.png\"\r\n";
    contents += "Content-Type: application/octet-stream\r\n";
    contents += "Content-Transfer-Encoding: base64\r\n";
    contents += "\r\n";
    contents += "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAAAXNSR0IArs4c\n";
    contents += "6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAwSURB\n";
    contents += "VDhPlcexDQAwCMAw/j+XB8ruJarkxfP2j08++eSTTz755JNPPvnkk08++bBz\n";
    contents += "NrbxENvBoHQAAAAASUVORK5CYII=\n";
    contents += "\r\n";
    contents += "--"+BOUNDARY+"--\r\n";

@GOROman
Copy link
Author

GOROman commented Jan 2, 2014

OAuthがメンドイ。そこさえ乗り切ればなんとかなる。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment