2017 © Pedro Peláez
 

library zen-oauth2

一个更好的OAuth2访问类,同时支持微博、豆瓣、Google等社交网站

image

shen2/zen-oauth2

一个更好的OAuth2访问类,同时支持微博、豆瓣、Google等社交网站

  • Sunday, February 15, 2015
  • by shen2
  • Repository
  • 3 Watchers
  • 3 Stars
  • 100 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 4 Forks
  • 1 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

ZenOAuth2

通过一个统一风格的OAuth2访问库,统一各大社交网站接口。, (*1)

安装方法

ZenOAuth2需要php5.3+,唯一的依赖是curl extension。, (*2)

ZenOAuth2遵循PSR-4规范。只需在composer.json中添加依赖:, (*3)

    ...
    "require": {
        ...
        "duoshuo/php-cassandra": "dev-master"
    }
    ...

然后, (*4)

$ composer install

OAuth2类和授权流程

OAuht2类是包含了生成授权地址,获取token的方法。, (*5)

  • 跳转到社交网站的授权页
$config = array(
    'akey' => 'app key', 
    'skey' => 'secret key', 
    'scope' => 'email,friendships_groups_read',
);
$oauth = new ZenOAuth2\WeiboOAuth2($config['akey'], $config['skey']);  //初始化oauth
$params = array(
    'client_id' => $config['akey'],
    'redirect_uri'  => 'callback',//设置回调
    'response_type' => 'code',
    'state'     => 'made by md5 avoid crsf',
    'display'   => null,
    'scope'     => $config['scope'],
    'forcelogin'    => 0, //是否使用已登陆微博账号
);

$authorizeUrl = $oauth->authorizeURL() . "?" . http_build_query($params);
header('Location : ' . $authorizeUrl);
  • 获取access_token
//微博返回的code
$keys = array(
    'code'  => $_REQUEST['code'],
    'redirect_uri'=> '{{redirec_uri}}',
);

//获取token
$token = $oauth->getAccessToken('code', $keys);

Client类和API访问方法

  • 首先用之前获得的access_token实例化一个Client对象
  • 然后就可以通过调用RESTful的方法,访问各种数据接口,如get()、post()、delete()
//根据上一步的acces_token实例化Client对象
$client = new ZenOAuth2\WeiboClient($token['access_token']);

//根据uid获取用户信息
$info = $client->get('users/show', array('uid'=>1739476392)); 

//删除一条微博
$data = $client->post('comments/destory', array('uid'=>1739476392, 'cid' => 'weiboid'));

目前支持的社交网站

  • 微博
  • QQ
  • 腾讯微博
  • 人人
  • 百度
  • 搜狐
  • 豆瓣
  • 开心
  • Google
  • Instagram
  • Github

The Versions

15/02 2015

dev-master

9999999-dev

一个更好的OAuth2访问类,同时支持微博、豆瓣、Google等社交网站

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Shen Zhenyu

04/09 2014

v0.1.1

0.1.1.0

一个更好的OAuth2访问类,同时支持微博、豆瓣、Google等社交网站

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Shen Zhenyu