原文链接:http://blog.xiaonei.com/GetEntry.do?id=379273765&owner=253723720
51.com既然已经实现了opensocial restful api 那我们就可以用他的opensocial restful api开发我们的应用啦:)
第一步:我们看一下在申请应用的时候需要填写那些内容
必填部分
- //这里写我们应用的url地址【通常在自己的服务器】
- http://apps.51.com/
- 51ML iframe OpenSocial JS OpenSocial RESTful
这里选择了opensocial restful 。
第二步:
-
65368138f2b609e71cb712f979b01ffc //这个需要我们在应用中用到 -
[xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx] //这个需要我们在应用中用到 -
xxxxx@sina.com -
xxxxx@sina.com -
http://apps.51.com/os_rest_test/ -
http://www.unickway.org.cn/opensocial/php_rest/test/index.php
//这个就是我们应用的地址了,也是我们在上一步填写的回调地址
第三步:
我们去写自己的opensocial restful 应用了:)
首先我们去下载opensocial restful client library,我选择的是php版本,
这里是svn地址:http://opensocial-php-client.googlecode.com/svn/trunk .
然后我们将我们下载的内容部署到我们的library中:)
这是我的部署:
在osapi的目录下有一个providers目录,我们打开这个目录增加一个osapi51Provider.php文件,修改一个osapiProvider.php文件:
osapi51Provider.php 的代码如下:
<code>
class osapi51Provider extends osapiProvider {
public function __construct(osapiHttpProvider $httpProvider = null) {
parent::__construct(
null,
null,
null,
"http://os.51.com/social/rest", // 这里是51的restful的路径地址
"http://os.51.com/social/rpc", // 这里是51的rpc的路径地址
"51", true, $httpProvider);
}
}
</code>
osapiProvider.php 的代码修改如下:就是增加一个require_once,我们刚才写的文件
<code>
require_once "osapi51Provider.php";
</code>
我们将opensocial restful client library 中的example目录作为我们的restful测试应用
在examples文件夹中去修改一个__init__.php的文件代码:
<code>
case 'myspace':
$userId = '439607992';
$osapi = new osapi(new osapiMySpaceProvider(), new osapiOAuth2Legged("http://dev.gain.resource.com", "7ebda6dee096455889bdab23ddacdfae", '439607992'));
在这个下面写下51的请求方法和内容:
case '51':
$userId = 'new_lp';
$osapi = new osapi(new osapi51Provider(), new osapiOAuth2Legged(" 65368138f2b609e71cb712f979b01ffc 【这里是上面的app public key】 ", " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx【 这里是上面的app secret key , 要填写自己应用获得 】 ", $userId));
}
</code>
好了,最后将我们的example目录部署到我们的服务器上,
将里面的index.php填写到51的应用的回调地址的位置,这样就拥有了自己的opensocial restful 应用了:)。
51实现的有一些个别的问题,就是他在验证rest请求的时候,必须找到opensocial_owner_id用来知道是那个用户在请求数据。所以我们在请求个人数据或者朋友数据的时候,要将这个参数加入。当然这个是在我们的rul里面获得的。
$self_request_params = array(
'userId' => $userId, // Person we are fetching.
'groupId' => '@self', // @self for one person.
'fields' => $profile_fields, // Which profile fields to request.
'opensocial_owner_id' => $_REQUEST['opensocial_owner_id'],
);
同理在请求friends的时候也有这个问题
$friends_request_params = array(
'userId' => $userId, // Person whose friends we are fetching.
'groupId' => '@friends', // @friends for the Friends group.
'opensocial_owner_id' => $_REQUEST['opensocial_owner_id'],
'fields' => $profile_fields, // Which profile fields to request.
'count' => $friend_count // Max friends to fetch.
);
$batch->add($osapi->people->get($friends_request_params), 'friends');
// Send the batch request.
$result = $batch->execute();
上面的红色代码是需要注意的地方
粉色代码就是去发送restful请求,请求数据了。
完毕。