Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ISlandFace · May 29, 2013 at 07:52 AM · c#androidtwitter

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);
         }
     }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · Jun 07, 2013 at 05:21 AM 0
Share

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}


2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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);    
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · Jun 07, 2013 at 05:22 AM 0
Share

$$anonymous$$ check this out ...

http://answers.unity3d.com/questions/443240/using-www-to-simply-post-a-slab-of-text-ala-json.html

avatar image david.gavilan · Jun 10, 2013 at 10:09 AM 0
Share

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 :'(

avatar image Fattie · Jun 10, 2013 at 10:17 AM 0
Share

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.

avatar image david.gavilan · Jun 10, 2013 at 10:50 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Support the MONO AOT Compilation on Android 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges