<?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>润物无声 &#187; API</title>
	<atom:link href="http://blog.zhourunsheng.com/tag/api/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.zhourunsheng.com</link>
	<description>天空一朵雨做的云</description>
	<lastBuildDate>Sat, 08 May 2021 05:17:21 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.41</generator>
	<item>
		<title>Google Plus API 之 程序设计</title>
		<link>http://blog.zhourunsheng.com/2011/09/google-plus-api-%e4%b9%8b-%e7%a8%8b%e5%ba%8f%e8%ae%be%e8%ae%a1/</link>
		<comments>http://blog.zhourunsheng.com/2011/09/google-plus-api-%e4%b9%8b-%e7%a8%8b%e5%ba%8f%e8%ae%be%e8%ae%a1/#comments</comments>
		<pubDate>Sat, 17 Sep 2011 05:25:22 +0000</pubDate>
		<dc:creator><![CDATA[润物无声]]></dc:creator>
				<category><![CDATA[Web设计]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[SAE]]></category>

		<guid isPermaLink="false">http://blog.zhourunsheng.com/?p=888</guid>
		<description><![CDATA[<p>昨天Google官方发布新闻(Getting started on the Google+ API)，Goog [&#8230;]</p>
<p><a rel="nofollow" href="http://blog.zhourunsheng.com/2011/09/google-plus-api-%e4%b9%8b-%e7%a8%8b%e5%ba%8f%e8%ae%be%e8%ae%a1/">Google Plus API 之 程序设计</a>，首发于<a rel="nofollow" href="http://blog.zhourunsheng.com">润物无声</a>。</p>
]]></description>
				<content:encoded><![CDATA[<p>昨天Google官方发布新闻(<a href="http://googlecode.blogspot.com/2011/09/getting-started-on-google-api.html">Getting started on the Google+ <em>API</em></a>)，Google Plus开放了一个只读的API，允许抓取用户的公开Profile和公开的Activity信息流，其中<a href="http://developers.google.com/+/">Google+ Platform</a>的具体开发API介绍详见官网。</p>
<p>今天我就带领大家熟悉一下基于Google+ 开放API的程序设计，本程序基于SAE平台，采用PHP语言开发，基于Java语言和Python语言也都比较好开发，google官方都有开放的lib可以直接使用，不用自己再做底层封装了。<span id="more-888"></span></p>
<h3>实现步骤</h3>
<ol>
<li>访问 <a href="https://code.google.com/apis/console/?api=plus" rel="nofollow">Google API Console</a> 开启Google+ API权限.<br />
<img class="alignnone size-full wp-image-891" title="active_google_plus" src="http://blog.zhourunsheng.com/wp-content/uploads/2011/09/active_google_plus.png" alt="" width="668" height="40" /></li>
<li>访问 <a href="https://code.google.com/apis/console/?api=plus" rel="nofollow">Google API Console</a> 生成  developer key, OAuth2 client id, OAuth2 client secret, 和 注册 OAuth2 redirect uri.<br />
<img class="alignnone size-full wp-image-890" title="google_plus_client_id" src="http://blog.zhourunsheng.com/wp-content/uploads/2011/09/google_plus_client_id.png" alt="" width="754" height="151" /><br />
<img class="alignnone size-full wp-image-892" title="google-plus-simple-api-access" src="http://blog.zhourunsheng.com/wp-content/uploads/2011/09/google-plus-simple-api-access.png" alt="" width="576" height="147" /></li>
<li>下载 google api的php开发库，<a href="http://google-api-php-client.googlecode.com/files/google-api-php-client-0.4.4.tar.gz">google-api-php-client</a>.</li>
<li>更新google api开发库，因为我的运行环境基于新浪云SAE平台，所以要做一些特别的改动：<br />
1. 实现SAE平台的MemCache，在google-api-php-clientsrccache中建立apiSAEMemcacheCache.php文件，具体内容如下</p>
<pre>&lt;?php
/**
 * SAE Memcache
 *
 * @author Carey Zhou &lt;zhourunsheng2008@google.com&gt;
 */
class apiSAEMemcacheCache extends apiCache {
  private $connection = false;

  public function __construct() {
	//do nothing
  }

  private function connect() {
    if (! $this-&gt;connection = @memcache_init()) {
      throw new apiCacheException("Memcache init failed");
    }
  }

  private function check() {
    if (! $this-&gt;connection) {
      $this-&gt;connect();
    }
  }

  /**
   * @inheritDoc
   */
  public function get($key, $expiration = false) {
    $this-&gt;check();
    if (($ret = @memcache_get($this-&gt;connection, $key)) === false) {
      return false;
    }
    if (! $expiration || (time() - $ret['time'] &gt; $expiration)) {
      $this-&gt;delete($key);
      return false;
    }
    return $ret['data'];
  }

  /**
   * @inheritDoc
   */
  public function set($key, $value) {
    $this-&gt;check();
    // we store it with the cache_time default expiration so objects will at least get cleaned eventually.
    if (@memcache_set($this-&gt;connection, $key, array('time' =&gt; time(),
        'data' =&gt; $value), false) == false) {
      throw new apiCacheException("Couldn't store data in cache");
    }
  }

  /**
   * @inheritDoc
   */
  public function delete($key) {
    $this-&gt;check();
    @memcache_delete($this-&gt;connection, $key);
  }
}</pre>
<p>2. 更新 google-api-php-clientsrccacheapiCache.php文件，将我们新添加的SAEMemcache加进去，代码如下：</p>
<pre>require_once "apiSAEMemcacheCache.php";</pre>
<p>3. 更新 google-api-php-clientsrcioapiCurlIO.php文件，修正一个bug【当Header中出现"HTTP/1.1 100 Continue"的时候，原程序会出现解析错误，导致程序崩溃，估计也是SAE环境的特殊性导致的】，具体改正如下：</p>
<p>原来的代码：</p>
<pre>// Parse out the raw response into usable bits
    list($rawResponseHeaders, $responseBody) = explode("rnrn", $data, 2);</pre>
<p>现在的代码：</p>
<pre>// Parse out the raw response into usable bits
    list($rawResponseHeaders, $responseBody) = explode("rnrn", $data, 2);
    if ($rawResponseHeaders == "HTTP/1.1 100 Continue") {
	list($rawResponseHeaders, $responseBody) = explode("rnrn", $responseBody, 2);
    }</pre>
<p>更改原因：如果出现了"HTTP/1.1 100 Continue"的时候，还要继续向下解析一次，即出现了特殊的“双Header现象”，否则的话就会导致Header解析出错，真正的Header没有被解析出来，也混到body里面去了。</li>
<li>google plus 代码编写，获取用户公开的Activity信息流，核心代码如下：
<pre>&lt;?php
/*
 * Google Plus
 *
 * @author Carey Zhou(zhourunsheng2008@gmail.com)
 */
require_once '../libs/google-api-php-client/src/apiClient.php';
require_once '../libs/google-api-php-client/src/contrib/apiPlusService.php';

//sae_set_display_errors(true);

session_start();

$client = new apiClient();
$client-&gt;setApplicationName("Google Plus Application");

$client-&gt;setClientId('800611776987-kp6h5nfv5l9gp5v2qipqhc5l8dkqe0lu.apps.googleusercontent.com');
$client-&gt;setClientSecret('5o-eK5M4iftPxB7_fhNqEAhm');
$client-&gt;setRedirectUri('https://carey.sinaapp.com/googleplus/index.php');
$client-&gt;setDeveloperKey('AIzaSyD6FERuKcWPn2xGUcgFmKW9Ush50nu6PYQ');

$client-&gt;setScopes(array('https://www.googleapis.com/auth/plus.me'));
$plus = new apiPlusService($client);

if (isset($_REQUEST['logout'])) {
  unset($_SESSION['access_token']);
}

if (isset($_GET['code'])) {
  $client-&gt;authenticate();
  $_SESSION['access_token'] = $client-&gt;getAccessToken();
  //header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
  $url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  echo "&lt;script language='javascript' type='text/javascript'&gt;";
  echo "window.location.href='$url'";
  echo "&lt;/script&gt;";
}

if (isset($_SESSION['access_token'])) {
  $client-&gt;setAccessToken($_SESSION['access_token']);
}

if ($client-&gt;getAccessToken()) {
  $me = $plus-&gt;people-&gt;get('me');

  $optParams = array('maxResults' =&gt; 100);
  $activities = $plus-&gt;activities-&gt;listActivities('me', 'public', $optParams);

  // The access token may have been updated lazily.
  $_SESSION['access_token'] = $client-&gt;getAccessToken();
} else {
  $authUrl = $client-&gt;createAuthUrl();
}
?&gt;</pre>
<p>可以注意到，在程序中我们配置了ClientID，ClientSecret，RedirectUri，DeveloperKey和Google+的scope，然后通过OAuth2机制获取用户的授权，进而来获得用户公开的Activity信息流。</li>
</ol>
<h3>程序部署和验证</h3>
<ol>
<li>把上面的代码部署到SAE平台，<a href="https://carey.sinaapp.com/googleplus/index.php">https://carey.sinaapp.com/googleplus/index.php</a><br />
<img class="alignnone size-medium wp-image-901" title="google-plus-carey-sinaapp" src="http://blog.zhourunsheng.com/wp-content/uploads/2011/09/google-plus-carey-sinaapp-300x161.png" alt="" width="300" height="161" /></li>
<li>点击Connet Me进行OAuth2用户授权<br />
<img class="alignnone size-medium wp-image-902" title="google-plus-user-oauth" src="http://blog.zhourunsheng.com/wp-content/uploads/2011/09/google-plus-user-oauth-300x161.png" alt="" width="300" height="161" /></li>
<li>点击Allow Access允许访问，则会回到刚才的页面，把用户的信息流抓取出来<br />
<img class="alignnone size-medium wp-image-904" title="google-plus-get-activity" src="http://blog.zhourunsheng.com/wp-content/uploads/2011/09/google-plus-get-activity-300x161.png" alt="" width="300" height="161" /></li>
</ol>
<h3>小结</h3>
<p>到此为止，google plus 基于PHP开发的基本流程就都OK了，希望google过几天能开放出更多的API来，毕竟google+是个圈，所以圈子的API才是最核心的，O(∩_∩)O哈哈~,期待中</p>
<p>源码下载<a href="http://blog.zhourunsheng.com/wp-content/uploads/2011/09/google_plus_sae_source.zip">google_plus_sae_source</a></p>
<p><a rel="nofollow" href="http://blog.zhourunsheng.com/2011/09/google-plus-api-%e4%b9%8b-%e7%a8%8b%e5%ba%8f%e8%ae%be%e8%ae%a1/">Google Plus API 之 程序设计</a>，首发于<a rel="nofollow" href="http://blog.zhourunsheng.com">润物无声</a>。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.zhourunsheng.com/2011/09/google-plus-api-%e4%b9%8b-%e7%a8%8b%e5%ba%8f%e8%ae%be%e8%ae%a1/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Google API</title>
		<link>http://blog.zhourunsheng.com/2011/08/google-api/</link>
		<comments>http://blog.zhourunsheng.com/2011/08/google-api/#comments</comments>
		<pubDate>Tue, 02 Aug 2011 09:09:06 +0000</pubDate>
		<dc:creator><![CDATA[润物无声]]></dc:creator>
				<category><![CDATA[程序设计]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[Google]]></category>

		<guid isPermaLink="false">http://blog.zhourunsheng.com/?p=636</guid>
		<description><![CDATA[<p>本文收集了一系列的Google开发API，可以有效与自己的web服务相集成，提高开发效率。 1) Lattit [&#8230;]</p>
<p><a rel="nofollow" href="http://blog.zhourunsheng.com/2011/08/google-api/">Google API</a>，首发于<a rel="nofollow" href="http://blog.zhourunsheng.com">润物无声</a>。</p>
]]></description>
				<content:encoded><![CDATA[<p>本文收集了一系列的Google开发API，可以有效与自己的web服务相集成，提高开发效率。<br />
<img class="alignnone size-full wp-image-637" title="google-api" src="http://blog.zhourunsheng.com/wp-content/uploads/2011/08/google-api.jpg" alt="" width="250" height="252" /><br />
<span id="more-636"></span></p>
<h3>1) <a href="http://code.google.com/apis/latitude/">Lattitude API</a></h3>
<p><img class="alignnone size-full wp-image-638" title="Lattitude API" src="http://blog.zhourunsheng.com/wp-content/uploads/2011/08/Lattitude-API.jpg" alt="" width="500" height="375" /></p>
<h3>2) <a href="http://code.google.com/apis/earth/">Earth API</a></h3>
<p><img class="alignnone size-full wp-image-639" title="Earth API" src="http://blog.zhourunsheng.com/wp-content/uploads/2011/08/Earth-API.jpg" alt="" width="500" height="356" /></p>
<h3>3) <a href="http://code.google.com/apis/earth/">Google Map API</a></h3>
<p><img class="alignnone size-full wp-image-640" title="Google Map API" src="http://blog.zhourunsheng.com/wp-content/uploads/2011/08/Google-Map-API.jpg" alt="" width="500" height="310" /></p>
<h3>4)<a href="http://code.google.com/apis/maps/documentation/geocoding/"> GeoCoding API</a></h3>
<p><img class="alignnone size-full wp-image-641" title="GeoCoding API" src="http://blog.zhourunsheng.com/wp-content/uploads/2011/08/GeoCoding-API.jpg" alt="" width="397" height="319" /></p>
<h3>5)<a href="http://code.google.com/apis/maps/documentation/directions/#Introduction"> Google Direction API</a></h3>
<p><img class="alignnone size-full wp-image-642" title="Google Direction API" src="http://blog.zhourunsheng.com/wp-content/uploads/2011/08/Google-Direction-API.gif" alt="" width="500" height="227" /></p>
<h3>6)<a href="http://code.google.com/apis/maps/documentation/staticmaps/"> Static API</a></h3>
<p><img class="alignnone size-full wp-image-643" title="Static API" src="http://blog.zhourunsheng.com/wp-content/uploads/2011/08/Static-API.png" alt="" width="500" height="493" /></p>
<h3>7)<a href="http://code.google.com/apis/maps/documentation/places/#Introduction"> Places API</a></h3>
<p><img class="alignnone size-full wp-image-644" title="Places API" src="http://blog.zhourunsheng.com/wp-content/uploads/2011/08/Places-API.png" alt="" width="500" height="332" /></p>
<h3>8 ) <a href="http://code.google.com/apis/buzz/">Buzz API</a></h3>
<p><img class="alignnone size-full wp-image-646" title="Buzz API" src="http://blog.zhourunsheng.com/wp-content/uploads/2011/08/Buzz-API1.png" alt="" width="500" height="449" /></p>
<h3>9)<a href="http://code.google.com/apis/friendconnect/">Friend Connect API</a></h3>
<p><img class="alignnone size-full wp-image-661" title="Friend Connect API" src="http://blog.zhourunsheng.com/wp-content/uploads/2011/08/Friend-Connect-API.png" alt="" width="432" height="426" /></p>
<h3>10)<a href="http://code.google.com/apis/socialgraph/"> Social Graph API</a></h3>
<p><a href="http://blog.zhourunsheng.com/wp-content/uploads/2011/08/Social-Graph-API.jpg"><img class="alignnone size-full wp-image-660" title="Social Graph API" src="http://blog.zhourunsheng.com/wp-content/uploads/2011/08/Social-Graph-API.jpg" alt="" width="500" height="496" /></a></p>
<h3>11) <a href="http://code.google.com/apis/wave/">Wave API</a></h3>
<p><img class="alignnone size-full wp-image-659" title="Wave API" src="http://blog.zhourunsheng.com/wp-content/uploads/2011/08/Wave-API.jpg" alt="" width="500" height="325" /></p>
<h3>12)<a href="http://code.google.com/apis/youtube/overview.html"> YouTube API’s</a></h3>
<p><img class="alignnone size-full wp-image-663" title="YouTube API" src="http://blog.zhourunsheng.com/wp-content/uploads/2011/08/YouTube-API.jpg" alt="" width="500" height="375" /></p>
<h3>13)<a href="http://code.google.com/apis/adwords/"> ADwords API</a></h3>
<p><img class="alignnone size-full wp-image-657" title="ADwords API" src="http://blog.zhourunsheng.com/wp-content/uploads/2011/08/ADwords-API.png" alt="" width="500" height="237" /></p>
<h3>14) <a href="http://code.google.com/apis/adsense/">Adsense API</a></h3>
<p><img class="alignnone size-full wp-image-656" title="Adsense API" src="http://blog.zhourunsheng.com/wp-content/uploads/2011/08/Adsense-API1.jpg" alt="" width="558" height="70" /></p>
<h3>15)<a href="http://code.google.com/apis/blogger/"> Blogger Data API</a></h3>
<p><img class="alignnone size-full wp-image-654" title="Blogger Data API" src="http://blog.zhourunsheng.com/wp-content/uploads/2011/08/Blogger-Data-API.png" alt="" width="500" height="237" /></p>
<h3>16)<a href="http://code.google.com/apis/webmastertools/"> Webmaster Tools API</a></h3>
<p><img class="alignnone size-full wp-image-653" title="Webmaster Tools API" src="http://blog.zhourunsheng.com/wp-content/uploads/2011/08/Webmaster-Tools-API.png" alt="" width="500" height="237" /></p>
<h3>17) <a href="http://code.google.com/apis/codesearch/">Code Search API</a></h3>
<p><img class="alignnone size-full wp-image-652" title="Code Search API" src="http://blog.zhourunsheng.com/wp-content/uploads/2011/08/Code-Search-API.png" alt="" width="500" height="237" /></p>
<h3>18) <a href="http://code.google.com/apis/picasaweb/overview.html">Picasa Web Album  DATA API</a></h3>
<p><img class="alignnone size-full wp-image-651" title="Picasa Web Album  DATA API" src="http://blog.zhourunsheng.com/wp-content/uploads/2011/08/Picasa-Web-Album-DATA-API.png" alt="" width="500" height="237" /></p>
<h3>19) <a href="http://code.google.com/apis/analytics/docs/mgmt/home.html">Management API</a></h3>
<p><img class="alignnone size-full wp-image-650" title="Management API" src="http://blog.zhourunsheng.com/wp-content/uploads/2011/08/Management-API.png" alt="" width="500" height="237" /></p>
<h3>20) <a href="http://code.google.com/apis/webfonts/">Font API</a></h3>
<p><img class="alignnone size-full wp-image-649" title="Font API" src="http://blog.zhourunsheng.com/wp-content/uploads/2011/08/Font-API.png" alt="" width="500" height="237" /></p>
<h3>21)<a href="http://code.google.com/apis/urlshortener/"> URL Shortner API</a></h3>
<p><img class="alignnone size-full wp-image-648" title="URL Shortner API" src="http://blog.zhourunsheng.com/wp-content/uploads/2011/08/URL-Shortner-API.png" alt="" width="500" height="237" /></p>
<p><a rel="nofollow" href="http://blog.zhourunsheng.com/2011/08/google-api/">Google API</a>，首发于<a rel="nofollow" href="http://blog.zhourunsheng.com">润物无声</a>。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.zhourunsheng.com/2011/08/google-api/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
