estebanmatias92/ohmy-auth

OAuth如此简单.. 你甚至不会相信它是OAuth

0.0.8 2014-04-22 15:28 UTC

This package is auto-updated.

Last update: 2024-09-03 12:23:10 UTC


README

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

use ohmy\Auth;

$credentials = array(
    'key'    => 'key',
    'secret' => 'secret'
);

Auth::init($credentials)
    ->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": {
        "ohmy/auth": "*"
    }
}

手动安装

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

双因素OAuth 1.0a

use ohmy\Auth;

# configuration
$credentials = array(
    'key'    => 'key',
    'secret' => 'secret'
);

# do 2-legged oauth
$termie = Auth::init($credentials)
              # 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\Auth;

# configuration
$credentials = array(
    'consumer_key'    => 'your_consumer_key',
    'consumer_secret' => 'your_consumer_secret',
    'callback'        => 'your_callback_url'
);

# do 3-legged oauth
$tumblr = Auth::init($credentials)
               # 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\Auth;

# configuration
$credentials = array(
    'id'       => 'your_github_client_id',
    'secret'   => 'your_github_client_secret',
    'redirect' => 'your_redirect_uri'
);

# do 3-legged oauth
$github = Auth::init($credentials)
              # oauth flow
              ->authorize('https://github.com/login/oauth/authorize')
              ->access('https://github.com/login/oauth/access_token')
              # save 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许可证。