- Home /
Is it possible to convert this to the Unity API?
Ive been trying to find a way to upload screenshot to twitter from my game without having to buy the prime31 plugins. I tried Lets Tweet in Unity but I could only get it to work with text. I found this and am wondering if it could be converted to unity api? I am only learning JS and don't really know anything about C#. The original is here http://cropperplugins.codeplex.com/discussions/269906
string twitterUrl1 = "http://api.twitter.com/1/statuses/update.xml?status=";
string twitterUrl2 = "https://upload.twitter.com/1/statuses/update_with_media.xml";
private string GetTwitterUpdateUrl()
{
return (imageFile == null) ?
twitterUrl1 + message : twitterUrl2;
}
private static string GetMimeType(String filename)
{
var extension = System.IO.Path.GetExtension(filename).ToLower();
var regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(extension);
string result =
((regKey != null) && (regKey.GetValue("Content Type") != null))
? regKey.GetValue("Content Type").ToString()
: "image/unknown" ;
return result;
}
private void Tweet()
{
var url = GetTwitterUpdateUrl();
var authzHeader = oauth.GenerateAuthzHeader(url, "POST");
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.PreAuthenticate = true;
request.AllowWriteStreamBuffering = true;
request.Headers.Add("Authorization", authzHeader);
if (imageFile != null)
{
string boundary = "======" +
Guid.NewGuid().ToString().Substring(18).Replace("-","") +
"======";
var separator = "--" + boundary;
var footer = "\r\n" + separator + "--\r\n";
string shortFileName = Path.GetFileName(imageFile);
string fileContentType = GetMimeType(shortFileName);
string fileHeader = string.Format("Content-Disposition: file; " +
"name=\"media\"; filename=\"{0}\"",
shortFileName);
var encoding = System.Text.Encoding.GetEncoding("iso-8859-1");
var contents = new System.Text.StringBuilder();
contents.AppendLine(separator);
contents.AppendLine("Content-Disposition: form-data; name=\"status\"");
contents.AppendLine();
contents.AppendLine(message);
contents.AppendLine(separator);
contents.AppendLine(fileHeader);
contents.AppendLine(string.Format("Content-Type: {0}", fileContentType));
contents.AppendLine();
request.ServicePoint.Expect100Continue = false;
request.ContentType = "multipart/form-data; boundary=" + boundary;
// actually send the request
using (var s = request.GetRequestStream())
{
byte[] bytes = encoding.GetBytes(contents.ToString());
s.Write(bytes, 0, bytes.Length);
bytes = File.ReadAllBytes(imageFile);
s.Write(bytes, 0, bytes.Length);
bytes = encoding.GetBytes(footer);
s.Write(bytes, 0, bytes.Length);
}
}
using (var response = (HttpWebResponse)request.GetResponse())
{
if (response.StatusCode != HttpStatusCode.OK)
MessageBox.Show("There's been a problem trying to tweet:" +
Environment.NewLine +
response.StatusDescription +
Environment.NewLine +
Environment.NewLine +
"You will have to tweet manually." +
Environment.NewLine);
}
}
Does this critical answer help ?
Very Important Answer...
http://answers.unity3d.com/questions/443240/using-www-to-simply-post-a-slab-of-text-ala-json.html
So here's an example how to send a fully customized POST request:
//C#
string ourPostData = "{\"someJSON\":42}";
Hashtable headers = new Hashtable();
headers.Add("Content-Type", "application/json");
headers.Add("Cookie", "Our session cookie");
byte[] pData = Encoding.ASCII.GetBytes(ourPostData.ToCharArray());
WWW www = new WWW("http://our.server.domain.name/path/to/our/resource/file", pData, headers);
This would produce a request like this:
POST /path/to/our/resource/file HTTP/1.1
Content-Type: application/json
Cookie: Our session cookie
{"someJSON":42}
Answer by Graham-Dunnett · May 29, 2013 at 07:20 PM
Unity has a WWWForm
class which supports POST
requests. However there is no way to set the headers used by the request, so I'd assume the code cannot be converted.
Answer by david.gavilan · Jun 07, 2013 at 05:02 AM
I was struggling with the same thing... Twitter seems very picky with the format of the Http request, so I couldn't get it working just with WWWForm and WWW, so in the end I used the HttpWebRequest class to create the request, but to make the actual request I use the WWW class:
byte[] bytes; // ... your POST data should go here
// ... now create the headers
// HttpWebRequest tHttpWebRequest;
// Get everything from your raw request
System.Collections.Hashtable tHeaders = new System.Collections.Hashtable();
for(int i = 0; i < tHttpWebRequest.Headers.Count; ++i) {
string tHeader = tHttpWebRequest.Headers.GetKey(i);
foreach(string tValue in tHttpWebRequest.Headers.GetValues(i)) {
tHeaders[tHeader] = tValue;
}
}
WWW www = new WWW(url, bytes, tHeaders);
$$anonymous$$ check this out ...
http://answers.unity3d.com/questions/443240/using-www-to-simply-post-a-slab-of-text-ala-json.html
Actually, I had to give up on using the WWW class on iOS... WWW www = new WWW(url, bytes, tHeaders); works fine in the Editor and Android, but on iOS I always get a 403 Forbidden response... So I had to do something like this,
#if UNITY_IPHONE
HttpWebResponse tHttpWebResponse = (HttpWebResponse)tHttpWebRequest.GetResponse();
//...
#else
WWW www = new WWW(tUrl, bytes, tHeaders);
//...
#endif
which of course will block the main game thread on iOS :'(
it's strange you have trouble on iOS as we generate zillions of calls like that every day on iOS apps using ordinary old www.
PS if you have a new, separate question, do not hesitate to ask it.
Regarding this question please tick any useful answer (the ROUND symbol on the left with a TIC$$anonymous$$ mark). As the questioner, it is your job to close questions where nothing more can be achieved.
Also you get points so you can ask future questions without being moderated.
Thanks for your help. I suspect that it may be some setting like tHttpWebRequest.ServicePoint.Expect100Continue = false; that gets properly set in HttpWebRequest, but something goes wrong in WWW... Not sure, because only iOS fails...
I don't see any TIC$$anonymous$$ mark, and I can't even vote up any answer. The system requests me to login as a different user to do that.
Your answer
Follow this Question
Related Questions
adding a share button. 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Receipt JSON Split 0 Answers