Last active
July 9, 2016 02:03
-
-
Save kazumalab/21548ac8e40cd8efa57c5709c221ed58 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using UnityEngine.UI; | |
public class Demo : MonoBehaviour | |
{ | |
private string CONSUMER_KEY = "各自作成"; | |
private string CONSUMER_SECRET = "各自作成"; | |
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"; | |
Twitter.RequestTokenResponse m_RequestTokenResponse; | |
Twitter.AccessTokenResponse m_AccessTokenResponse; | |
public string input_pin; | |
public InputField input_pin_field; | |
public string input_tweet_content; | |
public InputField input_tweet_field; | |
void Start() | |
{ | |
LoadTwitterUserInfo(); | |
} | |
public void OnClickTwitterGetRequestToken() | |
{ | |
StartCoroutine(Twitter.API.GetRequestToken(CONSUMER_KEY, CONSUMER_SECRET, | |
new Twitter.RequestTokenCallback(this.OnRequestTokenCallback))); | |
} | |
public void OnClickTwitterGetAccessToken() | |
{ | |
input_pin = input_pin_field.GetComponent<InputField> ().text; | |
StartCoroutine(Twitter.API.GetAccessToken(CONSUMER_KEY, CONSUMER_SECRET, m_RequestTokenResponse.Token, input_pin, | |
new Twitter.AccessTokenCallback(this.OnAccessTokenCallback))); | |
} | |
public void OnClickTwitterPostTweet() | |
{ | |
input_tweet_content = input_tweet_field.GetComponent<InputField> ().text; | |
StartCoroutine(Twitter.API.PostTweet(input_tweet_content, CONSUMER_KEY, CONSUMER_SECRET, m_AccessTokenResponse, | |
new Twitter.PostTweetCallback(this.OnPostTweet))); | |
} | |
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)) | |
{ | |
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 OnRequestTokenCallback(bool success, Twitter.RequestTokenResponse response) | |
{ | |
if (success) | |
{ | |
string log = "OnRequestTokenCallback - succeeded"; | |
log += "\n Token : " + response.Token; | |
log += "\n TokenSecret : " + response.TokenSecret; | |
print(log); | |
m_RequestTokenResponse = response; | |
Twitter.API.OpenAuthorizationPage(response.Token); | |
} | |
else | |
{ | |
print("OnRequestTokenCallback - failed."); | |
} | |
} | |
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."); | |
} | |
} | |
void OnPostTweet(bool success) | |
{ | |
print("OnPostTweet - " + (success ? "succedded." : "failed.")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment