<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[Серый форум &mdash; Google API. Веб-приложение за 5 минут.]]></title>
		<link>http://forum.script-coding.com/viewtopic.php?id=8205</link>
		<atom:link href="http://forum.script-coding.com/extern.php?action=feed&amp;tid=8205&amp;type=rss" rel="self" type="application/rss+xml" />
		<description><![CDATA[Недавние сообщения в теме «Google API. Веб-приложение за 5 минут.».]]></description>
		<lastBuildDate>Wed, 03 Apr 2013 11:31:39 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Google API. Веб-приложение за 5 минут.]]></title>
			<link>http://forum.script-coding.com/viewtopic.php?pid=71058#p71058</link>
			<description><![CDATA[<p>Заголовок говорит сам за себя. На регистрацию проекта уйдет больше времени, чем на кодинг. Приложение будет укорачивать URL, для чего используем URL Shortener API. Требования: аккаунт Google, браузер, блокнот и 5 минут времени. Go Go Go <img src="//forum.script-coding.com/img/smilies/smile.png" width="15" height="15" />.<br /><span class="postimg"><img src="http://1.bp.blogspot.com/-C--L4WIpyCo/UVwAqkmqFDI/AAAAAAAAAtA/z4162Vwb7RI/s200/shorten-google-map-URL.jpg" alt="http://1.bp.blogspot.com/-C--L4WIpyCo/UVwAqkmqFDI/AAAAAAAAAtA/z4162Vwb7RI/s200/shorten-google-map-URL.jpg" /></span><br />Открываем в браузере консоль API по адресу <a href="https://code.google.com/apis/console/">https://code.google.com/apis/console/</a>.<br />Создаем новый проект.<br />Если это наш первый проект, нажимаем на большую синюю кнопку.<br /><span class="postimg"><img src="http://3.bp.blogspot.com/-JOLa1YO1ZUo/UVwCA16n1II/AAAAAAAAAtM/O1qCqipwA64/s1600/uapi1.png" alt="http://3.bp.blogspot.com/-JOLa1YO1ZUo/UVwCA16n1II/AAAAAAAAAtM/O1qCqipwA64/s1600/uapi1.png" /></span><br />Активируем URL Shortener API.<br /><span class="postimg"><img src="http://2.bp.blogspot.com/-gutk0GzkqJw/UVwCL80NEKI/AAAAAAAAAtU/WqCh2A5w_T4/s1600/uapi2.png" alt="http://2.bp.blogspot.com/-gutk0GzkqJw/UVwCL80NEKI/AAAAAAAAAtU/WqCh2A5w_T4/s1600/uapi2.png" /></span><br />Переходим к параметрам доступа к проекту (вкладка API Access) и копируем ключ проекта (API key).<br /><span class="postimg"><img src="http://4.bp.blogspot.com/-pt6ujLFcT-M/UVwCY5SMLSI/AAAAAAAAAtc/2ef1gdMrzug/s640/uapi4.png" alt="http://4.bp.blogspot.com/-pt6ujLFcT-M/UVwCY5SMLSI/AAAAAAAAAtc/2ef1gdMrzug/s640/uapi4.png" /></span><br />Открываем блокнот, пишем нехитрый код.<br /></p><div class="codebox"><pre><code>
&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;URL Shortener&lt;/title&gt;
        &lt;style type=&quot;text/css&quot;&gt;
            button{width:100%; margin-bottom:1px; font:bold 18px Georgia serif; cursor:pointer;}
            input{width:100%;}
            a{width:50%; display:inline-block;}
        &lt;/style&gt;
        &lt;script&gt;      
        function shorten() {            
            var url = document.getElementById(&#039;txt&#039;).value;
            if (url == &#039;&#039;) return;        
            var xhr = new XMLHttpRequest();
            xhr.open(&#039;POST&#039;, &#039;https://www.googleapis.com/urlshortener/v1/url?key=Ключ_Вашего_Проекта&#039;, true);
            xhr.setRequestHeader(&#039;Content-Type&#039;, &#039;application/json&#039;);
            xhr.onreadystatechange = function() {
                if(xhr.readyState != 4 || xhr.status != 200) return;
                var res = JSON.parse(xhr.responseText)
                var div = document.body.appendChild(document.createElement(&quot;div&quot;));
                var s1 = document.createElement(&quot;a&quot;);
                s1.href = res.id;
                s1.target = &#039;_blank&#039;;
                s1.appendChild(document.createTextNode(res.id));
                var s2 = document.createElement(&quot;a&quot;);
                s2.href = res.longUrl;
                s2.target = &#039;_blank&#039;;
                s2.appendChild(document.createTextNode(res.longUrl));
                div.appendChild(s1);
                div.appendChild(s2);            
            }
            xhr.send(JSON.stringify({&#039;longUrl&#039;: url}));
        }
        function keyUp(e) {
            var keyCode = e.which || e.keyCode;                
            if(keyCode != 13) return;
            shorten();
        }
        &lt;/script&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;button onclick=&quot;shorten()&quot;&gt;Shorten&lt;/button&gt;
        &lt;input id=&quot;txt&quot; type=&quot;text&quot; onkeyup=&quot;keyUp(event)&quot;/&gt;    
    &lt;/body&gt;
&lt;/html&gt;
</code></pre></div><p>Сохраняем, открываем в браузере, тестируем.<br /><span class="postimg"><img src="http://2.bp.blogspot.com/-lrvtnGkkoKY/UVwDOOjs7WI/AAAAAAAAAto/w9otzp2vPoM/s1600/uapi5.png" alt="http://2.bp.blogspot.com/-lrvtnGkkoKY/UVwDOOjs7WI/AAAAAAAAAto/w9otzp2vPoM/s1600/uapi5.png" /></span></p><p>Для полноты ощущений размещаем <a href="http://goo.gl/ANrS7">файл на Google Drive</a>.<br />That&#039;s all folks.</p><p>p.s. Полученное приложение поддерживается <a href="http://caniuse.com/#search=cors">&quot;современными&quot; браузерами</a>.<br />p.p.s. <a href="http://www.daspot.ru/2013/04/google-api-5.html">Оригинал статьи</a>.</p>]]></description>
			<author><![CDATA[null@example.com (dab00)]]></author>
			<pubDate>Wed, 03 Apr 2013 11:31:39 +0000</pubDate>
			<guid>http://forum.script-coding.com/viewtopic.php?pid=71058#p71058</guid>
		</item>
	</channel>
</rss>
