想在sae中做一个豆瓣我说功能的同步..
后来发现sae只提供了sina微博很友好的OAuth接口.
而对于其他的OAuth服务.我们不得不来用非常简陋的SaeFetchurl来实现.
再我参考了他的一些源码后.下面的代码已经完成了OAuth验证.测试正常.
注:下面的代码我只提供了使用API的时候授权的方法.你要问我怎么授权.有了这些还不够吗?
| 以下是代码片段: class Douban { public static function saying($data) { global $PunnyConfig; $o = new OAuthClient(); $o->setParam(’oauth_consumer_key’, $PunnyConfig[’douban’][’oauth_consumer_key’]); //set your consumer key $o->setParam(’oauth_token’, $PunnyConfig[’douban’][’oauth_token’]); $o->setParam(’http_method’, ’POST’); //POST $o->setUrl(’http://api.douban.com/miniblog/saying’); //broadcast api url. see http://www.douban.com/service/apidoc/reference/miniblog#添加广播 $o->signature($PunnyConfig[’douban’][’oauth_consumer_secret’], $PunnyConfig[’douban’][’oauth_token_secret’]); //your consumer secret and access token secret ; don’t forget signature. $f = new SaeFetchurl(); $f->setMethod(’post’); $data = ’<?xml version=\’1.0\’ encoding=\’UTF-8\’?>’. ’<entry xmlns:ns0="http://www.w3.org/2005/Atom" ’. ’xmlns:db="http://www.douban.com/xmlns/">’. ’<content>’ . $data . ’</content>’. //content ’</entry>’; $f->setPostData( $data ); $f->setHeader( ’Content-Type’, ’application/atom+xml’ ); $f->setHeader( ’Authorization’, $o->getOAuthHeaders(’YourSecret’) ); return $f->fetch(’http://api.douban.com/miniblog/saying’); } } |