<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>zedia flash blog &#187; as3httpclientlib</title>
	<atom:link href="http://www.zedia.net/tag/as3httpclientlib/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.zedia.net</link>
	<description>Flash, ActionScript, SEO and everything in between</description>
	<lastBuildDate>Thu, 02 Feb 2012 17:58:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>How to connect to Google Analytics Data API in AIR</title>
		<link>http://www.zedia.net/2009/how-to-connect-to-google-analytics-data-api-in-air/</link>
		<comments>http://www.zedia.net/2009/how-to-connect-to-google-analytics-data-api-in-air/#comments</comments>
		<pubDate>Fri, 22 May 2009 03:37:38 +0000</pubDate>
		<dc:creator>zedia.net</dc:creator>
				<category><![CDATA[AIR]]></category>
		<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[as3corelib]]></category>
		<category><![CDATA[as3crypto]]></category>
		<category><![CDATA[as3httpclientlib]]></category>
		<category><![CDATA[GData]]></category>
		<category><![CDATA[Google Analytics]]></category>
		<category><![CDATA[Google Analytics Data Api]]></category>

		<guid isPermaLink="false">http://www.zedia.net/?p=415</guid>
		<description><![CDATA[I had a bit of trouble doing just this a while ago, ended up trying many different ways and finally a reader of this blog, Nakamachi, gave me the solution. Because I think many of you are trying to connect to Google Analytics Data API ( or any Google APIs for that matters) I will [...]]]></description>
			<content:encoded><![CDATA[<p>I had a bit of trouble doing <a title="Connecting to Google Analytics Data Api using AS3; my failures" href="http://www.zedia.net/2009/connecting-to-google-analytics-data-api-using-as3-my-failures/" target="_self">just this a while ago</a>, ended up trying many different ways and finally a reader of this blog, <span class="info">Nakamachi, gave me the solution. Because I think many of you are trying to connect to Google Analytics Data API ( or any Google APIs for that matters) I will give you here my solution. My solution only works in AIR; I don&#8217;t know why, but as soon as I put it on the web, it stops working. I have found a solution for that problem too but I will keep it for another post.</span><br />
Here is how you can connect to GData:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">import</span> org.<span style="color: #006600;">httpclient</span>.<span style="color: #006600;">HttpClient</span>;
<span style="color: #0066CC;">import</span> com.<span style="color: #006600;">adobe</span>.<span style="color: #006600;">net</span>.<span style="color: #006600;">URI</span>;
<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">httpclient</span>.<span style="color: #006600;">events</span>.<span style="color: #006600;">HttpResponseEvent</span>;
<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">httpclient</span>.<span style="color: #006600;">events</span>.<span style="color: #006600;">HttpRequestEvent</span>;
<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">httpclient</span>.<span style="color: #006600;">HttpRequest</span>;
<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">httpclient</span>.<span style="color: #006600;">http</span>.<span style="color: #0066CC;">Get</span>;
<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">httpclient</span>.<span style="color: #006600;">events</span>.<span style="color: #006600;">HttpDataEvent</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">var</span> _firstBuffer:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;&quot;</span>;
<span style="color: #000000; font-weight: bold;">var</span> _secondBuffer:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;&quot;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">//this first connection will get the authorization token by sending the user name and password for Google Analytics</span>
<span style="color: #000000; font-weight: bold;">function</span> submitForm<span style="color: #66cc66;">&#40;</span>email:<span style="color: #0066CC;">String</span>, <span style="color: #0066CC;">password</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">var</span> client:HttpClient = <span style="color: #000000; font-weight: bold;">new</span> HttpClient<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #000000; font-weight: bold;">var</span> uri:URI = <span style="color: #000000; font-weight: bold;">new</span> URI<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;https://www.google.com/accounts/ClientLogin&quot;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #000000; font-weight: bold;">var</span> variables:<span style="color: #0066CC;">Array</span> = <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#123;</span><span style="color: #0066CC;">name</span>:<span style="color: #ff0000;">&quot;accountType&quot;</span>, value:<span style="color: #ff0000;">&quot;GOOGLE&quot;</span><span style="color: #66cc66;">&#125;</span>, <span style="color: #66cc66;">&#123;</span><span style="color: #0066CC;">name</span>:<span style="color: #ff0000;">&quot;Email&quot;</span>, value: email<span style="color: #66cc66;">&#125;</span>, <span style="color: #66cc66;">&#123;</span><span style="color: #0066CC;">name</span>:<span style="color: #ff0000;">&quot;Passwd&quot;</span>, value: <span style="color: #0066CC;">password</span><span style="color: #66cc66;">&#125;</span>, <span style="color: #66cc66;">&#123;</span><span style="color: #0066CC;">name</span>:<span style="color: #ff0000;">&quot;service&quot;</span>, value: <span style="color: #ff0000;">&quot;analytics&quot;</span><span style="color: #66cc66;">&#125;</span>, <span style="color: #66cc66;">&#123;</span><span style="color: #0066CC;">name</span>:<span style="color: #ff0000;">&quot;source&quot;</span>, value: <span style="color: #ff0000;">&quot;your-application-identifier&quot;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#93;</span>;
  client.<span style="color: #006600;">listener</span>.<span style="color: #0066CC;">onData</span> = _onFirstData;
  client.<span style="color: #006600;">listener</span>.<span style="color: #006600;">onComplete</span> = _onLoaderComplete;
  client.<span style="color: #006600;">postFormData</span><span style="color: #66cc66;">&#40;</span>uri, variables<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> _onFirstData<span style="color: #66cc66;">&#40;</span>event:HttpDataEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
  _firstBuffer += event.<span style="color: #006600;">readUTFBytes</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//this second connection will request information to GData</span>
<span style="color: #808080; font-style: italic;">//in this case account info for Google Analytics by</span>
<span style="color: #808080; font-style: italic;">//putting the auth token in the header of the request</span>
<span style="color: #000000; font-weight: bold;">function</span> _onLoaderComplete<span style="color: #66cc66;">&#40;</span>event:HttpResponseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">var</span> tempArray:<span style="color: #0066CC;">Array</span> = _firstBuffer.<span style="color: #0066CC;">split</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Auth=&quot;</span><span style="color: #66cc66;">&#41;</span>;
 <span style="color: #000000; font-weight: bold;">var</span>  _authToken:<span style="color: #0066CC;">String</span> = <span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#40;</span>tempArray<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
  <span style="color: #000000; font-weight: bold;">var</span> client:HttpClient = <span style="color: #000000; font-weight: bold;">new</span> HttpClient<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #000000; font-weight: bold;">var</span> uri:URI = <span style="color: #000000; font-weight: bold;">new</span> URI<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;https://www.google.com/analytics/feeds/accounts/default&quot;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #000000; font-weight: bold;">var</span> request:HttpRequest = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Get</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
  request.<span style="color: #006600;">addHeader</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Authorization&quot;</span>, <span style="color: #ff0000;">&quot;GoogleLogin auth=&quot;</span> + _authToken<span style="color: #66cc66;">&#41;</span>;
&nbsp;
  client.<span style="color: #006600;">listener</span>.<span style="color: #0066CC;">onData</span> = <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>event:HttpDataEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
    _secondBuffer += event.<span style="color: #006600;">readUTFBytes</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>
  client.<span style="color: #006600;">listener</span>.<span style="color: #006600;">onComplete</span> = _onSecondLoaderComplete;
  client.<span style="color: #006600;">request</span><span style="color: #66cc66;">&#40;</span>uri, request<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> _onSecondLoaderComplete<span style="color: #66cc66;">&#40;</span>event:HttpResponseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
  <span style="color: #0066CC;">trace</span> <span style="color: #66cc66;">&#40;</span>_secondBuffer<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>As you can see in order to circumvent the Authorization header restriction I am using the <a title="as3httpclientlib" href="http://code.google.com/p/as3httpclientlib/">as3httpclientlib</a> library which requires the <a title="as3corelib" href="http://code.google.com/p/as3corelib/" target="_blank">as3corelib</a> and <a title="as3crypto" href="http://code.google.com/p/as3crypto/" target="_blank">as3crypto</a> libraries to compile. Using the same principle you should be able to connect to Google APIs from your AIR application.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zedia.net/2009/how-to-connect-to-google-analytics-data-api-in-air/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Connecting to Google Analytics Data Api using AS3; my failures</title>
		<link>http://www.zedia.net/2009/connecting-to-google-analytics-data-api-using-as3-my-failures/</link>
		<comments>http://www.zedia.net/2009/connecting-to-google-analytics-data-api-using-as3-my-failures/#comments</comments>
		<pubDate>Mon, 04 May 2009 23:26:13 +0000</pubDate>
		<dc:creator>zedia.net</dc:creator>
				<category><![CDATA[AIR]]></category>
		<category><![CDATA[as3httpclientlib]]></category>
		<category><![CDATA[Authorization]]></category>
		<category><![CDATA[Desktop Reporting]]></category>
		<category><![CDATA[Google Analytics Data Api]]></category>
		<category><![CDATA[HttpClient]]></category>
		<category><![CDATA[HTTPService]]></category>
		<category><![CDATA[Polaris]]></category>
		<category><![CDATA[URLLoader]]></category>
		<category><![CDATA[URLRequestHeader]]></category>

		<guid isPermaLink="false">http://www.zedia.net/?p=409</guid>
		<description><![CDATA[EDIT: Finally I turned my failures into success you can read it here: How to connect to Google Analytics Data API in AIR How to connect to Google Analytics Data API in Flash These past three days I have spent my time trying to connect from an AIR application to Google Analytics Data Api and [...]]]></description>
			<content:encoded><![CDATA[<p>EDIT:<br />
Finally I turned my failures into success you can read it here:<br />
<a href="http://www.zedia.net/2009/how-to-connect-to-google-analytics-data-api-in-air/" target="_self">How to connect to Google Analytics Data API in AIR</a><br />
<a href="http://www.zedia.net/2009/how-to-connect-to-google-analytics-data-api-in-flash/" target="_self">How to connect to Google Analytics Data API in Flash</a></p>
<p>These past three days I have spent my time trying to connect from an AIR application to <a title="Google Analytics Data Api" href="http://code.google.com/apis/analytics/docs/gdata/1.0/gdataProtocol.html#ClientLogin" target="_blank">Google Analytics Data Api</a> and I have failed. Sometimes you just admit to yourself thatwhat you want to do is just impossible. The problem is that I know it is feasible, <a title="Desktop Reporting" href="http://www.desktop-reporting.com/" target="_blank">Desktop Reporting</a> is doing it with their AIR application called <a title="Polaris" href="http://www.desktop-reporting.com/polaris.html" target="_blank">Polaris</a>. Now all I think about is how I can solve this problem. In this post I&#8217;m going to show what I tried in order to accomplish this so that it might help others or that others can help me.</p>
<p>The big obstacle at the root of my problem is the fact that you can&#8217;t set the Authorization header in ActionScript which is required to make request to the Google server to get data. Here is how I found out about this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> request:URLRequest = <span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;https://www.google.com/analytics/feeds/accounts/default&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
request.<span style="color: #006600;">method</span> = URLRequestMethod.<span style="color: #0066CC;">GET</span>;
<span style="color: #000000; font-weight: bold;">var</span> tempHeader:URLRequestHeader = <span style="color: #000000; font-weight: bold;">new</span> URLRequestHeader<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Authorization&quot;</span>, <span style="color: #ff0000;">&quot;GoogleLogin auth=&quot;</span> + _authToken<span style="color: #66cc66;">&#41;</span>;
request.<span style="color: #006600;">requestHeaders</span> = <span style="color: #66cc66;">&#91;</span>tempHeader<span style="color: #66cc66;">&#93;</span>;
<span style="color: #000000; font-weight: bold;">var</span>  _secondLoader:URLLoader = <span style="color: #000000; font-weight: bold;">new</span> URLLoader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
_secondLoader.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>, _onSecondLoaderComplete, <span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
_secondLoader.<span style="color: #006600;">dataFormat</span> = URLLoaderDataFormat.<span style="color: #0066CC;">TEXT</span>;
_secondLoader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span>request<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>This resulted in the following runtime error:</p>
<p>ArgumentError: Error #2096: The HTTP request header GoogleLogin auth=DQA &#8230; -lQ<br />
cannot be set via ActionScript</p>
<p>From that point on I was aware that you couldn&#8217;t set the Authorization request header even thought you see <a title="Misleading" href="http://kb2.adobe.com/cps/403/kb403030.html" target="_blank">misleading information</a> around the web that doesn&#8217;t help. My next try was using HTTPService from the Flex library here is what I tried:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">gService.<span style="color: #0066CC;">url</span> = <span style="color: #ff0000;">&quot;https://www.google.com/analytics/feeds/accounts/default&quot;</span>;
&nbsp;
gService.<span style="color: #006600;">headers</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;Authorization&quot;</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #ff0000;">&quot;GoogleLogin auth=&quot;</span> + _authToken;
<span style="color: #000000; font-weight: bold;">var</span> token:AsyncToken = gService.<span style="color: #0066CC;">send</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
token.<span style="color: #006600;">addResponder</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Responder<span style="color: #66cc66;">&#40;</span>onResponse, onFault<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>This ended up with the same result (obviously).</p>
<p>My next try was using the <a title="as3httpclientlib" href="http://code.google.com/p/as3httpclientlib/" target="_blank">as3httpclient</a> library which I did in this way:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> client:HttpClient = <span style="color: #000000; font-weight: bold;">new</span> HttpClient<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">var</span> uri:URI = <span style="color: #000000; font-weight: bold;">new</span> URI<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;https://www.google.com/analytics/feeds/accounts/default&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">var</span> request:HttpRequest = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Get</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
request.<span style="color: #006600;">addHeader</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Authorization&quot;</span>, <span style="color: #ff0000;">&quot;GoogleLogin auth=&quot;</span> + _authToken<span style="color: #66cc66;">&#41;</span>;
&nbsp;
client.<span style="color: #006600;">listener</span>.<span style="color: #0066CC;">onData</span> = <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>event:HttpDataEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
<span style="color: #0066CC;">trace</span> <span style="color: #66cc66;">&#40;</span>event.<span style="color: #006600;">readUTFBytes</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#125;</span>;
client.<span style="color: #006600;">request</span><span style="color: #66cc66;">&#40;</span>uri, request<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>This seemed to work a bit better but it always traced &#8220;200&#8243; and not the xml feed it was supposed to return.</p>
<p>So I am at this point very frustrated because I am not able to do something I know is feasible. I have other step I can take in my project and I have some not very optimal way of getting the data, but I have a very bitter taste left in my mouth.  If anyone can help me with this, it would be really appreciated.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zedia.net/2009/connecting-to-google-analytics-data-api-using-as3-my-failures/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>

