btafoya/ohmy-auth

OAuth 简单到不可思议.. 你甚至不会相信它是 OAuth

0.0.8 2015-02-13 09:19 UTC

This package is auto-updated.

Last update: 2024-09-05 09:40:18 UTC


README

ohmy-auth (Oma) 是一个简化 OAuth 的 PHP 库,将其转换为流畅的接口

use ohmy\Auth1;
Auth1::legs(2)
     ->set('key', 'key')
     ->set('secret', 'secret')
     ->request('http://term.ie/oauth/example/request_token.php')
     ->access('http://term.ie/oauth/example/access_token.php')
     ->GET('http://term.ie/oauth/example/echo_api.php')
     ->then(function($data) {
         # got data
     });

依赖关系

Oma 只需要 PHP (>= 5.3) 和通常的扩展库 Curl (curl_init(), curl_setopt(), 等), JSON (json_encode(), json_decode()) 和会话 (session_start(), session_destroy()).

使用 Composer 安装

安装 Oma 最好的方式是通过 Composer。只需将 ohmy/auth 添加到项目的 composer.json 文件中,然后运行 composer install。例如

{
    "require": {
        "btafoya/ohmy-auth": "*"
    }
}

手动安装

如果您不想使用 Composer,可以下载存档或克隆此存储库,并将 src/ohmy 放入您的项目设置中。

双端 OAuth 1.0a

use ohmy\Auth1;

# do 2-legged oauth
$termie = Auth1::legs(2)
               # configuration
               ->set('key', 'key')
               ->set('secret', 'secret')
               # oauth flow
               ->request('http://term.ie/oauth/example/request_token.php')
               ->access('http://term.ie/oauth/example/access_token.php');

# api call
$termie->GET('http://term.ie/oauth/example/echo_api.php')
       ->then(function($data) {
           # got data
       });

三端 OAuth 1.0a

注意:这需要会话来在重定向之间保存数据。没有会话将无法正常工作!

use ohmy\Auth1;

# do 3-legged oauth
$tumblr = Auth1::legs(3)
               # configuration
               ->set(array(
                    'consumer_key'    => 'your_consumer_key',
                    'consumer_secret' => 'your_consumer_secret',
                    'callback'        => 'your_callback_url'
               ))
               # oauth flow
               ->request('http://www.tumblr.com/oauth/request_token')
               ->authorize('http://www.tumblr.com/oauth/authorize')
               ->access('http://www.tumblr.com/oauth/access_token');

# access tumblr api      
$tumblr->GET('https://api.tumblr.com/v2/user/info')
       ->then(function($data) {
           # got user data
       });

三端 OAuth 2.0

use ohmy\Auth2;

# do 3-legged oauth
$github = Auth2::legs(3)
               # configuration
               ->set(array(
                    'id'       => 'your_github_client_id',
                    'secret'   => 'your_github_client_secret',
                    'redirect' => 'your_redirect_uri'
               ))
               # oauth flow
               ->authorize('https://github.com/login/oauth/authorize')
               ->access('https://github.com/login/oauth/access_token')
               ->finally(function($data) use(&$access_token) {
                   $access_token = $data['access_token'];
               });

# access github api
$github->GET("https://api.github.com/user?access_token=$access_token", null, array('User-Agent' => 'ohmy-auth'))
       ->then(function($data) {
           # got user data
       });

更多示例

许可证

  • PHP 许可证: PHP 许可证
  • ohmy-auth: 新 BSD 许可证。