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

Let's Tweet In Unity Api 1.1

Discussion in 'Editor & General Support' started by dragon376, Mar 2, 2013.

  1. dragon376

    dragon376

    Joined:
    Mar 2, 2013
    Posts:
    9
    Has anyone been able to get the free twitter package "Let's Tweet in Unity" work with the new API 1.1?
    It works fine with API 1.0 but when I try to call any function from the API 1.1 such as search (https://api.twitter.com/1.1/search/tweets.json) or update statuses (https://api.twitter.com/1.1/statuses/update.xml?status={0}) I either get 401 Unauthorized or after tweaking the authorization header I get a 400 Bad Request when I use API 1.1.
    Note that getting the token is independent of the API version, and that does work fine.
    Any ideas?
     
    Last edited: Mar 2, 2013
  2. skateborden

    skateborden

    Joined:
    Apr 1, 2009
    Posts:
    35
    Any luck getting this to work dragon? I'd love to use it if it's possible.
     
  3. abgamers

    abgamers

    Joined:
    Dec 22, 2009
    Posts:
    97
    Even im facing the same issue, any idea how to fix this?
     
  4. dragon376

    dragon376

    Joined:
    Mar 2, 2013
    Posts:
    9
    If I remember correctly API 1.1 requires GET with custom header for authentication.
    The WWW class that unity provides doesn't support this. uniweb (55$) supposedly works on all platform.
    For now I am using httpwebrequest but that won't work on iPhone.
    In any case, to debug http requests you need something like sharkwire.org it allows you to see the request.

    Hope this helps.
     
  5. Voronoi

    Voronoi

    Joined:
    Jul 2, 2012
    Posts:
    571
    Yes, I did get this to work with the 1.1 API and I am using GET to do a search on Twitter:

    Code (csharp):
    1.  
    2.             //string url = string.Format(GetTweetURL, UrlEncode(text));
    3.             string url = GetTweetURL;
    4.  
    5.  
    6.             Dictionary<string, string> parameters = new Dictionary<string, string>();
    7.            
    8.             //these might have to be sorted alphabetically, with 'q' at the end...
    9.             parameters.Add ("count", "20");
    10.             parameters.Add ("lang", "en");
    11.             parameters.Add("q", text);
    12.  
    13.  
    14.            
    15.             //Build the final search URL to match the oAuth signature passed in the header adding the parameters above
    16.             string appendURL = "";
    17.             for( int i = 0; i < parameters.Count; i++){
    18.  
    19.                 if(!parameters.Keys.ElementAt(i).Contains("q")){
    20.  
    21.                     string pre = "";
    22.                     if(i >0)
    23.                         pre = "";
    24.                    
    25.                     appendURL = appendURL + pre + parameters.Keys.ElementAt(i) + "=" + parameters.Values.ElementAt(i);
    26.                 }
    27.             }
    28.            
    29.             // HTTP header
    30.             Hashtable headers = new Hashtable();
    31.             headers["Authorization"] = GetHeaderWithAccessToken("GET", url, consumerKey, consumerSecret, response, parameters);
    32.  
    33.             //Escape all the spaces, quote marks to match what GetHeaderAccess did to the string
    34.             //Have to do this after the oAuth Signature was created so we don't double-encode...
    35.             string query = WWW.EscapeURL(parameters["q"]);
    36.  
    37.             url = "https://api.twitter.com/1.1/search/tweets.json?" + appendURL + "&q=" + query;
    38.  
    39.             Debug.Log("Url posted to web is " + url);
    40.  
    41.             WWW web = new WWW(url, null, headers);
    42.             yield return web;
    43.  
    However, I can't get this to work on iOS. I don't think it is able to send the oAuth headers because that's where it seems to get stuck. Clicking the button to 'Register the game' does not do anything, it is stuck at Twitter.API.GetRequestToken

    Works fine on desktop. Be great if someone can take this further and get it working on iOS!
     
  6. dragon376

    dragon376

    Joined:
    Mar 2, 2013
    Posts:
    9
    Prime31 has an IOS Social Networking plugin that works great, relatively easy to use and most important, works on Unity IOS basic. That is what I ended up using.
     
  7. Bonozo-com

    Bonozo-com

    Joined:
    Jan 7, 2013
    Posts:
    16
    we had already bought the prime[31] iOS Social Networking plug as mentioned by @dragon376 above, but it expires after 2 years and we didn't feel like buying it again (reg price: $120) :mad: ...

    so we used the free "Let's Tweet in Unity" asset, following the steps in the Comments to get it to work with Unity 5.5. However, it only Posts tweets, it does not Get twitter search results. So we implemented @Voronoi's code above, but it has a bug and would not authenticate with Twitter o_O

    Here is the fix:

    Line 25:
    Code (CSharp):
    1.                         appendURL = appendURL + pre + parameters.Keys.ElementAt(i) + "=" + parameters.Values.ElementAt(i) + "&";
    2.  
    Line 37:
    Code (CSharp):
    1.                 url = "https://api.twitter.com/1.1/search/tweets.json?" + appendURL + "q=" + query;
    2.  
    you can see how we did it in this video, where we also reviewed/tried/fixed these other Twitter and JSON libraries in trying to be able to Get twitter search queries:
    - Let's Tweet in Unity from Asset Store
    - TweetSharp
    - Tweetinvi
    - Fabric
    - MoPub
    - JSONUtility from Unity
    -SimpleJSON
    - MiniJSON
    - LitJSON
    - JSONObject
    - Karpach Twitter

    NOTE: Feel free to ask us anything on our live gamedev stream at twitch.com/bonozoapps

    V I D E O
     
    johnnieZombie likes this.
  8. johnnieZombie

    johnnieZombie

    Joined:
    Oct 23, 2012
    Posts:
    27
    @Bonozo-com thanks for sharing this great script fix/update! Can you help us perform a search to retrieve the trending Twitter hashtags please?
     
  9. AliceOliva

    AliceOliva

    Joined:
    Jun 30, 2017
    Posts:
    1
    I know its been awhile since anyone has posted here, but I was wondering if someone might be able to help me anyway. I got Let's Tweet in Unity to work with the Demo, but I'm having trouble getting it to work with Unity UI elements. Basically, when I try to enter the pin I end up catching a NullReferenceException: Object reference not set to an instance of the object, but I'm just not seeing what reference I'm missing.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Pin : MonoBehaviour {
    7.  
    8.     public string CONSUMER_KEY;
    9.     public string CONSUMER_SECRET;
    10.  
    11.     const string PLAYER_PREFS_TWITTER_USER_ID           = "TwitterUserID";
    12.     const string PLAYER_PREFS_TWITTER_USER_SCREEN_NAME  = "TwitterUserScreenName";
    13.     const string PLAYER_PREFS_TWITTER_USER_TOKEN        = "TwitterUserToken";
    14.     const string PLAYER_PREFS_TWITTER_USER_TOKEN_SECRET = "TwitterUserTokenSecret";
    15.  
    16.     Twitter.RequestTokenResponse m_RequestTokenResponse;
    17.     Twitter.AccessTokenResponse m_AccessTokenResponse;
    18.  
    19.     public InputField myPin;
    20.  
    21.     string m_PIN = "";
    22.  
    23.  
    24.     // Use this for initialization
    25.     void Start()
    26.     {
    27.  
    28.         myPin.text = "something lalala...";
    29.        
    30.         LoadTwitterUserInfo();
    31.     }
    32.    
    33.     // Update is called once per frame
    34.     void Update()
    35.     {
    36.    
    37.     }
    38.  
    39.  
    40.     // GUI
    41.        public void EnterPin ()
    42.     {
    43.    
    44.  
    45.         // PIN Input
    46.      
    47.  
    48.         m_PIN = myPin.text;
    49.         print(m_PIN);
    50.  
    51.             //var test = new Twitter.AccessTokenCallback(this.OnAccessTokenCallback);
    52.  
    53.             StartCoroutine (Twitter.API.GetAccessToken (CONSUMER_KEY, CONSUMER_SECRET, m_RequestTokenResponse.Token, m_PIN,
    54.                             new Twitter.AccessTokenCallback (this.OnAccessTokenCallback)));
    55.  
    56.            
    57.     }
    58.  
    59.  
    60.     void LoadTwitterUserInfo()
    61.     {
    62.         m_AccessTokenResponse = new Twitter.AccessTokenResponse();
    63.  
    64.         m_AccessTokenResponse.UserId        = PlayerPrefs.GetString(PLAYER_PREFS_TWITTER_USER_ID);
    65.         m_AccessTokenResponse.ScreenName    = PlayerPrefs.GetString(PLAYER_PREFS_TWITTER_USER_SCREEN_NAME);
    66.         m_AccessTokenResponse.Token         = PlayerPrefs.GetString(PLAYER_PREFS_TWITTER_USER_TOKEN);
    67.         m_AccessTokenResponse.TokenSecret   = PlayerPrefs.GetString(PLAYER_PREFS_TWITTER_USER_TOKEN_SECRET);
    68.  
    69.         if (!string.IsNullOrEmpty(m_AccessTokenResponse.Token) &&
    70.             !string.IsNullOrEmpty(m_AccessTokenResponse.ScreenName) &&
    71.             !string.IsNullOrEmpty(m_AccessTokenResponse.Token) &&
    72.             !string.IsNullOrEmpty(m_AccessTokenResponse.TokenSecret))
    73.         {
    74.             string log = "LoadTwitterUserInfo - succeeded";
    75.             log += "\n    UserId : " + m_AccessTokenResponse.UserId;
    76.             log += "\n    ScreenName : " + m_AccessTokenResponse.ScreenName;
    77.             log += "\n    Token : " + m_AccessTokenResponse.Token;
    78.             log += "\n    TokenSecret : " + m_AccessTokenResponse.TokenSecret;
    79.             print(log);
    80.         }
    81.     }
    82.  
    83.     void OnRequestTokenCallback(bool success, Twitter.RequestTokenResponse response)
    84.     {
    85.         if (success)
    86.         {
    87.             string log = "OnRequestTokenCallback - succeeded";
    88.             log += "\n    Token : " + response.Token;
    89.             log += "\n    TokenSecret : " + response.TokenSecret;
    90.             print(log);
    91.  
    92.             m_RequestTokenResponse = response;
    93.  
    94.             Twitter.API.OpenAuthorizationPage(response.Token);
    95.         }
    96.         else
    97.         {
    98.             print("OnRequestTokenCallback - failed.");
    99.         }
    100.     }
    101.  
    102.     void OnAccessTokenCallback(bool success, Twitter.AccessTokenResponse response)
    103.     {
    104.         if (success)
    105.         {
    106.             string log = "OnAccessTokenCallback - succeeded";
    107.             log += "\n    UserId : " + response.UserId;
    108.             log += "\n    ScreenName : " + response.ScreenName;
    109.             log += "\n    Token : " + response.Token;
    110.             log += "\n    TokenSecret : " + response.TokenSecret;
    111.             print(log);
    112.  
    113.             m_AccessTokenResponse = response;
    114.  
    115.             PlayerPrefs.SetString(PLAYER_PREFS_TWITTER_USER_ID, response.UserId);
    116.             PlayerPrefs.SetString(PLAYER_PREFS_TWITTER_USER_SCREEN_NAME, response.ScreenName);
    117.             PlayerPrefs.SetString(PLAYER_PREFS_TWITTER_USER_TOKEN, response.Token);
    118.             PlayerPrefs.SetString(PLAYER_PREFS_TWITTER_USER_TOKEN_SECRET, response.TokenSecret);
    119.         }
    120.         else
    121.         {
    122.             print("OnAccessTokenCallback - failed.");
    123.         }
    124.     }
    125. }
    The error is for line 54 and I think has something to do with OnAccessCallback method, but I need new eyes.

    Any help would be much appreciated!
     
  10. gohugo

    gohugo

    Joined:
    Jan 12, 2018
    Posts:
    3
    Do you know how we can do a Twitter authentication that are not Pin Based?
     
  11. Geads

    Geads

    Joined:
    Aug 24, 2017
    Posts:
    45
    Looking for help with other GET requests, and also what @gohugo said. A button authenticate would be nice. Trying to figure it out