Created
July 15, 2014 17:30
-
-
Save susisu/0d90d60625868fe8b529 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
package | |
{ | |
import flash.display.Sprite; | |
import flash.events.MouseEvent; | |
import flash.text.TextField; | |
import flash.text.TextFieldType; | |
import flash.net.navigateToURL; | |
import flash.net.URLRequest; | |
import isle.susisu.twitter.Twitter; | |
import isle.susisu.twitter.TwitterRequest; | |
import isle.susisu.twitter.events.TwitterRequestEvent; | |
import isle.susisu.twitter.events.TwitterErrorEvent; | |
[SWF(width="480",height="360",backgroundColor="0x000000",frameRate="60")] | |
public class TwitterExample extends Sprite | |
{ | |
private var outputConsole:TextField; | |
private var openBrowserButton:Sprite; | |
private var pinInputField:TextField; | |
private var getAccessTokenButton:Sprite; | |
private var tweetInputField:TextField; | |
private var tweetButton:Sprite; | |
private var twitter:Twitter; | |
public function TwitterExample() | |
{ | |
init(); | |
this.outputConsole.appendText("1. open browser\n2. input pin code\n3. get access tokens\n4. tweeeeet!\n"); | |
} | |
private function init():void | |
{ | |
// 出力コンソール | |
this.outputConsole = new TextField(); | |
this.outputConsole.width = 480; | |
this.outputConsole.height = 180; | |
this.outputConsole.border = true; | |
this.outputConsole.background = true; | |
this.outputConsole.backgroundColor = 0xffffff; | |
this.outputConsole.text = ""; | |
this.addChild(this.outputConsole); | |
// ブラウザを開くボタン | |
this.openBrowserButton = new Sprite(); | |
var openBrowserButtonText:TextField = new TextField(); | |
openBrowserButtonText.width = 80; | |
openBrowserButtonText.height = 20; | |
openBrowserButtonText.background = true; | |
openBrowserButtonText.backgroundColor = 0xc0c0c0; | |
openBrowserButtonText.border = true; | |
openBrowserButtonText.text = "open browser"; | |
this.openBrowserButton.addChild(openBrowserButtonText); | |
this.openBrowserButton.y = 180; | |
this.openBrowserButton.buttonMode = true; | |
this.openBrowserButton.mouseChildren = false; | |
this.addChild(this.openBrowserButton); | |
this.openBrowserButton.addEventListener(MouseEvent.CLICK, onOpenBrowserButtonClick); | |
// PIN入力フィールド | |
this.pinInputField = new TextField(); | |
this.pinInputField.x = 80; | |
this.pinInputField.y = 180; | |
this.pinInputField.width = 200; | |
this.pinInputField.height = 20; | |
this.pinInputField.background = true; | |
this.pinInputField.backgroundColor = 0xffffff; | |
this.pinInputField.border = true; | |
this.pinInputField.type = TextFieldType.INPUT; | |
this.addChild(this.pinInputField); | |
// access token 取得ボタン | |
this.getAccessTokenButton = new Sprite(); | |
var getAccessTokenButtonText:TextField = new TextField(); | |
getAccessTokenButtonText.width = 200; | |
getAccessTokenButtonText.height = 20; | |
getAccessTokenButtonText.background = true; | |
getAccessTokenButtonText.backgroundColor = 0xc0c0c0; | |
getAccessTokenButtonText.border = true; | |
getAccessTokenButtonText.text = "get access tokens"; | |
this.getAccessTokenButton.addChild(getAccessTokenButtonText); | |
this.getAccessTokenButton.x = 280; | |
this.getAccessTokenButton.y = 180; | |
this.getAccessTokenButton.buttonMode = true; | |
this.getAccessTokenButton.mouseChildren = false; | |
this.addChild(this.getAccessTokenButton); | |
this.getAccessTokenButton.addEventListener(MouseEvent.CLICK, onGetAccessTokenButtonClick); | |
// ツイート入力フィールド | |
this.tweetInputField = new TextField(); | |
this.tweetInputField.y = 200; | |
this.tweetInputField.width = 480; | |
this.tweetInputField.height = 140; | |
this.tweetInputField.background = true; | |
this.tweetInputField.backgroundColor = 0xffffff; | |
this.tweetInputField.border = true; | |
this.tweetInputField.type = TextFieldType.INPUT; | |
this.addChild(this.tweetInputField); | |
// ツイートボタン | |
this.tweetButton = new Sprite(); | |
var tweetButtonText:TextField = new TextField(); | |
tweetButtonText.width = 480; | |
tweetButtonText.height = 20; | |
tweetButtonText.background = true; | |
tweetButtonText.backgroundColor = 0xc0c0c0; | |
tweetButtonText.border = true; | |
tweetButtonText.text = "tweet"; | |
this.tweetButton.addChild(tweetButtonText); | |
this.tweetButton.y = 340; | |
this.tweetButton.buttonMode = true; | |
this.tweetButton.mouseChildren = false; | |
this.addChild(this.tweetButton); | |
this.tweetButton.addEventListener(MouseEvent.CLICK, onTweetButtonClick); | |
this.twitter = new Twitter("<consumer key>", "<consumer key secret>"); | |
} | |
// ブラウザを開くボタンを押した時の処理 | |
private function onOpenBrowserButtonClick(event:MouseEvent):void | |
{ | |
var request:TwitterRequest = this.twitter.oauth_requestToken(); | |
request.addEventListener(TwitterRequestEvent.COMPLETE, onOAuthRequestTokenComplete); | |
request.addEventListener(TwitterErrorEvent.CLIENT_ERROR, onOAuthRequestTokenError); | |
request.addEventListener(TwitterErrorEvent.SERVER_ERROR, onOAuthRequestTokenError); | |
this.outputConsole.appendText("please wait...\n"); | |
} | |
// oauth/request_token の完了処理 | |
private function onOAuthRequestTokenComplete(event:TwitterRequestEvent):void | |
{ | |
(event.target as TwitterRequest).removeEventListener(TwitterRequestEvent.COMPLETE, onOAuthRequestTokenComplete); | |
(event.target as TwitterRequest).removeEventListener(TwitterErrorEvent.CLIENT_ERROR, onOAuthRequestTokenError); | |
(event.target as TwitterRequest).removeEventListener(TwitterErrorEvent.SERVER_ERROR, onOAuthRequestTokenError); | |
// 得られた認証用URLを開く | |
navigateToURL(new URLRequest(this.twitter.getOAuthAuthorizeURL())); | |
this.outputConsole.appendText("borwser opened: " + this.twitter.getOAuthAuthorizeURL() + "\n"); | |
} | |
// oauth/request_token のエラー処理 | |
private function onOAuthRequestTokenError(event:TwitterErrorEvent):void | |
{ | |
(event.target as TwitterRequest).removeEventListener(TwitterRequestEvent.COMPLETE, onOAuthRequestTokenComplete); | |
(event.target as TwitterRequest).removeEventListener(TwitterErrorEvent.CLIENT_ERROR, onOAuthRequestTokenError); | |
(event.target as TwitterRequest).removeEventListener(TwitterErrorEvent.SERVER_ERROR, onOAuthRequestTokenError); | |
this.outputConsole.appendText("error: " + event.statusCode.toString() + "\n"); | |
} | |
// access token 取得ボタンを押した時の処理 | |
private function onGetAccessTokenButtonClick(event:MouseEvent):void | |
{ | |
// 入力された PIN を送信 | |
var request:TwitterRequest = this.twitter.oauth_accessToken(this.pinInputField.text); | |
request.addEventListener(TwitterRequestEvent.COMPLETE, onOAuthAccessTokenComplete); | |
request.addEventListener(TwitterErrorEvent.CLIENT_ERROR, onOAuthAccessTokenError); | |
request.addEventListener(TwitterErrorEvent.SERVER_ERROR, onOAuthAccessTokenError); | |
this.outputConsole.appendText("please wait...\n"); | |
} | |
// oauth/access_token の完了処理 | |
private function onOAuthAccessTokenComplete(event:TwitterRequestEvent):void | |
{ | |
(event.target as TwitterRequest).removeEventListener(TwitterRequestEvent.COMPLETE, onOAuthAccessTokenComplete); | |
(event.target as TwitterRequest).removeEventListener(TwitterErrorEvent.CLIENT_ERROR, onOAuthAccessTokenError); | |
(event.target as TwitterRequest).removeEventListener(TwitterErrorEvent.SERVER_ERROR, onOAuthAccessTokenError); | |
this.pinInputField.text = ""; | |
// 得られた access token をコンソールに出力 | |
this.outputConsole.appendText("access token: " + this.twitter.accessToken + "\n"); | |
this.outputConsole.appendText("access token secret: " + this.twitter.accessTokenSecret + "\n"); | |
} | |
// oauth/access_token エラー処理 | |
private function onOAuthAccessTokenError(event:TwitterErrorEvent):void | |
{ | |
(event.target as TwitterRequest).removeEventListener(TwitterRequestEvent.COMPLETE, onOAuthAccessTokenComplete); | |
(event.target as TwitterRequest).removeEventListener(TwitterErrorEvent.CLIENT_ERROR, onOAuthAccessTokenError); | |
(event.target as TwitterRequest).removeEventListener(TwitterErrorEvent.SERVER_ERROR, onOAuthAccessTokenError); | |
this.outputConsole.appendText("error: " + event.statusCode.toString() + "\n"); | |
} | |
// ツイートボタンを押した時の処理 | |
private function onTweetButtonClick(event:MouseEvent):void | |
{ | |
// ツイートを送信 | |
var request:TwitterRequest = this.twitter.statuses_update(this.tweetInputField.text); | |
request.addEventListener(TwitterRequestEvent.COMPLETE, onStatusesUpdateComplete); | |
request.addEventListener(TwitterErrorEvent.CLIENT_ERROR, onStatusesUpdateError); | |
request.addEventListener(TwitterErrorEvent.SERVER_ERROR, onStatusesUpdateError); | |
this.outputConsole.appendText("please wait...\n"); | |
} | |
private function onStatusesUpdateComplete(event:TwitterRequestEvent):void | |
{ | |
(event.target as TwitterRequest).removeEventListener(TwitterRequestEvent.COMPLETE, onStatusesUpdateComplete); | |
(event.target as TwitterRequest).removeEventListener(TwitterErrorEvent.CLIENT_ERROR, onStatusesUpdateError); | |
(event.target as TwitterRequest).removeEventListener(TwitterErrorEvent.SERVER_ERROR, onStatusesUpdateError); | |
this.tweetInputField.text = ""; | |
this.outputConsole.appendText("tweeted!\n"); | |
} | |
private function onStatusesUpdateError(event:TwitterErrorEvent):void | |
{ | |
(event.target as TwitterRequest).removeEventListener(TwitterRequestEvent.COMPLETE, onStatusesUpdateComplete); | |
(event.target as TwitterRequest).removeEventListener(TwitterErrorEvent.CLIENT_ERROR, onStatusesUpdateError); | |
(event.target as TwitterRequest).removeEventListener(TwitterErrorEvent.SERVER_ERROR, onStatusesUpdateError); | |
this.outputConsole.appendText("error: " + event.statusCode.toString() + "\n"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment