Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Adding a photo to "Lets Tweet in Unity" plugin

Discussion in 'Editor & General Support' started by ISlandFace, May 26, 2013.

  1. ISlandFace

    ISlandFace

    Joined:
    Feb 18, 2013
    Posts:
    9
    Hi everyone, been trying to figure this out but cannot get it. Im new to unity and am only learning JS, this is in C#. Can anyone help me get https://upload.twitter.com/1/statuses/update_with_media.xml to work? I'm not sure how I would add it to the parameters.


    Code (csharp):
    1.  private static readonly string PostTweetURL = "http://api.twitter.com/1/statuses/update.xml?status={0}";
    2.  
    3.         public static IEnumerator PostTweet(string text, string consumerKey, string consumerSecret, AccessTokenResponse response, PostTweetCallback callback)
    4.         {
    5.             if (string.IsNullOrEmpty(text) || text.Length > 140)
    6.             {
    7.                 Debug.Log(string.Format("PostTweet - text[{0}] is empty or too long.", text));
    8.  
    9.                 callback(false);
    10.             }
    11.             else
    12.             {
    13.                 string url = string.Format(PostTweetURL, UrlEncode(text));
    14.                 Dictionary<string, string> parameters = new Dictionary<string, string>();
    15.  
    16.                 parameters.Add("status", text);              
    17.  
    18.                 // Need to fill body since Unity doesn't like an empty request body.
    19.                 byte[] dummmy = new byte[1];
    20.                 dummmy[0] = 0;
    21.  
    22.                 // HTTP header
    23.                 Hashtable headers = new Hashtable();
    24.                 headers["Authorization"] = GetHeaderWithAccessToken("POST", url, consumerKey, consumerSecret, response, parameters);
    25.  
    26.                 WWW web = new WWW(url, dummmy, headers);
    27.                 yield return web;
    28.                 ...
     
  2. ISlandFace

    ISlandFace

    Joined:
    Feb 18, 2013
    Posts:
    9
  3. ISlandFace

    ISlandFace

    Joined:
    Feb 18, 2013
    Posts:
    9
    This is what I have now but I get a 401 unauthorized error.

    Code (csharp):
    1. private static readonly string PostTweetURL = "https://upload.twitter.com/1/statuses/update_with_media.xml?status={0}?media[]={1}";
    2.  
    3.         public static IEnumerator PostTweet(string text, string screenshot, string consumerKey, string consumerSecret, AccessTokenResponse response, PostTweetCallback callback)
    4.         {
    5.             if (string.IsNullOrEmpty(text) || text.Length > 140)
    6.             {
    7.                 Debug.Log(string.Format("PostTweet - text[{0}] is empty or too long.", text));
    8.  
    9.                 callback(false);
    10.             }
    11.             else
    12.             {
    13.                 string url = string.Format(PostTweetURL, UrlEncode(text), screenshot);
    14.                 Dictionary<string, string> parameters = new Dictionary<string, string>();
    15.  
    16.                 parameters.Add("status", text);
    17.                 parameters.Add("media[]", screenshot);
    18.  
    19.                 // Need to fill body since Unity doesn't like an empty request body.
    20.                 byte[] dummmy = new byte[1];
    21.                 dummmy[0] = 0;
    22.  
    23.                 // HTTP header
    24.                 Hashtable headers = new Hashtable();
    25.                 headers["Authorization"] = GetHeaderWithAccessToken("POST", url, consumerKey, consumerSecret, response, parameters);
    26.  
    27.                 WWW web = new WWW(url, dummmy, headers);
    28.                 yield return web;
    29.  
    30.                 if (!string.IsNullOrEmpty(web.error))
    31.                 {
    32.                     Debug.Log(string.Format("PostTweet - failed. {0}", web.error));
    33.                     callback(false);
    34.                 }
    35.                 else
    36.                 {
    37.                     string error = Regex.Match(web.text, @"<error>([^]+)</error>").Groups[1].Value;
    38.  
    39.                     if (!string.IsNullOrEmpty(error))
    40.                     {
    41.                         Debug.Log(string.Format("PostTweet - failed. {0}", error));
    42.                         callback(false);
    43.                     }
    44.                     else
    45.                     {
    46.                        callback(true);
    47.                     }
    48.                 }
    49.             }
    50.         }
     
  4. abgamers

    abgamers

    Joined:
    Dec 22, 2009
    Posts:
    97
    Hi were you able to fix this ???