AIRã§OAuth2èªè¨¼ããã«ã¯?
AIRã§AdSenseããåå¾ãããã¼ã¿ã表示ããã¦ã¿ããã¨æã£ã。
Getting Started - AdSense Management API - Google Code
ããã、ãã®ããã«ã¯OAuth2èªè¨¼ãéãå¿ è¦ãããããã 。ã§ã¯、AIRã§OAuth2èªè¨¼ããã«ã¯ã©ããããããã®ã?
「API Console」ã§å¿ è¦ãªæ å ±ããããã
ã¾ãOAuth2ã§å¿ è¦ãªæ å ±ãéããããã«、API Consoleã«éã。
æ°ããããã¸ã§ã¯ããä½ã。ããã§ã¯「OAuth2Test」ã¨ãã。
ããã¸ã§ã¯ããä½ãã¨èªåçã«éã「services」ã§「AdSense Management API」ã「ON」ãã。
「API Access」ãã¼ã¸ã«éã£ã¦「Create an OAuth 2.0 client ID」ã¨ããéãã¦ãã«ããã¿ã³ã¯ãªãã¯。
「Product Name」ãå ¥åãã¦「Next」。ããã§ã¯「OAuth2Test」ã¨ãã。「Product Logo」ã¯æ¾ã£ã¦ããã¦ãï½。
「Application Type」ã¨ãã¦「Installed Application」ãé¸ãã§「Create client ID」。
ããã¨、「Client ID」「Client secret」「Redirect URIs」ããããã。ããããOAuth2ã§å¿ è¦ãªæ å ±ã 。
ã¦ã¼ã¶ã¼ã«è¨±å¯ãæ±ãããã¼ã¸ã表示ãã¦「code」ãåå¾
ã¦ã¼ã¶ã¼ã«AdSenseã¸ã®ã¢ã¯ã»ã¹ã®è¨±å¯ãæ±ãããã¼ã¸ã表示ãã¦、「code」ãåå¾ããå¿ è¦ããã。
ãã®ããã«、StageWebViewã使ã。ã³ã¼ãã¯ãããªé¢¨。
ã¾ã、StageWebViewããã«ã¹ã¯ãªã¼ã³ã§è¡¨ç¤ºãã¦è¨±å¯ãæ±ãããã¼ã¸ã表示ãã。
許å¯ãã¼ã¸ã®URLãcreateAuthURL()ã§ä½ãæã«、redirect_uriã¨client_idã«、API Consoleã§ããã£ãæ å ±ã使ã。
ããä¸ã¤ã®ãã¤ã³ãã¯「scope」ã®æå®ã®ä»æ¹。ãã®scopeã®ç¨®é¡ã¯ããã«æ¸ãã¦ãã£ã。
Getting Started - AdSense Management API - Google Code
å¤å、ä»ã®APIã®ããã¥ã¡ã³ãã«ãåæ§ã®è¨è¿°ãããã®ã ããã¨æãã。
許å¯ãã¼ã¸ã§ã¦ã¼ã¶ã¼ã許å¯ãåºãã¨ãã¼ã¸ãåãæ¿ãã。ãã®ãã¼ã¸ã®ã¿ã¤ãã«é¨åã«å¿ è¦ãª「code」ãå«ã¾ãã¦ããã®ã§åãåºã。
「access_token」ãåå¾
codeãåãåºããã¨åæã«、codeã使ã£ã¦access_tokenåå¾ã®ãªã¯ã¨ã¹ãã「https://accounts.google.com/o/oauth2/token」ã«æãã。
ããã§æ³¨æãå¿ è¦ãªã®ã¯
・ãã®URLã¸ã®ãªã¯ã¨ã¹ãã¯POSTã§è¡ã(GETã¯åãä»ããªã)
・ã¬ã¹ãã³ã¹ã¯JSONã§è¿ã£ã¦ãã
ã¨ãããã¨ã 。
ä»åã¯ãã®ããã«、URLã¸ã®ãªã¯ã¨ã¹ãã«HTTPServiceã使ããã¨ã«ãã。
ãªã¯ã¨ã¹ããæåããæã®å¦çã¯ãããªé¢¨。
JSONã¯FP11.0/AIR3.0ãã使ããããã«ãªã£ãã¯ã©ã¹ã 。æè¿ã®RESTåã®ãµã¼ãã¹ã®ã¬ã¹ãã³ã¹ã¯XMLã§ã¯ãªãã»ã¨ãã©JSONãªã®ã§、ãããããµãã¼ããã¦ãããªãã¨å°ã£ã¡ãããããª。
ä»åã¯FB4.6ã§éçºããã®ã§åé¡ãªãã£ããã©、FP10ç³»ã対å¿ããããã¨æãã¨ç¬èªã«JSONãæ±ããã©ã¤ãã©ãªãçµã¿è¾¼ããã¨ã«ãªãããã 。
ããã¦、ãã®ãªã¯ã¨ã¹ããå®äºããæç¹ã§「swv.stage = null;」ãã¦WebViewãé表示ã«ããã¨ãããéè¦。
ã¨ããã§、access_tokenãåå¾ã§ãããOAuth2èªè¨¼ã¯ã§ããã¨ãããã¨ã 。ã¤ã¾ã、ããã¾ã§ã§èªè¨¼å¦çã¯ããã¾ã。
AdSenseã®æ¥æ¯ã®å£²ä¸ã¬ãã¼ãããªã¯ã¨ã¹ããã
ã¡ããã¨èªè¨¼ã§ããã®ãã確ãããããã«、å®éã«AdSenseã«2012å¹´1æã®æ¥æ¯ã®å£²ä¸ã¬ãã¼ãããªã¯ã¨ã¹ããã¦ã¿ã。
ãã®ãªã¯ã¨ã¹ãã«å¯¾å¿ããã®ã¯「https://www.googleapis.com/adsense/v1.1/reports」ã 。
AdSenseã«éããªããã©、Googleã®APIã§ã©ããªã¡ã½ãã(ãªã¯ã¨ã¹ãå )ããããã、ãã®ãã©ã¡ã¼ã¿ã¼ã«ã¯ã©ããªãã®ããããã¯「APIs Explorer」ã§ããã。
Google APIs Explorer
ä»åãã¾ã、ãã®ããã«HTTPServiceã使ã。
å®éã«ãªã¯ã¨ã¹ããéãé¨åã¯ãããªé¢¨。
ããã§access_tokenãå¿ è¦ãªã®ã 。
ã¬ã¹ãã³ã¹ã¯ã¾ãJSONã§è¿ã£ã¦ããã®ã§、ãã¨ã¯ã°ã©ãã«ãããªã©、好ããªå¦çãããã°ãã。ä»åã¯åã«TextAreaã«è¡¨ç¤ºããã。
å ¨ã³ã¼ã
æå¾ã«、å ¨ã³ã¼ãããã´ãº。å人æ å ±ã®é¨åã¯「xxx」ãã。
ãã¡ãã、ãã®ã³ã¼ããã©ã®ããã«ä½¿ã£ã¦ããã ãã¦ãçµæ§ã§ã。ãã ã、ç¡ä¿è¨¼、ãµãã¼ãç¡ãã§ã。
以ä¸、çµäº!ããã¼ããã¡ã〜!
Getting Started - AdSense Management API - Google Code
ããã、ãã®ããã«ã¯OAuth2èªè¨¼ãéãå¿ è¦ãããããã 。ã§ã¯、AIRã§OAuth2èªè¨¼ããã«ã¯ã©ããããããã®ã?
「API Console」ã§å¿ è¦ãªæ å ±ããããã
ã¾ãOAuth2ã§å¿ è¦ãªæ å ±ãéããããã«、API Consoleã«éã。
æ°ããããã¸ã§ã¯ããä½ã。ããã§ã¯「OAuth2Test」ã¨ãã。
ããã¸ã§ã¯ããä½ãã¨èªåçã«éã「services」ã§「AdSense Management API」ã「ON」ãã。
「API Access」ãã¼ã¸ã«éã£ã¦「Create an OAuth 2.0 client ID」ã¨ããéãã¦ãã«ããã¿ã³ã¯ãªãã¯。
「Product Name」ãå ¥åãã¦「Next」。ããã§ã¯「OAuth2Test」ã¨ãã。「Product Logo」ã¯æ¾ã£ã¦ããã¦ãï½。
「Application Type」ã¨ãã¦「Installed Application」ãé¸ãã§「Create client ID」。
ããã¨、「Client ID」「Client secret」「Redirect URIs」ããããã。ããããOAuth2ã§å¿ è¦ãªæ å ±ã 。
ã¦ã¼ã¶ã¼ã«è¨±å¯ãæ±ãããã¼ã¸ã表示ãã¦「code」ãåå¾
ã¦ã¼ã¶ã¼ã«AdSenseã¸ã®ã¢ã¯ã»ã¹ã®è¨±å¯ãæ±ãããã¼ã¸ã表示ãã¦、「code」ãåå¾ããå¿ è¦ããã。
ãã®ããã«、StageWebViewã使ã。ã³ã¼ãã¯ãããªé¢¨。
protected function onClickBtnStartOAuth2():void { swv = new StageWebView(); swv.addEventListener(Event.COMPLETE, onCompleteSWV); function onCompleteSWV(evt:Event):void { const codeSubstringKey:String = "code="; var title:String = swv.title; //「code」ã¯ã¿ã¤ãã«ã«å«ã¾ãã¦ãã trace("onCompleteSWV() title=[" + title + "]"); var idx:int = title.indexOf(codeSubstringKey); if (idx != -1) { //「code」ãåãåºã code = title.substring(idx + codeSubstringKey.length); trace("onCompleteSWV() code=[" + code + "]"); //codeãå ã«accessTokenããªã¯ã¨ã¹ã(grant_typeã¯InstalledAppã®å ´åã¯ããã§åºå®) getAccessToken.send({"code":code, "client_id":CLIENT_ID, "client_secret":CLIENT_SECRET, "redirect_uri":REDIRECT_URI, "grant_type":"authorization_code"}); } } swv.stage = stage; //WebView表示éå§(WebViewã¯ãã£ã¹ãã¬ã¤ãªã¹ãã«è¿½å ã§ããªãã®ã§ãã«ã¹ã¯ãªã¼ã³ã«ãªã) swv.viewPort = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight); swv.loadURL(createAuthURL()); //許å¯ãã¼ã¸ãªã¯ã¨ã¹ã } protected function createAuthURL():String { var url:String = "https://accounts.google.com/o/oauth2/auth?" //ããã«æãããã¨ã«ãªã£ã¦ã + "scope=" + escapeMultiByte("https://www.googleapis.com/auth/adsense.readonly") //許å¯ãå¾ãããµã¼ãã¹ + "&redirect_uri=" + REDIRECT_URI + "&response_type=code" //InstalledAppã ã¨ãã®å¤ã§åºå® + "&client_id=" + CLIENT_ID return url; }
ã¾ã、StageWebViewããã«ã¹ã¯ãªã¼ã³ã§è¡¨ç¤ºãã¦è¨±å¯ãæ±ãããã¼ã¸ã表示ãã。
許å¯ãã¼ã¸ã®URLãcreateAuthURL()ã§ä½ãæã«、redirect_uriã¨client_idã«、API Consoleã§ããã£ãæ å ±ã使ã。
ããä¸ã¤ã®ãã¤ã³ãã¯「scope」ã®æå®ã®ä»æ¹。ãã®scopeã®ç¨®é¡ã¯ããã«æ¸ãã¦ãã£ã。
Getting Started - AdSense Management API - Google Code
å¤å、ä»ã®APIã®ããã¥ã¡ã³ãã«ãåæ§ã®è¨è¿°ãããã®ã ããã¨æãã。
許å¯ãã¼ã¸ã§ã¦ã¼ã¶ã¼ã許å¯ãåºãã¨ãã¼ã¸ãåãæ¿ãã。ãã®ãã¼ã¸ã®ã¿ã¤ãã«é¨åã«å¿ è¦ãª「code」ãå«ã¾ãã¦ããã®ã§åãåºã。
「access_token」ãåå¾
codeãåãåºããã¨åæã«、codeã使ã£ã¦access_tokenåå¾ã®ãªã¯ã¨ã¹ãã「https://accounts.google.com/o/oauth2/token」ã«æãã。
ããã§æ³¨æãå¿ è¦ãªã®ã¯
・ãã®URLã¸ã®ãªã¯ã¨ã¹ãã¯POSTã§è¡ã(GETã¯åãä»ããªã)
・ã¬ã¹ãã³ã¹ã¯JSONã§è¿ã£ã¦ãã
ã¨ãããã¨ã 。
ä»åã¯ãã®ããã«、URLã¸ã®ãªã¯ã¨ã¹ãã«HTTPServiceã使ããã¨ã«ãã。
<!-- JSONã§è¿ã£ã¦ããã®ã§resultFormatãtextã«。ãã®URLã«ã¯POSTã§ãªã¯ã¨ã¹ãããªãããã¨ã«ãªã£ã¦ã。 --> <s:HTTPService id="getAccessToken" url="https://accounts.google.com/o/oauth2/token" method="POST" resultFormat="text" result="onResultGetAccessToken(event);" fault="onFaultGetAccessToken(event);"/>
ãªã¯ã¨ã¹ããæåããæã®å¦çã¯ãããªé¢¨。
protected function onResultGetAccessToken(evt:ResultEvent):void { var result:String = getAccessToken.lastResult as String; taAccessToken.text = result; trace("onResultGetAccessToken():" + evt + "\n" + result); var json:Object = JSON.parse(result); //JSONããã¼ã¹ accessToken = json.access_token; trace("onResultGetAccessToken():access_token=" + accessToken); swv.stage = null; //WebViewãé表示ã«ãã }
JSONã¯FP11.0/AIR3.0ãã使ããããã«ãªã£ãã¯ã©ã¹ã 。æè¿ã®RESTåã®ãµã¼ãã¹ã®ã¬ã¹ãã³ã¹ã¯XMLã§ã¯ãªãã»ã¨ãã©JSONãªã®ã§、ãããããµãã¼ããã¦ãããªãã¨å°ã£ã¡ãããããª。
ä»åã¯FB4.6ã§éçºããã®ã§åé¡ãªãã£ããã©、FP10ç³»ã対å¿ããããã¨æãã¨ç¬èªã«JSONãæ±ããã©ã¤ãã©ãªãçµã¿è¾¼ããã¨ã«ãªãããã 。
ããã¦、ãã®ãªã¯ã¨ã¹ããå®äºããæç¹ã§「swv.stage = null;」ãã¦WebViewãé表示ã«ããã¨ãããéè¦。
ã¨ããã§、access_tokenãåå¾ã§ãããOAuth2èªè¨¼ã¯ã§ããã¨ãããã¨ã 。ã¤ã¾ã、ããã¾ã§ã§èªè¨¼å¦çã¯ããã¾ã。
AdSenseã®æ¥æ¯ã®å£²ä¸ã¬ãã¼ãããªã¯ã¨ã¹ããã
ã¡ããã¨èªè¨¼ã§ããã®ãã確ãããããã«、å®éã«AdSenseã«2012å¹´1æã®æ¥æ¯ã®å£²ä¸ã¬ãã¼ãããªã¯ã¨ã¹ããã¦ã¿ã。
ãã®ãªã¯ã¨ã¹ãã«å¯¾å¿ããã®ã¯「https://www.googleapis.com/adsense/v1.1/reports」ã 。
AdSenseã«éããªããã©、Googleã®APIã§ã©ããªã¡ã½ãã(ãªã¯ã¨ã¹ãå )ããããã、ãã®ãã©ã¡ã¼ã¿ã¼ã«ã¯ã©ããªãã®ããããã¯「APIs Explorer」ã§ããã。
Google APIs Explorer
ä»åãã¾ã、ãã®ããã«HTTPServiceã使ã。
<!-- JSONã§è¿ã£ã¦ããã®ã§resultFormatãtextã«。 --> <s:HTTPService id="reportsGen" url="https://www.googleapis.com/adsense/v1.1/reports" resultFormat="text" result="onResultReportsGen(event);" fault="onFaultReportsGen(event);"/>
å®éã«ãªã¯ã¨ã¹ããéãé¨åã¯ãããªé¢¨。
protected var startDate:String = "2012-01-01"; protected var endDate:String = "2012-01-31"; protected var dimension:String = "DATE"; protected var metric:String = "EARNINGS"; public function requestReportsGen():void { trace("requestReportsGen()"); reportsGen.send({"startDate":startDate, "endDate":endDate, "dimension":dimension, "metric":metric, "key":API_KEY, "access_token":accessToken}); }
ããã§access_tokenãå¿ è¦ãªã®ã 。
ã¬ã¹ãã³ã¹ã¯ã¾ãJSONã§è¿ã£ã¦ããã®ã§、ãã¨ã¯ã°ã©ãã«ãããªã©、好ããªå¦çãããã°ãã。ä»åã¯åã«TextAreaã«è¡¨ç¤ºããã。
å ¨ã³ã¼ã
æå¾ã«、å ¨ã³ã¼ãããã´ãº。å人æ å ±ã®é¨åã¯「xxx」ãã。
ãã¡ãã、ãã®ã³ã¼ããã©ã®ããã«ä½¿ã£ã¦ããã ãã¦ãçµæ§ã§ã。ãã ã、ç¡ä¿è¨¼、ãµãã¼ãç¡ãã§ã。
<?xml version="1.0" encoding="utf-8"?> <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" width="640" height="480" > <!-- å種å®ç¾© --> <fx:Declarations> <!-- JSONã§è¿ã£ã¦ããã®ã§resultFormatãtextã«。ãã®URLã«ã¯POSTã§ãªã¯ã¨ã¹ãããªãããã¨ã«ãªã£ã¦ã。 --> <s:HTTPService id="getAccessToken" url="https://accounts.google.com/o/oauth2/token" method="POST" resultFormat="text" result="onResultGetAccessToken(event);" fault="onFaultGetAccessToken(event);"/> <!-- JSONã§è¿ã£ã¦ããã®ã§resultFormatãtextã«。 --> <s:HTTPService id="reportsGen" url="https://www.googleapis.com/adsense/v1.1/reports" resultFormat="text" result="onResultReportsGen(event);" fault="onFaultReportsGen(event);"/> </fx:Declarations> <!-- ã¹ã¯ãªãã --> <fx:Script> <![CDATA[ import mx.rpc.events.FaultEvent; import mx.rpc.events.ResultEvent; //OAuth2ã®è¨±å¯ãã¼ã¸è¡¨ç¤ºç¨WebView protected var swv:StageWebView; //APIs Consoleã§ããã£ãæ å ± protected const CLIENT_ID:String ="xxx"; protected const CLIENT_SECRET:String = "xxx"; protected const REDIRECT_URI:String = "urn:ietf:wg:oauth:2.0:oob"; //InstalledAppã ã¨ãã®å¤ã¯åºå®ããã。「http://localhost」ã ã¨ã¨ã©ã¼åºã。 protected const API_KEY:String = "xxx"; //欲ããæ å ± [Bindable] protected var code:String; //accessTokenããããããã«å¿ è¦ [Bindable] protected var accessToken:String; //APIã«ã¢ã¯ã»ã¹ããããã«å¿ è¦ protected function onClickBtnStartOAuth2():void { swv = new StageWebView(); swv.addEventListener(Event.COMPLETE, onCompleteSWV); function onCompleteSWV(evt:Event):void { const codeSubstringKey:String = "code="; var title:String = swv.title; //「code」ã¯ã¿ã¤ãã«ã«å«ã¾ãã¦ãã trace("onCompleteSWV() title=[" + title + "]"); var idx:int = title.indexOf(codeSubstringKey); if (idx != -1) { //「code」ãåãåºã code = title.substring(idx + codeSubstringKey.length); trace("onCompleteSWV() code=[" + code + "]"); //codeãå ã«accessTokenããªã¯ã¨ã¹ã(grant_typeã¯InstalledAppã®å ´åã¯ããã§åºå®) getAccessToken.send({"code":code, "client_id":CLIENT_ID, "client_secret":CLIENT_SECRET, "redirect_uri":REDIRECT_URI, "grant_type":"authorization_code"}); } } swv.stage = stage; //WebView表示éå§(WebViewã¯ãã£ã¹ãã¬ã¤ãªã¹ãã«è¿½å ã§ããªãã®ã§ãã«ã¹ã¯ãªã¼ã³ã«ãªã) swv.viewPort = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight); swv.loadURL(createAuthURL()); //許å¯ãã¼ã¸ãªã¯ã¨ã¹ã } protected function createAuthURL():String { var url:String = "https://accounts.google.com/o/oauth2/auth?" //ããã«æãããã¨ã«ãªã£ã¦ã + "scope=" + escapeMultiByte("https://www.googleapis.com/auth/adsense.readonly") //許å¯ãå¾ãããµã¼ãã¹ + "&redirect_uri=" + REDIRECT_URI + "&response_type=code" //InstalledAppã ã¨ãã®å¤ã§åºå® + "&client_id=" + CLIENT_ID return url; } protected function onResultGetAccessToken(evt:ResultEvent):void { var result:String = getAccessToken.lastResult as String; taAccessToken.text = result; trace("onResultGetAccessToken():" + evt + "\n" + result); var json:Object = JSON.parse(result); //JSONããã¼ã¹ accessToken = json.access_token; trace("onResultGetAccessToken():access_token=" + accessToken); swv.stage = null; //WebViewãé表示ã«ãã } protected function onFaultGetAccessToken(evt:FaultEvent):void { trace("onResultGetAccessToken():" + evt); } protected var startDate:String = "2012-01-01"; protected var endDate:String = "2012-01-31"; protected var dimension:String = "DATE"; protected var metric:String = "EARNINGS"; public function requestReportsGen():void { trace("requestReportsGen()"); reportsGen.send({"startDate":startDate, "endDate":endDate, "dimension":dimension, "metric":metric, "key":API_KEY, "access_token":accessToken}); } public function onResultReportsGen(evt:ResultEvent):void { var result:String = reportsGen.lastResult as String; trace("onResultReportsGen():" + evt + "\n" + result); taReportsGen.text = result; } public function onFaultReportsGen(evt:FaultEvent):void { trace("onFaultReportsGen():" + evt); } ]]> </fx:Script> <!-- UIé¨ --> <s:VGroup width="100%" height="100%" horizontalAlign="center" paddingBottom="10" paddingLeft="10" paddingRight="10" paddingTop="10"> <s:HGroup width="100%" verticalAlign="middle"> <s:Label text="code" width="125" fontSize="18"/> <s:Label width="100%" text="{code}"/> </s:HGroup> <s:TextArea id="taAccessToken" width="100%" height="100%" editable="false"/> <s:HGroup width="100%" verticalAlign="middle"> <s:Label text="accessToken" width="125" fontSize="18"/> <s:Label width="100%" text="{accessToken}"/> </s:HGroup> <s:Button enabled="{accessToken == null}" label="Start OAuth2" click="onClickBtnStartOAuth2();"/> <s:TextArea id="taReportsGen" width="100%" height="100%" editable="false"/> <s:Button enabled="{accessToken != null}" label="Request reports.generate" click="requestReportsGen();"/> </s:VGroup> </s:WindowedApplication>
以ä¸、çµäº!ããã¼ããã¡ã〜!
ã³ã¡ã³ã
ã³ã¡ã³ããæ稿