Example #1
1
        public static IEnumerator PostTweet(string text, string mediaID, string consumerKey, string consumerSecret, AccessTokenResponse response, Text outputText)
        {
            Dictionary<string, string> parameters = new Dictionary<string, string>();
            parameters.Add("status", text);
            parameters.Add("media_ids", mediaID);

            // Add data to the form to post.
            WWWForm form = new WWWForm();
            form.AddField("status", text);
            form.AddField("media_ids", mediaID);

            Debug.Log(mediaID);
            Debug.Log("Posting to Twitter...");
            // HTTP header
            var headers = new Dictionary<string, string>();
            headers["Authorization"] = GetHeaderWithAccessToken("POST", "https://api.twitter.com/1.1/statuses/update.json", consumerKey, consumerSecret, response, parameters);
            WWW web = new WWW("https://api.twitter.com/1.1/statuses/update.json", form.data, headers);
            yield return web;

            Debug.Log(web.text);
            if (web.error != "Null")
            {
                outputText.text = "Upload complete!";
                yield return new WaitForSeconds(2);
                outputText.DOFade(0, 1);
            }
            else
            {
                Debug.Log(web.error);
            }
        }
Example #2
0
    void LoadTwitterUserInfo()
    {
        m_AccessTokenResponse = new Twitter.AccessTokenResponse();

        m_AccessTokenResponse.UserId      = PlayerPrefs.GetString(UserID);
        m_AccessTokenResponse.ScreenName  = PlayerPrefs.GetString(ScreenName);
        m_AccessTokenResponse.Token       = PlayerPrefs.GetString(Token);
        m_AccessTokenResponse.TokenSecret = PlayerPrefs.GetString(TokenSecret);

        if (!string.IsNullOrEmpty(m_AccessTokenResponse.Token) &&
            !string.IsNullOrEmpty(m_AccessTokenResponse.ScreenName) &&
            !string.IsNullOrEmpty(m_AccessTokenResponse.Token) &&
            !string.IsNullOrEmpty(m_AccessTokenResponse.TokenSecret))
        {
            string log = "LoadTwitterUserInfo - succeeded";
            log += "\n    UserId : " + m_AccessTokenResponse.UserId;
            log += "\n    ScreenName : " + m_AccessTokenResponse.ScreenName;
            log += "\n    Token : " + m_AccessTokenResponse.Token;
            log += "\n    TokenSecret : " + m_AccessTokenResponse.TokenSecret;
            Debug.Log(log);

            OpenFunctionalityPanel();
            GetProfile();
        }
        else
        {
            Debug.Log("User not Registered on Twitter");
            OpenMainMenuPanel();
        }
    }
    void OnAccessTokenCallback(bool success, Twitter.AccessTokenResponse response)
    {
        this.enterPinMenu.SetActive(false);
        this.reportBackField.gameObject.SetActive(true);
        this.reportBackField.text = success ? "Twitter Connected!" : "Please try again...";
        Invoke("ClearAndReset", 1.0f);

        if (success)
        {
            string log = "OnAccessTokenCallback - succeeded";
            log += "\n    UserId : " + response.UserId;
            log += "\n    ScreenName : " + response.ScreenName;
            log += "\n    Token : " + response.Token;
            log += "\n    TokenSecret : " + response.TokenSecret;
            Debug.Log(log);

            accessTokenResponse = response;

            PlayerPrefs.SetString(Globals.PLAYER_PREFS_TWITTER_USER_ID, response.UserId);
            PlayerPrefs.SetString(Globals.PLAYER_PREFS_TWITTER_USER_SCREEN_NAME, response.ScreenName);
            PlayerPrefs.SetString(Globals.PLAYER_PREFS_TWITTER_USER_TOKEN, response.Token);
            PlayerPrefs.SetString(Globals.PLAYER_PREFS_TWITTER_USER_TOKEN_SECRET, response.TokenSecret);

            this.enterPinMenu.SetActive(false);
            GoBack();
        }
        else
        {
            Debug.Log("OnAccessTokenCallback - failed.");
        }
    }
        void LoadTwitterUserInfo()
        {
            m_AccessTokenResponse = new Twitter.AccessTokenResponse();

            const string PLAYER_PREFS_TWITTER_USER_ID           = "TwitterUserID";
            const string PLAYER_PREFS_TWITTER_USER_SCREEN_NAME  = "TwitterUserScreenName";
            const string PLAYER_PREFS_TWITTER_USER_TOKEN        = "TwitterUserToken";
            const string PLAYER_PREFS_TWITTER_USER_TOKEN_SECRET = "TwitterUserTokenSecret";

            string m_PIN   = "832955370649759744-Rpm97IGJ5ecZffWo9xb0ekBegJ8Lsbj";
            string m_Tweet = "tsTest";

            m_AccessTokenResponse.UserId      = PlayerPrefs.GetString(PLAYER_PREFS_TWITTER_USER_ID);
            m_AccessTokenResponse.ScreenName  = PlayerPrefs.GetString(PLAYER_PREFS_TWITTER_USER_SCREEN_NAME);
            m_AccessTokenResponse.Token       = PlayerPrefs.GetString(PLAYER_PREFS_TWITTER_USER_TOKEN);
            m_AccessTokenResponse.TokenSecret = PlayerPrefs.GetString(PLAYER_PREFS_TWITTER_USER_TOKEN_SECRET);

            if (!string.IsNullOrEmpty(m_AccessTokenResponse.Token) &&
                !string.IsNullOrEmpty(m_AccessTokenResponse.ScreenName) &&
                !string.IsNullOrEmpty(m_AccessTokenResponse.Token) &&
                !string.IsNullOrEmpty(m_AccessTokenResponse.TokenSecret))
            {
                string log = "LoadTwitterUserInfo - succeeded";
                log += "\n    UserId : " + m_AccessTokenResponse.UserId;
                log += "\n    ScreenName : " + m_AccessTokenResponse.ScreenName;
                log += "\n    Token : " + m_AccessTokenResponse.Token;
                log += "\n    TokenSecret : " + m_AccessTokenResponse.TokenSecret;
            }
        }
Example #5
0
    void OnAccessTokenCallback(bool success, Twitter.AccessTokenResponse response)
    {
        if (success)
        {
            string log = "OnAccessTokenCallback - succeeded";
            log += "\n    UserId : " + response.UserId;
            log += "\n    ScreenName : " + response.ScreenName;
            log += "\n    Token : " + response.Token;
            log += "\n    TokenSecret : " + response.TokenSecret;
            Debug.Log(log);
            Debug.Log("Name : " + response.ScreenName);
            m_AccessTokenResponse = response;

            //save values for next session
            PlayerPrefs.SetString(UserID, response.UserId);
            PlayerPrefs.SetString(ScreenName, response.ScreenName);
            PlayerPrefs.SetString(Token, response.Token);
            PlayerPrefs.SetString(TokenSecret, response.TokenSecret);

            OpenFunctionalityPanel();
            GetProfile();
        }
        else
        {
            Debug.Log("OnAccessTokenCallback - failed.");
        }
    }
Example #6
0
        // Token: 0x06002F3A RID: 12090 RVA: 0x000E5068 File Offset: 0x000E3468
        public static IEnumerator GetAccessToken(string consumerKey, string consumerSecret, string requestToken, string pin, AccessTokenCallback callback)
        {
            WWW web = API.WWWAccessToken(consumerKey, consumerSecret, requestToken, pin);

            yield return(web);

            if (!string.IsNullOrEmpty(web.error))
            {
                Debug.Log(string.Format("GetAccessToken - failed. error : {0}", web.error));
                callback(false, null);
            }
            else
            {
                AccessTokenResponse accessTokenResponse = new AccessTokenResponse
                {
                    Token       = Regex.Match(web.text, "oauth_token=([^&]+)").Groups[1].Value,
                    TokenSecret = Regex.Match(web.text, "oauth_token_secret=([^&]+)").Groups[1].Value,
                    UserId      = Regex.Match(web.text, "user_id=([^&]+)").Groups[1].Value,
                    ScreenName  = Regex.Match(web.text, "screen_name=([^&]+)").Groups[1].Value
                };
                if (!string.IsNullOrEmpty(accessTokenResponse.Token) && !string.IsNullOrEmpty(accessTokenResponse.TokenSecret) && !string.IsNullOrEmpty(accessTokenResponse.UserId) && !string.IsNullOrEmpty(accessTokenResponse.ScreenName))
                {
                    callback(true, accessTokenResponse);
                }
                else
                {
                    Debug.Log(string.Format("GetAccessToken - failed. response : {0}", web.text));
                    callback(false, null);
                }
            }
            yield break;
        }
    void LoadTwitterUserInfo()
    {
        m_AccessTokenResponse = new Twitter.AccessTokenResponse();

        m_AccessTokenResponse.UserId = PlayerPrefs.GetString(PLAYER_PREFS_TWITTER_USER_ID);
        m_AccessTokenResponse.ScreenName = PlayerPrefs.GetString(PLAYER_PREFS_TWITTER_USER_SCREEN_NAME);
        m_AccessTokenResponse.Token = PlayerPrefs.GetString(PLAYER_PREFS_TWITTER_USER_TOKEN);
        m_AccessTokenResponse.TokenSecret = PlayerPrefs.GetString(PLAYER_PREFS_TWITTER_USER_TOKEN_SECRET);

        userID = PlayerPrefs.GetString(PLAYER_PREFS_TWITTER_USER_ID);
        ScreenName = PlayerPrefs.GetString(PLAYER_PREFS_TWITTER_USER_SCREEN_NAME);
        Token = PlayerPrefs.GetString(PLAYER_PREFS_TWITTER_USER_TOKEN);
        TokenSecret = PlayerPrefs.GetString(PLAYER_PREFS_TWITTER_USER_TOKEN_SECRET);


        if (!string.IsNullOrEmpty(m_AccessTokenResponse.Token) &&
            !string.IsNullOrEmpty(m_AccessTokenResponse.ScreenName) &&
            !string.IsNullOrEmpty(m_AccessTokenResponse.Token) &&
            !string.IsNullOrEmpty(m_AccessTokenResponse.TokenSecret))
        {
            string log = "LoadTwitterUserInfo - succeeded";
            log += "\n    UserId : " + m_AccessTokenResponse.UserId;
            log += "\n    ScreenName : " + m_AccessTokenResponse.ScreenName;
            log += "\n    Token : " + m_AccessTokenResponse.Token;
            log += "\n    TokenSecret : " + m_AccessTokenResponse.TokenSecret;
            Debug.Log(log);
        }
    }
 void OnSuccess_AuthenticateTwitter(bool success, Twitter.AccessTokenResponse response)
 {
     if (success)
     {
         BrainCloudWrapper.GetBC().IdentityService.MergeTwitterIdentity(response.UserId, response.Token, response.TokenSecret, Attach_Success, Attach_Fail);
         //BrainCloudWrapper.GetBC().IdentityService.AttachTwitterIdentity(response.UserId, response.Token, response.TokenSecret, Attach_Success, Attach_Fail);
     }
     else
     {
         Debug.Log("OnAccessTokenCallback - failed.");
     }
 }
 void OnSuccess_AttachTwitter(bool success, Twitter.AccessTokenResponse response)
 {
     if (success)
     {
         _bc.IdentityService.AttachTwitterIdentity(response.UserId, response.Token, response.TokenSecret, Attach_Success, Attach_Fail);
         //_bc.IdentityService.AttachTwitterIdentity(response.UserId, response.Token, response.TokenSecret, Attach_Success, Attach_Fail);
     }
     else
     {
         Debug.Log("OnAccessTokenCallback - failed.");
     }
 }
    void OnSuccess_AuthenticateTwitter(bool success, Twitter.AccessTokenResponse response)
    {
        if (success)
        {
            string log = "OnAccessTokenCallback - succeeded";
            log += "\n    UserId : " + response.UserId;
            log += "\n    ScreenName : " + response.ScreenName;
            log += "\n    Token : " + response.Token;
            log += "\n    TokenSecret : " + response.TokenSecret;
            print(log);

            m_AccessTokenResponse = response;

            BrainCloudWrapper.GetBC().AuthenticationService.AuthenticateTwitter(response.UserId, response.Token, response.TokenSecret, true, OnSuccess_Authenticate, OnError_Authenticate);
        }
        else
        {
            print("OnAccessTokenCallback - failed.");
        }
    }
Example #11
0
    void OnAccessTokenCallback(bool success, Twitter.AccessTokenResponse response)
    {
        if (success)
        {
            // 認証
            Access    = response;
            Text.text = Ready;
            Input.placeholder.GetComponent <Text>().text = PlaceholderReady;
            Input.text = "";

            // 保存
            string config = "";
            config += Access.Token + ",";
            config += Access.TokenSecret + ",";
            config += Access.UserId + ",";
            config += Access.ScreenName + ",";
            config  = AESCryption.Encrypt(config);
            File.WriteAllText(FileName, config);
        }
    }
Example #12
0
    private void LoadTwitterUserInfo()
    {
        m_AccessTokenResponse = new Twitter.AccessTokenResponse();

        m_AccessTokenResponse.UserId      = VINICOLA_USER_ID;
        m_AccessTokenResponse.ScreenName  = VINICOLA_USER_SCREEN_NAME;
        m_AccessTokenResponse.Token       = VINICOLA_USER_TOKEN;
        m_AccessTokenResponse.TokenSecret = VINICOLA_USER_TOKEN_SECRET;

        if (!string.IsNullOrEmpty(m_AccessTokenResponse.Token) &&
            !string.IsNullOrEmpty(m_AccessTokenResponse.ScreenName) &&
            !string.IsNullOrEmpty(m_AccessTokenResponse.Token) &&
            !string.IsNullOrEmpty(m_AccessTokenResponse.TokenSecret))
        {
            string log = "LoadTwitterUserInfo - succeeded";
            log += "\n    UserId : " + m_AccessTokenResponse.UserId;
            log += "\n    ScreenName : " + m_AccessTokenResponse.ScreenName;
            log += "\n    Token : " + m_AccessTokenResponse.Token;
            log += "\n    TokenSecret : " + m_AccessTokenResponse.TokenSecret;
            Debug.Log(log);
        }
    }
Example #13
0
    void LoadTwitterUserInfo()
    {
        accessTokenResponse = new Twitter.AccessTokenResponse();

        accessTokenResponse.UserId      = PlayerPrefs.GetString(Globals.PLAYER_PREFS_TWITTER_USER_ID);
        accessTokenResponse.ScreenName  = PlayerPrefs.GetString(Globals.PLAYER_PREFS_TWITTER_USER_SCREEN_NAME);
        accessTokenResponse.Token       = PlayerPrefs.GetString(Globals.PLAYER_PREFS_TWITTER_USER_TOKEN);
        accessTokenResponse.TokenSecret = PlayerPrefs.GetString(Globals.PLAYER_PREFS_TWITTER_USER_TOKEN_SECRET);

        if (!string.IsNullOrEmpty(accessTokenResponse.Token) &&
            !string.IsNullOrEmpty(accessTokenResponse.ScreenName) &&
            !string.IsNullOrEmpty(accessTokenResponse.Token) &&
            !string.IsNullOrEmpty(accessTokenResponse.TokenSecret))
        {
            string log = "LoadTwitterUserInfo - succeeded";
            log += "\n    UserId : " + accessTokenResponse.UserId;
            log += "\n    ScreenName : " + accessTokenResponse.ScreenName;
            log += "\n    Token : " + accessTokenResponse.Token;
            log += "\n    TokenSecret : " + accessTokenResponse.TokenSecret;
            Debug.Log(log);
        }
    }
Example #14
0
    void LoadTwitterUserInfo()
    {
        m_AccessTokenResponse = new Twitter.AccessTokenResponse();

        m_AccessTokenResponse.UserId      = PlayerPrefs.GetString(PLAYER_PREFS_TWITTER_USER_ID);
        m_AccessTokenResponse.ScreenName  = PlayerPrefs.GetString(PLAYER_PREFS_TWITTER_USER_SCREEN_NAME);
        m_AccessTokenResponse.Token       = PlayerPrefs.GetString(PLAYER_PREFS_TWITTER_USER_TOKEN);
        m_AccessTokenResponse.TokenSecret = PlayerPrefs.GetString(PLAYER_PREFS_TWITTER_USER_TOKEN_SECRET);
        twitterUserIdForCheck             = m_AccessTokenResponse.UserId;

        if (!string.IsNullOrEmpty(m_AccessTokenResponse.Token) &&
            !string.IsNullOrEmpty(m_AccessTokenResponse.ScreenName) &&
            !string.IsNullOrEmpty(m_AccessTokenResponse.Token) &&
            !string.IsNullOrEmpty(m_AccessTokenResponse.TokenSecret))
        {
            string log = "LoadTwitterUserInfo - succeeded";
            log += "\n    UserId : " + m_AccessTokenResponse.UserId;
            log += "\n    ScreenName : " + m_AccessTokenResponse.ScreenName;
            log += "\n    Token : " + m_AccessTokenResponse.Token;
            log += "\n    TokenSecret : " + m_AccessTokenResponse.TokenSecret;
            print(log);
        }
    }
        void OnAccessTokenCallback(bool success, Twitter.AccessTokenResponse response)
        {
            if (success)
            {
                string log = "OnAccessTokenCallback - succeeded";
                log += "\n    UserId : " + response.UserId;
                log += "\n    ScreenName : " + response.ScreenName;
                log += "\n    Token : " + response.Token;
                log += "\n    TokenSecret : " + response.TokenSecret;
                print(log);

                m_AccessTokenResponse = response;

                PlayerPrefs.SetString(PLAYER_PREFS_TWITTER_USER_ID, response.UserId);
                PlayerPrefs.SetString(PLAYER_PREFS_TWITTER_USER_SCREEN_NAME, response.ScreenName);
                PlayerPrefs.SetString(PLAYER_PREFS_TWITTER_USER_TOKEN, response.Token);
                PlayerPrefs.SetString(PLAYER_PREFS_TWITTER_USER_TOKEN_SECRET, response.TokenSecret);
            }
            else
            {
                print("OnAccessTokenCallback - failed.");
            }
        }
Example #16
0
    void OnAccessTokenCallback(bool success, Twitter.AccessTokenResponse response)
    {
        if (success)
        {
            string log = "アカウントを認証しました";
            log += "\n" + response.ScreenName + "さん";

            Debug.Log(log);


            m_AccessTokenResponse = response;

            PlayerPrefs.SetString(PLAYER_PREFS_TWITTER_USER_ID, response.UserId);
            PlayerPrefs.SetString(PLAYER_PREFS_TWITTER_USER_SCREEN_NAME, response.ScreenName);
            PlayerPrefs.SetString(PLAYER_PREFS_TWITTER_USER_TOKEN, response.Token);
            PlayerPrefs.SetString(PLAYER_PREFS_TWITTER_USER_TOKEN_SECRET, response.TokenSecret);
        }
        else
        {
            Debug.Log("アカウントの認証に失敗しました。PINコードが正しいか確認してください");
            //AcceceSuccess.text = "アカウントの認証に失敗しました";
        }
    }
Example #17
0
    void OnAccessTokenCallback(bool success, Twitter.AccessTokenResponse response)
    {
        if (success)
        {
            string log = "OnAccessTokenCallback - succeeded";
            log += "\n    UserId : " + response.UserId;
            log += "\n    ScreenName : " + response.ScreenName;
            log += "\n    Token : " + response.Token;
            log += "\n    TokenSecret : " + response.TokenSecret;
            print(log);
  
            m_AccessTokenResponse = response;

            PlayerPrefs.SetString(PLAYER_PREFS_TWITTER_USER_ID, response.UserId);
            PlayerPrefs.SetString(PLAYER_PREFS_TWITTER_USER_SCREEN_NAME, response.ScreenName);
            PlayerPrefs.SetString(PLAYER_PREFS_TWITTER_USER_TOKEN, response.Token);
            PlayerPrefs.SetString(PLAYER_PREFS_TWITTER_USER_TOKEN_SECRET, response.TokenSecret);
        }
        else
        {
            print("OnAccessTokenCallback - failed.");
        }
    }
Example #18
0
        public static void GetTinyTimeline(string consumerKey, string consumerSecret, AccessTokenResponse response)
        {
            var oauth = new TinyTwitter.OAuthInfo
            {
                AccessToken    = "YOUR ACCESS TOKEN",
                AccessSecret   = "YOUR ACCES SECRET",
                ConsumerKey    = "YOUR CONSUMER KEY",
                ConsumerSecret = "YOUR CONSUMER SECRET"
            };

            oauth.AccessToken    = response.Token;
            oauth.AccessSecret   = response.TokenSecret;
            oauth.ConsumerKey    = consumerKey;
            oauth.ConsumerSecret = consumerSecret;

            var twitter = new TinyTwitter.TinyTwitter(oauth);

            // Update status, i.e, post a new tweet
            //twitter.UpdateStatus("I'm tweeting from C#");

            // Get home timeline tweets
            var tweets = twitter.GetHomeTimeline();

            foreach (var tweet in tweets)
            {
                Console.WriteLine("{0}: {1}", tweet.UserName, tweet.Text);
            }
        }
Example #19
0
        public static IEnumerator PostTweet(string text, string consumerKey, string consumerSecret, AccessTokenResponse response, PostTweetCallback callback)
        {
            if (string.IsNullOrEmpty(text) || text.Length > 140)
            {
                Debug.Log(string.Format("PostTweet - text[{0}] is empty or too long.", text));

                callback(false);
            }
            else
            {
                Dictionary <string, string> parameters = new Dictionary <string, string>();
                parameters.Add("status", text);

                // Add data to the form to post.
                WWWForm form = new WWWForm();
                form.AddField("status", text);

                // HTTP header
                Dictionary <string, string> headers = new Dictionary <string, string>();
                headers["Authorization"] = GetHeaderWithAccessToken("POST", PostTweetURL, consumerKey, consumerSecret, response, parameters);

                WWW web = new WWW(PostTweetURL, form.data, headers);
                yield return(web);

                if (!string.IsNullOrEmpty(web.error))
                {
                    Debug.Log(string.Format("PostTweet - failed. {0}\n{1}", web.error, web.text));
                    callback(false);
                }
                else
                {
                    string error = Regex.Match(web.text, @"<error>([^&]+)</error>").Groups[1].Value;

                    if (!string.IsNullOrEmpty(error))
                    {
                        Debug.Log(string.Format("PostTweet - failed. {0}", error));
                        callback(false);
                    }
                    else
                    {
                        callback(true);
                    }

                    Debug.Log("PostTweet - " + web.text);
                }
            }
        }
Example #20
0
        private static IEnumerator CommonRequest(string api, Method method, Dictionary <string, string> parameters, string consumerKey, string consumerSecret, AccessTokenResponse response, CommonCallback callback)
        {
            string baseURL = string.Format(ApiBaseURL, api);
            string url     = baseURL;

            byte[] data = null;

            if (parameters == null)
            {
                parameters = new Dictionary <string, string>();
            }

            if (method == Method.GET)
            {
                url = BuildURL(url, parameters);
            }

            if (method == Method.POST)
            {
                WWWForm form = new WWWForm();
                foreach (var pair in parameters)
                {
                    form.AddField(pair.Key, pair.Value);
                }
                data = form.data;
            }

            //var headers = new Hashtable();
            Dictionary <string, string> headers = new Dictionary <string, string>();

            headers["Authorization"] = GetHeaderWithAccessToken(method.ToString(), url, consumerKey, consumerSecret, response, parameters);

            WWW web = new WWW(url, data, headers);

            yield return(web);

            if (!string.IsNullOrEmpty(web.error))
            {
                Debug.Log(string.Format("{0} failed. {1}\n{2}", api, web.error, web.text));
                callback(false, web.error);
            }
            else
            {
                string error = Regex.Match(web.text, @"<error>([^&]+)</error>").Groups[1].Value;

                if (!string.IsNullOrEmpty(error))
                {
                    Debug.Log(string.Format("{0} failed. {1}\n{2}", api, web.error, web.text));
                    callback(false, web.text);
                }
                else
                {
                    callback(true, web.text);
                }
            }
        }
Example #21
0
        private static string GetHeaderWithAccessToken(string httpRequestType, string apiURL, string consumerKey, string consumerSecret, AccessTokenResponse response, Dictionary<string, string> parameters)
        {
            AddDefaultOAuthParams(parameters, consumerKey, consumerSecret);

            parameters.Add("oauth_token", response.Token);
            parameters.Add("oauth_token_secret", response.TokenSecret);

            return GetFinalOAuthHeader(httpRequestType, apiURL, parameters);
        }
Example #22
0
        public static IEnumerator GetAccessToken(string consumerKey, string consumerSecret, string requestToken, string pin, AccessTokenCallback callback)
        {
            WWW web = WWWAccessToken(consumerKey, consumerSecret, requestToken, pin);

            yield return web;

            if (!string.IsNullOrEmpty(web.error))
            {
                Debug.Log(string.Format("GetAccessToken - failed. error : {0}", web.error));
                callback(false, null);
            }
            else
            {
                AccessTokenResponse response = new AccessTokenResponse
                                               {
                                                   Token = Regex.Match(web.text, @"oauth_token=([^&]+)").Groups[1].Value,
                                                   TokenSecret = Regex.Match(web.text, @"oauth_token_secret=([^&]+)").Groups[1].Value,
                                                   UserId = Regex.Match(web.text, @"user_id=([^&]+)").Groups[1].Value,
                                                   ScreenName = Regex.Match(web.text, @"screen_name=([^&]+)").Groups[1].Value
                                               };

                if (!string.IsNullOrEmpty(response.Token) &&
                    !string.IsNullOrEmpty(response.TokenSecret) &&
                    !string.IsNullOrEmpty(response.UserId) &&
                    !string.IsNullOrEmpty(response.ScreenName))
                {
                    callback(true, response);
                }
                else
                {
                    Debug.Log(string.Format("GetAccessToken - failed. response : {0}", web.text));

                    callback(false, null);
                }
            }
        }
Example #23
0
        public static IEnumerator SendDirectMessage(string text, string USERID, string consumerKey, string consumerSecret, AccessTokenResponse response, PostDMCallback callback)
        {
            if (string.IsNullOrEmpty(text) || text.Length > 140)
            {
                Debug.Log(string.Format("PostTweet - text[{0}] is empty or too long.", text));

                callback(false);
            }
            else
            {
                Dictionary <string, string> parameters = new Dictionary <string, string>();

                parameters.Add("text", text);
                parameters.Add("user_id", USERID);

                // Add data to the form to post.
                WWWForm form = new WWWForm();
                form.AddField("text", text);
                form.AddField("user_id", USERID);

                // HTTP header
                Dictionary <string, string> headers = new Dictionary <string, string>();
                headers["Authorization"] = GetHeaderWithAccessToken("POST", PostMSGURL, consumerKey, consumerSecret, response, parameters);

                WWW web = new WWW(PostMSGURL, form.data, headers);
                yield return(web);

                if (!string.IsNullOrEmpty(web.error))
                {
                    Debug.Log(string.Format("Post Message Sending - failed. {0}\n{1}", web.error, web.text));
                    callback(false);
                }
                else
                {
                    string error = Regex.Match(web.text, @"<error>([^&]+)</error>").Groups[1].Value;

                    if (!string.IsNullOrEmpty(error))
                    {
                        Debug.Log(string.Format("Message Send - failed. {0}", error));
                        callback(false);
                    }
                    else
                    {
                        callback(true);
                    }

                    Debug.Log("Message Send - " + web.text);
                    var d = JsonConvert.DeserializeObject <RootObject2>(web.text);
                    NameOfRecipent = d.recipient_screen_name;
                    Debug.Log("Message Sent to " + d.recipient_screen_name + " Succcessfully!");
                }
            }
        }
Example #24
0
        public static IEnumerator GetHashtag(string hashtag, int num, string consumerKey, string consumerSecret, AccessTokenResponse response, GetTimelineCallback callback)
        {
            string url = "https://api.twitter.com/1.1/search/tweets.json";

            List <String> forbiddenHash = new List <String>();

            forbiddenHash.Add(hashtag);
            forbiddenHash.Add("GGJ18");
            forbiddenHash.Add("GGJ");

            Dictionary <string, string> parameters = new Dictionary <string, string>();

            //these might have to be sorted alphabetically, with 'q' at the end...
            parameters.Add("count", num.ToString());
            //parameters.Add("lang", "en");// NOTE: OPTIONAL
            parameters.Add("q", hashtag);


            //Build the final search URL to match the oAuth signature passed in the header adding the parameters above
            string appendURL = "";

            for (int i = 0; i < parameters.Count; i++)
            {
                if (!parameters.Keys.ElementAt(i).Contains("q"))
                {
                    string pre = "";
                    if (i > 0)
                    {
                        pre = "";
                    }

                    appendURL = appendURL + pre + parameters.Keys.ElementAt(i) + "=" + parameters.Values.ElementAt(i) + "&";
                }
            }

            // HTTP header
            Dictionary <string, string> headers = new Dictionary <string, string>();

            headers["Authorization"] = GetHeaderWithAccessToken("GET", url, consumerKey, consumerSecret, response, parameters);

            //Escape all the spaces, quote marks to match what GetHeaderAccess did to the string
            //Have to do this after the oAuth Signature was created so we don't double-encode...
            string query = WWW.EscapeURL(parameters["q"]);

            url = "https://api.twitter.com/1.1/search/tweets.json?" + appendURL + "q=" + query;

            Debug.Log("Url posted to web is " + url);

            WWW            web = new WWW(url, null, headers);
            WaitForSeconds w;

            while (!web.isDone)
            {
                w = new WaitForSeconds(0.1f);
                w.ToString();
            }
            Debug.Log("GetTimeline - " + web.text);

            List <TweetStructure> tweetsToReturn = new List <TweetStructure>();
            var tweets = JSON.Parse(web.text);

            Debug.Log("# of Tweets: " + tweets["statuses"].Count);
            for (int i = 0; i < tweets["statuses"].Count; i++)
            {
                string text = tweets["statuses"] [i] ["text"];
                foreach (string key in forbiddenHash)
                {
                    string realHash = "#" + key;
                    text = text.Replace(realHash, "");
                }
                text = text.Replace("RT @", "");
                text = RemoveSpecialCharacters(text);
                TweetStructure toAdd = new TweetStructure();
                toAdd.text     = text.Trim();
                toAdd.username = tweets["statuses"] [i] ["user"]["name"];
                toAdd.image    = tweets["statuses"] [i] ["user"]["profile_image_url"];
                tweetsToReturn.Add(toAdd);
                Debug.Log("Tweet #" + i + " " + tweetsToReturn.ToString());
            }

            //WWW web = new WWW(GetTimelineURL, dummmy, headers);
            //yield return web;

            if (!string.IsNullOrEmpty(web.error))
            {
                Debug.Log(string.Format("GetTimeline1 - web error - failed. {0}\n{1}", web.error, web.text));
                callback(false, null);
            }
            else
            {
                string error = Regex.Match(web.text, @"<error>([^&]+)</error>").Groups[1].Value;

                if (!string.IsNullOrEmpty(error))
                {
                    Debug.Log(string.Format("GetTimeline - bad response - failed. {0}", error));
                    callback(false, null);
                }
                else
                {
                    //needs to be a twitter structure
                    callback(true, tweetsToReturn);
                }
            }

            yield return(web);
        }
Example #25
0
        public static IEnumerator GetMentionsTimeLine(string userId, int num, string consumerKey, string consumerSecret, AccessTokenResponse response, GetTimelineCallback callback)
        {
            string GetTimelineURL = "https://api.twitter.com/1.1/statuses/mentions_timeline.json";


            Dictionary <string, string> parameters = new Dictionary <string, string>();

            parameters.Add("count", num.ToString());



            //Build the final search URL to match the oAuth signature passed in the header adding the parameters above
            string appendURL = "";

            for (int i = 0; i < parameters.Count; i++)
            {
                if (!parameters.Keys.ElementAt(i).Contains("q"))
                {
                    string pre = "";
                    if (i > 0)
                    {
                        pre = "";
                    }

                    appendURL = appendURL + pre + parameters.Keys.ElementAt(i) + "=" + parameters.Values.ElementAt(i) + "&";
                }
            }
            appendURL = appendURL.Remove(appendURL.Length - 1);

            // HTTP header
            Dictionary <string, string> headers = new Dictionary <string, string>();

            headers["Authorization"] = GetHeaderWithAccessToken("GET", GetTimelineURL, consumerKey, consumerSecret, response, parameters);

            //Escape all the spaces, quote marks to match what GetHeaderAccess did to the string
            //Have to do this after the oAuth Signature was created so we don't double-encode...

            GetTimelineURL = GetTimelineURL + "?" + appendURL;

            Debug.Log("Url posted to web is " + GetTimelineURL);

            WWW            web = new WWW(GetTimelineURL, null, headers);
            WaitForSeconds w;

            while (!web.isDone)
            {
                w = new WaitForSeconds(0.1f);
                w.ToString();
            }
            Debug.Log("GetTimeLineForUser - " + web.text);


            var tweets = JSON.Parse(web.text);

            string[] toReturn = new string[tweets.Count];
            Debug.Log("# of Tweets: " + tweets.Count);
            for (int i = 0; i < tweets.Count; i++)
            {
                string text = tweets [i] ["text"];
                toReturn[i] = text.Replace("@" + userId, "");
                toReturn[i] = toReturn [i].Trim();
                Debug.Log("Tweet #" + i + " " + toReturn[i]);
            }

            if (!string.IsNullOrEmpty(web.error))
            {
                Debug.Log(string.Format("GetTimeLineForUser - web error - failed. {0}\n{1}", web.error, web.text));
                callback(false, null);
            }
            else
            {
                string error = Regex.Match(web.text, @"<error>([^&]+)</error>").Groups[1].Value;

                if (!string.IsNullOrEmpty(error))
                {
                    Debug.Log(string.Format("GetTimeLineForUser - bad response - failed. {0}", error));
                    callback(false, null);
                }
                else
                {
                    //needs to be a twitter structure now
                    //callback(true,toReturn);
                }
            }

            yield return(web);
        }
Example #26
0
        public static IEnumerator PostMedia(byte[] image, string consumerKey, string consumerSecret, AccessTokenResponse response, PostMediaCollBack callback)
        {
            string textImage = Convert.ToBase64String(image);

            string media_data = "" + textImage;
            Dictionary <string, string> parameters = new Dictionary <string, string>();

            parameters.Add("media_data", media_data);

            // Add data to the form to post.
            WWWForm form = new WWWForm();

            form.AddField("media_data", media_data);


            // HTTP header
            var headers = new Dictionary <string, string>();

            headers["Authorization"] = GetHeaderWithAccessToken("POST", PostMediaURL, consumerKey, consumerSecret, response, parameters);

            WWW web = new WWW(PostMediaURL, form.data, headers);

            yield return(web);

            if (!string.IsNullOrEmpty(web.error))
            {
                Debug.Log(string.Format("PostTweet - failed. {0}\n{1}", web.error, web.text));
                callback(false, "");
            }
            else
            {
                string error = Regex.Match(web.text, @"<error>([^&]+)</error>").Groups[1].Value;

                if (!string.IsNullOrEmpty(error))
                {
                    Debug.Log(string.Format("PostTweet - failed. {0}", error));
                    callback(false, "");
                }
                else
                {
                    Debug.Log("txttt" + web.text);

                    string data = "";

                    bool isIn = false;
                    for (int i = 0; i < web.text.Length; i++)
                    {
                        char c  = web.text[i];
                        bool ok = '0' <= c && c <= '9';

                        if (isIn && !ok)
                        {
                            break;
                        }
                        if (!isIn && ok)
                        {
                            isIn = true;
                        }

                        if (isIn)
                        {
                            data += c;
                        }
                    }
                    //string data = Regex.Match(web.text, @"media_id_string=([^&]+)").Groups[1].Value;
                    callback(true, data);
                }
            }
        }
Example #27
0
 public static IEnumerator PostTweet(string text, string consumerKey, string consumerSecret, AccessTokenResponse response, PostTweetCallback callback)
 {
     return(PostTweet(text, "", consumerKey, response, callback));
 }
Example #28
0
        public static IEnumerator PostTweet(string text, string mediaString, string consumerKey, string consumerSecret, AccessTokenResponse response, PostTweetCallback callback)
        {
            if (string.IsNullOrEmpty(text) || text.Length > 140)
            {
                Debug.Log(string.Format("PostTweet - text[{0}] is empty or too long.", text));

                callback(false);
            }
            else
            {
                Debug.Log("media string>>" + mediaString);
                Dictionary <string, string> mediaParameters = new Dictionary <string, string>();

                mediaParameters.Add("status", text);
                mediaParameters.Add("media_data", mediaString);

                // Add data to the form to post.
                WWWForm mediaForm = new WWWForm();
                mediaForm.AddField("media_data", mediaString);
                mediaForm.AddField("status", text);

                //Debug.Log (System.Convert.ToBase64String (File.ReadAllBytes (filePath)));
                string UploadMediaURL = "https://upload.twitter.com/1.1/media/upload.json";
                // HTTP header
                Dictionary <string, string> mediaHeaders = new Dictionary <string, string>();
                string url  = UploadMediaURL;
                string auth = GetHeaderWithAccessToken("POST", UploadMediaURL, consumerKey, consumerSecret, response, mediaParameters);
                mediaHeaders.Add("Authorization", auth);
                mediaHeaders.Add("Content-Transfer-Encoding", "base64");

                WWW mw = new WWW(UploadMediaURL, mediaForm.data, mediaHeaders);
                yield return(mw);

                string mID = Regex.Match(mw.text, @"(\Dmedia_id\D\W)(\d*)").Groups[2].Value;

                Debug.Log("response from media request : " + mw.text);

                Debug.Log("mID = " + mID);

                if (!string.IsNullOrEmpty(mw.error))
                {
                    Debug.Log(string.Format("PostTweet - failed. {0}\n{1}", mw.error, mw.text));
                    callback(false);
                }
                else
                {
                    string error = Regex.Match(mw.text, @"<error>([^&]+)</error>").Groups[1].Value;

                    if (!string.IsNullOrEmpty(error))
                    {
                        Debug.Log(string.Format("PostTweet - failed. {0}", error));
                        callback(false);
                    }
                    else
                    {
                        callback(true);
                    }
                }

                Dictionary <string, string> parameters = new Dictionary <string, string>();
                parameters.Add("status", text);
                parameters.Add("media_ids", mID);

                // Add data to the form to post.
                WWWForm form = new WWWForm();
                form.AddField("status", text);
                form.AddField("media_ids", mID);

                // HTTP header
                var headers = new Dictionary <string, string>();
                headers["Authorization"] = GetHeaderWithAccessToken("POST", PostTweetURL, consumerKey, consumerSecret, response, parameters);

                WWW web = new WWW(PostTweetURL, form.data, headers);
                yield return(web);

                if (!string.IsNullOrEmpty(web.error))
                {
                    Debug.Log(string.Format("PostTweet - failed. {0}\n{1}", web.error, web.text));
                    callback(false);
                }
                else
                {
                    string error = Regex.Match(web.text, @"<error>([^&]+)</error>").Groups[1].Value;

                    if (!string.IsNullOrEmpty(error))
                    {
                        Debug.Log(string.Format("PostTweet - failed. {0}", error));
                        callback(false);
                    }
                    else
                    {
                        callback(true);
                    }
                }
            }
        }
Example #29
0
        //int OtherUserId;



        public static IEnumerator GetProfileInfo(string text, string ID, string consumerKey, string consumerSecret, AccessTokenResponse response, PostGetProfileCallback callback)
        {
            string url = "https://api.twitter.com/1.1/users/show.json";


            Dictionary <string, string> parameters = new Dictionary <string, string>();

            parameters.Add("screen_name", text);
            string appendURL = "";

            for (int i = 0; i < parameters.Count; i++)
            {
                if (!parameters.Keys.ElementAt(i).Contains("q"))
                {
                    string pre = "";
                    if (i > 0)
                    {
                        pre = "";
                    }

                    appendURL = appendURL + pre + parameters.Keys.ElementAt(i) + "=" + parameters.Values.ElementAt(i) + "&";
                }
            }

            Dictionary <string, string> headers = new Dictionary <string, string>();

            headers["Authorization"] = GetHeaderWithAccessToken("GET", url, consumerKey, consumerSecret, response, parameters);

            url = "https://api.twitter.com/1.1/users/show.json?" + appendURL;

            WWW web = new WWW(url, null, headers);

            yield return(web);

            if (!string.IsNullOrEmpty(web.error))
            {
                Debug.Log(string.Format("Get Profile - failed. {0}\n{1}", web.error, web.text));
                callback(false);
            }
            else
            {
                string error = Regex.Match(web.text, @"<error>([^&]+)</error>").Groups[1].Value;

                if (!string.IsNullOrEmpty(error))
                {
                    Debug.Log(string.Format("Get Profile - failed. {0}", error));
                    callback(false);
                }
                else
                {
                    callback(true);
                }


                //converting JSON object to classes in C#
                var d    = JsonConvert.DeserializeObject <RootObject> (web.text);
                WWW web2 = new WWW(d.profile_image_url_https);
                yield return(web2);

                DemoClass.Instance.UserProfilePIC.sprite = Sprite.Create(web2.texture, new Rect(0, 0, web2.texture.width, web2.texture.height), new Vector2(0, 0));
                DemoClass.Instance.Username.text         = d.name;
                DemoClass.Instance.FollowersCount.text   = d.followers_count.ToString();
                DemoClass.Instance.FollowingCount.text   = d.friends_count.ToString();
            }
        }
Example #30
0
 private void RevokeToken() {
     _accessTokenResponse = new Twitter.AccessTokenResponse();
     PlayerPrefs.DeleteKey(PLAYER_PREFS_TWITTER_USER_ID);
     PlayerPrefs.DeleteKey(PLAYER_PREFS_TWITTER_USER_SCREEN_NAME);
     PlayerPrefs.DeleteKey(PLAYER_PREFS_TWITTER_USER_TOKEN);
     PlayerPrefs.DeleteKey(PLAYER_PREFS_TWITTER_USER_TOKEN_SECRET);
     _gotToken = false;
 }
    void OnAccessTokenCallback(bool success, Twitter.AccessTokenResponse response)
    {
        if (success)
        {
            string log = "OnAccessTokenCallback - succeeded";
            log += "\n    UserId : " + response.UserId;
            log += "\n    ScreenName : " + response.ScreenName;
            log += "\n    Token : " + response.Token;
            log += "\n    TokenSecret : " + response.TokenSecret;
            print(log);

            m_AccessTokenResponse = response;
            userName = response.ScreenName;

            userInfo.text = "Authorized as "+response.ScreenName + ". Ready to post.";
            postScreenshotButton.interactable = true;

            PlayerPrefs.SetString(PLAYER_PREFS_TWITTER_USER_ID, response.UserId);
            PlayerPrefs.SetString(PLAYER_PREFS_TWITTER_USER_SCREEN_NAME, response.ScreenName);
            PlayerPrefs.SetString(PLAYER_PREFS_TWITTER_USER_TOKEN, response.Token);
            PlayerPrefs.SetString(PLAYER_PREFS_TWITTER_USER_TOKEN_SECRET, response.TokenSecret);
        }
        else
        {
            print("OnAccessTokenCallback - failed.");
            postScreenshotButton.interactable = false;
        }
    }
Example #32
0
        public static IEnumerator PostTweet(string text, string consumerKey, string consumerSecret, AccessTokenResponse response, PostTweetCallback callback)
        {
            if (string.IsNullOrEmpty(text) || text.Length > 140)
            {
                Debug.Log(string.Format("PostTweet - text[{0}] is empty or too long.", text));

                callback(false);
            }
            else
            {
                string url = string.Format(PostTweetURL, UrlEncode(text));
                Dictionary<string, string> parameters = new Dictionary<string, string>();

                parameters.Add("status", text);

                // Need to fill body since Unity doesn't like an empty request body.
                byte[] dummmy = new byte[1];
                dummmy[0] = 0;

                // HTTP header
                Hashtable headers = new Hashtable();
                headers["Authorization"] = GetHeaderWithAccessToken("POST", url, consumerKey, consumerSecret, response, parameters);

                WWW web = new WWW(url, dummmy, headers);
                yield return web;

                if (!string.IsNullOrEmpty(web.error))
                {
                    Debug.Log(string.Format("PostTweet - failed. {0}", web.error));
                    callback(false);
                }
                else
                {
                    string error = Regex.Match(web.text, @"<error>([^&]+)</error>").Groups[1].Value;

                    if (!string.IsNullOrEmpty(error))
                    {
                        Debug.Log(string.Format("PostTweet - failed. {0}", error));
                        callback(false);
                    }
                    else
                    {
                       callback(true);
                    }
                }
            }
        }
    void LoadTwitterUserInfo()
    {
        m_AccessTokenResponse = new Twitter.AccessTokenResponse();

        m_AccessTokenResponse.UserId = PlayerPrefs.GetString(PLAYER_PREFS_TWITTER_USER_ID);
        m_AccessTokenResponse.ScreenName = PlayerPrefs.GetString(PLAYER_PREFS_TWITTER_USER_SCREEN_NAME);
        m_AccessTokenResponse.Token = PlayerPrefs.GetString(PLAYER_PREFS_TWITTER_USER_TOKEN);
        m_AccessTokenResponse.TokenSecret = PlayerPrefs.GetString(PLAYER_PREFS_TWITTER_USER_TOKEN_SECRET);

        if (!string.IsNullOrEmpty(m_AccessTokenResponse.Token) &&
            !string.IsNullOrEmpty(m_AccessTokenResponse.ScreenName) &&
            !string.IsNullOrEmpty(m_AccessTokenResponse.Token) &&
            !string.IsNullOrEmpty(m_AccessTokenResponse.TokenSecret))
        {
            userName = m_AccessTokenResponse.ScreenName;
            userInfo.text = "Authorized as " + m_AccessTokenResponse.ScreenName + ". Ready to post.";
            postScreenshotButton.interactable = true;
            string log = "LoadTwitterUserInfo - succeeded";
            log += "\n    UserId : " + m_AccessTokenResponse.UserId;
            log += "\n    ScreenName : " + m_AccessTokenResponse.ScreenName;
            log += "\n    Token : " + m_AccessTokenResponse.Token;
            log += "\n    TokenSecret : " + m_AccessTokenResponse.TokenSecret;
            print(log);
        }
        else
        {
            userInfo.text = "Unauthorized, PIN required to post.";
            postScreenshotButton.interactable = false;
        }
    }
Example #34
0
        /// <summary>
        /// クエリを指定してツイートを検索
        /// </summary>
        /// <param name="query">検索クエリ.</param>
        /// <param name="consumerKey">コンシューマキー</param>
        /// <param name="consumerSecret">コンシューマシークレット</param>
        /// <param name="response">Twitter.API.GetAccessTokenで取得したアクセストークン</param>
        /// <param name="callback">検索結果を受け取るコールバック</param>
        /// <example>
        ///
        /// /*使用例*/
        ///
        /// Twitter.AccessTokenResponse m_AccessTokenResponse; // 事前にTwitter.API.GetAccessTokenでアクセストークンを取得しておく
        ///
        /// void SerchByHashTag()
        /// {
        ///     // ハッシュタグ "#Unity" で検索(終了するとOnSearchTweetsResponseが実行される)
        ///     StartCoroutine(Twitter.API.SearchTweets("#Unity", "CONSUMER_KEY", "CONSUMER_SECRET", m_AccessTokenResponse, OnSearchTweetsResponse));
        /// }
        ///
        /// void OnSearchTweetsResponse(bool success, string response)
        /// {
        ///     // responseに検索結果のJSON文字列が入ってくるので解析して各ツイートのテキストを得る
        /// }
        ///
        /// </example>
        public static IEnumerator SearchTweets(string query, string consumerKey, string consumerSecret, AccessTokenResponse response, CommonCallback callback)
        {
            var parameters = new Dictionary <string, string>();

            parameters["q"] = query;

            return(CommonRequest("search/tweets", Method.GET, parameters, consumerKey, consumerSecret, response, callback));
        }
Example #35
0
    void LoadTwitterUserInfo()
    {
        m_AccessTokenResponse = new Twitter.AccessTokenResponse();

        m_AccessTokenResponse.UserId        = (PLAYER_PREFS_TWITTER_USER_ID);
        m_AccessTokenResponse.ScreenName    = (PLAYER_PREFS_TWITTER_USER_SCREEN_NAME);
        m_AccessTokenResponse.Token         = (PLAYER_PREFS_TWITTER_USER_TOKEN);
        m_AccessTokenResponse.TokenSecret   = (PLAYER_PREFS_TWITTER_USER_TOKEN_SECRET);

        if (!string.IsNullOrEmpty(m_AccessTokenResponse.Token) &&
            !string.IsNullOrEmpty(m_AccessTokenResponse.ScreenName) &&
            !string.IsNullOrEmpty(m_AccessTokenResponse.Token) &&
            !string.IsNullOrEmpty(m_AccessTokenResponse.TokenSecret))
        {
            string log = "We've got the details, sir.";
            log += "\n    UserId : " + m_AccessTokenResponse.UserId;
            log += "\n    ScreenName : " + m_AccessTokenResponse.ScreenName;
            log += "\n    Token : " + m_AccessTokenResponse.Token;
            log += "\n    TokenSecret : " + m_AccessTokenResponse.TokenSecret;
            print(log);
        }
    }
Example #36
0
        private static string GetHeaderWithAccessToken(string httpRequestType, string apiURL, string consumerKey, string consumerSecret, AccessTokenResponse response, Dictionary <string, string> parameters)
        {
            AddDefaultOAuthParams(parameters, consumerKey, consumerSecret);

            parameters.Add("oauth_token", response.Token);
            parameters.Add("oauth_token_secret", response.TokenSecret);

            return(GetFinalOAuthHeader(httpRequestType, apiURL, parameters));
        }
    //string m_facebookUserId = "";
    //string m_facebookAuthToken = "";

    void Start()
    {
        BrainCloudWrapper.Initialize();
        m_AccessTokenResponse = new Twitter.AccessTokenResponse();
    }
Example #38
0
        public static IEnumerator GetHashtag(string hashtag, int num, string consumerKey, string consumerSecret, AccessTokenResponse response, GetTimelineCallback callback)
        {
            /*
             *          if (string.IsNullOrEmpty(text) || text.Length > 140)
             *          {
             *              Debug.Log(string.Format("GetTimeline - text[{0}] is empty or too long.", text));
             *
             *              callback(false);
             *          }
             *          else
             */
            {
                /*
                 *              Dictionary<string, string> parameters = new Dictionary<string, string>();
                 *              //parameters.Add("status", text);
                 *
                 *              // Add data to the form to post.
                 *              //WWWForm form = new WWWForm();
                 *              //form.AddField("status", text);
                 *              // Need to fill body since Unity doesn't like an empty request body.
                 *              byte[] dummmy = new byte[1];
                 *              dummmy[0] = 0;
                 *
                 *              // HTTP header
                 *              Dictionary<string, string> headers = new Dictionary<string, string>();
                 *              headers["Authorization"] = GetHeaderWithAccessToken("GET", GetTimelineURL, consumerKey, consumerSecret, response, parameters);
                 *              //var customerInfo = Convert.ToBase64String(
                 *              //    new UTF8Encoding().GetBytes(consumerKey + ":" + consumerSecret));
                 *              //headers["Authorization"] = "Basic " + customerInfo;
                 */
                //string url = string.Format(GetTweetURL, UrlEncode(text));
                string url = "https://api.twitter.com/1.1/search/tweets.json";


                Dictionary <string, string> parameters = new Dictionary <string, string>();

                //these might have to be sorted alphabetically, with 'q' at the end...
                parameters.Add("count", num.ToString());
                //parameters.Add("lang", "en");// NOTE: OPTIONAL
                parameters.Add("q", hashtag);


                //Build the final search URL to match the oAuth signature passed in the header adding the parameters above
                string appendURL = "";
                for (int i = 0; i < parameters.Count; i++)
                {
                    if (!parameters.Keys.ElementAt(i).Contains("q"))
                    {
                        string pre = "";
                        if (i > 0)
                        {
                            pre = "";
                        }

                        appendURL = appendURL + pre + parameters.Keys.ElementAt(i) + "=" + parameters.Values.ElementAt(i) + "&";
                    }
                }

                // HTTP header
                Dictionary <string, string> headers = new Dictionary <string, string>();
                headers["Authorization"] = GetHeaderWithAccessToken("GET", url, consumerKey, consumerSecret, response, parameters);

                //Escape all the spaces, quote marks to match what GetHeaderAccess did to the string
                //Have to do this after the oAuth Signature was created so we don't double-encode...
                string query = WWW.EscapeURL(parameters["q"]);

                url = "https://api.twitter.com/1.1/search/tweets.json?" + appendURL + "q=" + query;

                Debug.Log("Url posted to web is " + url);

                WWW web = new WWW(url, null, headers);
                yield return(web);

                //WWW web = new WWW(GetTimelineURL, dummmy, headers);
                //yield return web;

                if (!string.IsNullOrEmpty(web.error))
                {
                    Debug.Log(string.Format("GetTimeline1 - web error - failed. {0}\n{1}", web.error, web.text));
                    callback(false);
                }
                else
                {
                    string error = Regex.Match(web.text, @"<error>([^&]+)</error>").Groups[1].Value;

                    if (!string.IsNullOrEmpty(error))
                    {
                        Debug.Log(string.Format("GetTimeline - bad response - failed. {0}", error));
                        callback(false);
                    }
                    else
                    {
                        callback(true);
                    }
                }
                Debug.Log("GetTimeline - " + web.text);


                var tweets = JSON.Parse(web.text);

                Debug.Log("# of Tweets: " + tweets["statuses"].Count);
                for (int i = 0; i < tweets["statuses"].Count; i++)
                {
                    Debug.Log("Tweet #" + i + tweets["statuses"][i]["text"]);
                }


                //var tweets = JsonUtility.FromJson<TwitCollection>(web.text);
                //if (tweets == null) Debug.Log("NULL TWEETS");
                //Debug.Log("# of Tweets: " + tweets.statuses.Length);
                //foreach (var tweet in tweets.statuses)
                //{
                //    Debug.Log(tweet.text);
                //}
            }
        }
        //ファイルパスから画像ツイート
        public static IEnumerator PostTweetWithMedia(string text, string imagePath, string consumerKey, string consumerSecret, AccessTokenResponse response, PostTweetCallback callback)
        {
            if (string.IsNullOrEmpty(text) || text.Length > 140)
            {
                Debug.Log(string.Format("PostTweet - text[{0}] is empty or too long.", text));

                callback(false);
            }
            else
            {
                FileStream   fileStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read);
                BinaryReader bin        = new BinaryReader(fileStream);
                byte[]       bytes      = bin.ReadBytes((int)bin.BaseStream.Length);
                var          bs64       = Convert.ToBase64String(bytes);
                bin.Close();

                Dictionary <string, string> mediaParameters = new Dictionary <string, string>
                {
                    { "media_data", bs64 }
                };
                WWWForm mediaForm = new WWWForm();
                mediaForm.AddField("media_data", bs64);
                var mediaHeaders = new Dictionary <string, string>();
                mediaHeaders.Add("Authorization", GetHeaderWithAccessToken("POST", UploadMediaURL, consumerKey, consumerSecret, response, mediaParameters));
                WWW mediaWeb = new WWW(UploadMediaURL, mediaForm.data, mediaHeaders);

                yield return(mediaWeb);

                string media_id_string = "";
                if (!string.IsNullOrEmpty(mediaWeb.error))
                {
                    Debug.Log(string.Format("PostMedia - failed. {0}\n{1}", mediaWeb.error, mediaWeb.text));
                    callback(false);
                    yield break;
                }
                else
                {
                    string error = Regex.Match(mediaWeb.text, @"<error>([^&]+)</error>").Groups[1].Value;

                    if (!string.IsNullOrEmpty(error))
                    {
                        Debug.Log(string.Format("PostTweet - failed. {0}", error));
                        callback(false);
                        yield break;
                    }
                    else
                    {
                        var res = JsonUtility.FromJson <mediaResponse>(mediaWeb.text);
                        media_id_string = res.media_id_string;
                    }
                }

                Dictionary <string, string> parameters = new Dictionary <string, string>
                {
                    { "status", text },
                    { "media_ids", media_id_string }
                };
                WWWForm form = new WWWForm();
                form.AddField("status", text);
                form.AddField("media_ids", media_id_string);

                // HTTP header
                var headers = new Dictionary <string, string>();
                headers.Add("Authorization", GetHeaderWithAccessToken("POST", PostTweetURL, consumerKey, consumerSecret, response, parameters));

                WWW web = new WWW(PostTweetURL, form.data, headers);
                yield return(web);

                if (!string.IsNullOrEmpty(web.error))
                {
                    Debug.Log(string.Format("PostTweet - failed. {0}\n{1}", web.error, web.text));
                    callback(false);
                }
                else
                {
                    string error = Regex.Match(web.text, @"<error>([^&]+)</error>").Groups[1].Value;

                    if (!string.IsNullOrEmpty(error))
                    {
                        Debug.Log(string.Format("PostTweet - failed. {0}", error));
                        callback(false);
                    }
                    else
                    {
                        callback(true);
                    }
                }
            }
        }
Example #40
0
        public static IEnumerator PostImageTweet(string text, byte[] image, string consumerKey, string consumerSecret, AccessTokenResponse response, PostTweetCallback callback) {
            if (string.IsNullOrEmpty(text) || text.Length > 140) {
                Debug.Log(string.Format("PostTweet - text[{0}] is empty or too long.", text));

                callback(false);
            } else if (image == null || image.Length == 0) {
                Debug.Log("Missing image.");

                callback(false);
            } else {
                Dictionary<string, string> parameters = new Dictionary<string, string>();
                
                var form = new WWWForm();
                form.AddBinaryData("status", UTF8Encoding.UTF8.GetBytes(text));
                form.AddBinaryData("media[]", image, "anuki.jpg", "application/octet-stream");
                var request = new HTTP.Request(PostImageTweetURL, form);
                request.headers.Add("Authorization", GetHeaderWithAccessToken("POST", PostImageTweetURL, consumerKey, consumerSecret, response, parameters));
                yield return request.Send();
                
                if (request.exception != null) {
                    
                    Debug.Log(string.Format("PostImageTweet - failed:\n" + request.exception.Message));
                    callback(false);
                } else {
                    var txt = request.response.Text;

                    if (txt.Contains("\"errors\":")) {
                        Debug.Log(string.Format("PostImageTweet - failed. {0}", txt));
                        callback(false);
                    } else {
                        Debug.Log("Response: " + txt);
                        callback(true);
                    }
                }
            }
        }
        //Spriteから変換して画像ツイート
        public static IEnumerator PostTweetWithMedia(string _text, Sprite _sprite, string consumerKey, string consumerSecret, AccessTokenResponse response, PostTweetCallback callback)
        {
            if (string.IsNullOrEmpty(_text) || _text.Length > 140)
            {
                Debug.Log(string.Format("PostTweet - text[{0}] is empty or too long.", _text));

                callback(false);
            }
            else
            {
                Texture2D readableTexture = new Texture2D((int)_sprite.rect.width, (int)_sprite.rect.height);
                readableTexture.ReadPixels(_sprite.textureRect, 0, 0);

                var pixels = _sprite.texture.GetPixels((int)_sprite.textureRect.x,
                                                       (int)_sprite.textureRect.y,
                                                       (int)_sprite.textureRect.width,
                                                       (int)_sprite.textureRect.height);

                readableTexture.SetPixels(pixels);
                readableTexture.Apply();

                byte[] bytes = readableTexture.EncodeToPNG();
                var    bs64  = Convert.ToBase64String(bytes);

                Dictionary <string, string> mediaParameters = new Dictionary <string, string>
                {
                    { "media_data", bs64 }
                };
                WWWForm mediaForm = new WWWForm();
                mediaForm.AddField("media_data", bs64);
                var mediaHeaders = new Dictionary <string, string>();
                mediaHeaders.Add("Authorization", GetHeaderWithAccessToken("POST", UploadMediaURL, consumerKey, consumerSecret, response, mediaParameters));
                WWW mediaWeb = new WWW(UploadMediaURL, mediaForm.data, mediaHeaders);

                yield return(mediaWeb);

                string media_id_string = "";
                if (!string.IsNullOrEmpty(mediaWeb.error))
                {
                    Debug.Log(string.Format("PostMedia - failed. {0}\n{1}", mediaWeb.error, mediaWeb.text));
                    callback(false);
                    yield break;
                }
                else
                {
                    string error = Regex.Match(mediaWeb.text, @"<error>([^&]+)</error>").Groups[1].Value;

                    if (!string.IsNullOrEmpty(error))
                    {
                        Debug.Log(string.Format("PostTweet - failed. {0}", error));
                        callback(false);
                        yield break;
                    }
                    else
                    {
                        var res = JsonUtility.FromJson <mediaResponse>(mediaWeb.text);
                        media_id_string = res.media_id_string;
                    }
                }

                Dictionary <string, string> parameters = new Dictionary <string, string>
                {
                    { "status", _text },
                    { "media_ids", media_id_string }
                };
                WWWForm form = new WWWForm();
                form.AddField("status", _text);
                form.AddField("media_ids", media_id_string);

                // HTTP header
                var headers = new Dictionary <string, string>();
                headers.Add("Authorization", GetHeaderWithAccessToken("POST", PostTweetURL, consumerKey, consumerSecret, response, parameters));

                WWW web = new WWW(PostTweetURL, form.data, headers);
                yield return(web);

                if (!string.IsNullOrEmpty(web.error))
                {
                    Debug.Log(string.Format("PostTweet - failed. {0}\n{1}", web.error, web.text));
                    callback(false);
                }
                else
                {
                    string error = Regex.Match(web.text, @"<error>([^&]+)</error>").Groups[1].Value;

                    if (!string.IsNullOrEmpty(error))
                    {
                        Debug.Log(string.Format("PostTweet - failed. {0}", error));
                        callback(false);
                    }
                    else
                    {
                        callback(true);
                    }
                }
            }
        }
Example #42
0
        public static IEnumerator PostScreenshot(string encodedImage, string consumerKey, string consumerSecret, AccessTokenResponse response, screenshot secondaryCaller, bool isBack)
        {
            Dictionary<string, string> parameters = new Dictionary<string, string>();
            parameters.Add("media_data", encodedImage);

            // Add data to the form to post.
            WWWForm form = new WWWForm();
            form.AddField("media_data", encodedImage);

            Debug.Log("uploading to twitter...");
            // HTTP header
            var headers = new Dictionary<string, string>();
            headers["Authorization"] = GetHeaderWithAccessToken("POST", "https://upload.twitter.com/1.1/media/upload.json", consumerKey, consumerSecret, response, parameters);
            headers["Content-Transfer-Encoding"] = "base64";
            WWW web = new WWW("https://upload.twitter.com/1.1/media/upload.json", form.data, headers);
            yield return web;

            if (web.error != "Null")
            {
                string mediaID = web.text.Remove(web.text.IndexOf(','), web.text.Length - web.text.IndexOf(','));
                mediaID = mediaID.Remove(0, 12);
                Debug.Log("Upload complete - " + mediaID);
                secondaryCaller.mediaIDs.Add(new KeyValuePair<string, bool>(mediaID, isBack));
            }
            else
            {
                Debug.Log(web.error);
            }
        }
Example #43
0
        public static IEnumerator PostTweet(string text, string consumerKey, string consumerSecret, AccessTokenResponse response, PostTweetCallback callback)
        {
            if (string.IsNullOrEmpty(text) || text.Length > 140)
            {
                Debug.Log(string.Format("PostTweet - text[{0}] is empty or too long.", text));

                callback(false);
            }
            else
            {
                Dictionary<string, string> parameters = new Dictionary<string, string>();
                parameters.Add("status", text);

                // Add data to the form to post.
                WWWForm form = new WWWForm();
                form.AddField("status", text);

                // HTTP header
                var headers = new Hashtable();
                headers["Authorization"] = GetHeaderWithAccessToken("POST", PostTweetURL, consumerKey, consumerSecret, response, parameters);

                WWW web = new WWW(PostTweetURL, form.data, headers);
                yield return web;

                if (!string.IsNullOrEmpty(web.error))
                {
                    Debug.Log(string.Format("PostTweet - failed. {0}\n{1}", web.error, web.text));
                    callback(false);
                }
                else
                {
                    string error = Regex.Match(web.text, @"<error>([^&]+)</error>").Groups[1].Value;

                    if (!string.IsNullOrEmpty(error))
                    {
                        Debug.Log(string.Format("PostTweet - failed. {0}", error));
                        callback(false);
                    }
                    else
                    {
                        callback(true);
                    }
                }
            }
        }
    void OnSuccess_AuthenticateTwitter(bool success, Twitter.AccessTokenResponse response)
    {
        if (success)
        {
            string log = "OnAccessTokenCallback - succeeded";
            log += "\n    UserId : " + response.UserId;
            log += "\n    ScreenName : " + response.ScreenName;
            log += "\n    Token : " + response.Token;
            log += "\n    TokenSecret : " + response.TokenSecret;
            print(log);

            m_AccessTokenResponse = response;

            BrainCloudWrapper.GetBC().AuthenticationService.AuthenticateTwitter(response.UserId, response.Token, response.TokenSecret, true, OnSuccess_Authenticate, OnError_Authenticate);
        }
        else
        {
            print("OnAccessTokenCallback - failed.");
        }
    }
Example #45
0
        public static IEnumerator GetHashtag(string hashtag, string num, string consumerKey, string consumerSecret, AccessTokenResponse response, HashTagSearchCallback callback)
        {
            {
                string url = "https://api.twitter.com/1.1/search/tweets.json";


                Dictionary <string, string> parameters = new Dictionary <string, string>();

                parameters.Add("count", num);
                parameters.Add("q", hashtag);

                string appendURL = "";
                for (int i = 0; i < parameters.Count; i++)
                {
                    if (!parameters.Keys.ElementAt(i).Contains("q"))
                    {
                        string pre = "";
                        if (i > 0)
                        {
                            pre = "";
                        }

                        appendURL = appendURL + pre + parameters.Keys.ElementAt(i) + "=" + parameters.Values.ElementAt(i) + "&";
                    }
                }

                // HTTP header
                Dictionary <string, string> headers = new Dictionary <string, string>();
                headers["Authorization"] = GetHeaderWithAccessToken("GET", url, consumerKey, consumerSecret, response, parameters);

                string query = WWW.EscapeURL(parameters["q"]);

                url = "https://api.twitter.com/1.1/search/tweets.json?" + appendURL + "q=" + query;

                Debug.Log("Url posted to web is " + url);
                PlayerPrefs.SetString("DeletePrefab", "YES");
                WWW web = new WWW(url, null, headers);
                yield return(web);

                if (!string.IsNullOrEmpty(web.error))
                {
                    Debug.Log(string.Format("GetTimeline1 - web error - failed. {0}\n{1}", web.error, web.text));
                    callback(false);
                }
                else
                {
                    string error = Regex.Match(web.text, @"<error>([^&]+)</error>").Groups[1].Value;

                    if (!string.IsNullOrEmpty(error))
                    {
                        Debug.Log(string.Format("GetTimeline - bad response - failed. {0}", error));
                        callback(false);
                    }
                    else
                    {
                        callback(true);
                    }
                }
                Debug.Log("GetTimeline - " + web.text);
                PlayerPrefs.SetString("DeletePrefab", "NO");


                var tweets = JSON.Parse(web.text);

                Debug.Log("# of Tweets: " + tweets["statuses"].Count);
                for (int i = 0; i < tweets["statuses"].Count; i++)
                {
                    Debug.Log("Tweet # " + i + tweets["statuses"][i]["text"]);
                    GameObject NewText = GameObject.Instantiate(DemoClass.Instance.TextToInstantiate);
                    Text       TEXT    = NewText.GetComponent <Text> ();
                    TEXT.text = "Tweet # " + i + " " + tweets ["statuses"] [i] ["text"];
                    NewText.transform.SetParent(DemoClass.Instance.ContentParent.transform);
                    NewText.transform.localScale = Vector3.one;
                }
            }
        }