kompas/oauth2-client

OAuth 2.0 客户端库

0.3 2014-01-28 12:13 UTC

This package is not auto-updated.

Last update: 2024-09-24 01:46:18 UTC


README

Build Status Total Downloads Latest Stable Version

此库使得将您的应用程序与 OAuth 2.0 身份提供者集成变得异常简单。它内置了对以下服务的支持:

  • Facebook
  • Github
  • Google
  • Instagram
  • LinkedIn
  • Microsoft
  • Vkontakte
  • Kompas

添加对其他提供者的支持非常简单。

此库需要 PHP 5.3+ 并兼容 PSR-0。

首先,您必须在项目中安装 composer

在项目根目录中创建一个 composer.json 文件

{
    "require": {
        "kompas/oauth2-client": "0.3"
    }
}

将此行添加到应用程序的 index.php 文件中

require 'vendor/autoload.php';

Kompas 使用示例

// composer autoload
require_once "vendor/autoload.php";

$provider = new League\OAuth2\Client\Provider\Kompas(array(
    'clientId'  =>  'XXXXXXXX',
    'clientSecret'  =>  'XXXXXXXX',
    'redirectUri'   =>  ''
));

try {

    // Try to get an access token (using the client credentials grant)
    $t = $provider->getAccessToken('client_credentials');

    try {

        $provider->setFilterBySite('nasional,megapolitan');
        $latest = $provider->getRssLatest($t);
        $response['latestFiltered'] = json_decode($latest, true); // result filtered
        $mostcommented = $provider->getRssMostCommented($t);
        $response['mostCommentedFiltered'] = json_decode($mostcommented, true); // result filtered
        $provider->setFilterBySite(); // reset filtered
        $mostpopular = $provider->getRssMostPopular($t);
        $response['mostPopularNonFiltered'] = json_decode($mostpopular, true); // result not filtered

    } catch (Exception $e) {

        // Failed to get Rss
        $response = array(
            'status' => false,
            'error' => $e->getMessage()
        );
    }

} catch (Exception $e) {

    // Failed to get access token
    $response = array(
        'status' => false,
        'error' => $e->getMessage()
    );
}

header("Content-Type: application/json");
echo json_encode($response);

可用功能

getRssLatest(token, service, siteno, sectionid) getRssMostCommented(token, service, siteno, sectionid) getRssMostPopular(token, service, siteno, sectionid) setFilterBySite(sites) *仅支持 json 格式

示例

$all_latest = $provider->getRssLatest(AccessToken);
$provider->setFilterBySite('nasional,megapolitan'); // (,) delimiter
$filter_latest = $provider->getRssLatest(AccessToken);
$provider->setFilterBySite(); // reset filter
$news_latest = $provider->getRssLatest(AccessToken, 'kompascom', 1, 1);

Kompas API 参考

授权

请求体

  • HTTP 请求 POST http://apis.kompas.com/oauth2/token

  • 参数 需要请求体参数

    • client_id [您的已注册客户端 ID]
    • client_secret [您的已注册客户端密钥]
    • grant_type [必须值为 "client_credentials"]
  • 示例

    POST /oauth2/token HTTP/1.1
    Host: apis.kompas.com
    Cache-Control: no-cache
    Content-Type: application/x-www-form-urlencoded
    
    client_id=xxx&client_secret=xxx&grant_type=client_credentials
  • 响应

{
    "access_token": "xxx",
    "token_type": "bearer",
    "expires": 1387445831,
    "expires_in": 3600
}

Kompascom: 最新

需要授权

请求体

  • HTTP 请求 GET http://apis.kompas.com/rss/kompascom/latest

  • 参数 需要查询参数

    • access_token [必须值为授权响应中的 access token]

    可选路径参数

    • siteId [整数]
    • sectionId [整数]

    可选查询参数

    • filterBySite [字符串,用逗号分隔。例如:nasional,megapolitan]
  • 示例:所有最新内容

    GET /rss/kompascom/latest?access_token=xxx HTTP/1.1
    Host: apis.kompas.com
    Cache-Control: no-cache
    Content-Type: application/x-www-form-urlencoded
  • 示例:带有过滤网站的最新内容

    GET /rss/kompascom/latest?access_token=xxx&filterBySite=nasional,megapolitan HTTP/1.1
    Host: apis.kompas.com
    Cache-Control: no-cache
    Content-Type: application/x-www-form-urlencoded
  • 示例:特定网站的最新内容

    GET /rss/kompascom/latest/1?access_token=xxx HTTP/1.1
    Host: apis.kompas.com
    Cache-Control: no-cache
    Content-Type: application/x-www-form-urlencoded
  • 示例:特定版块的特定网站最新内容

    GET /rss/kompascom/latest/1/1?access_token=xxx HTTP/1.1
    Host: apis.kompas.com
    Cache-Control: no-cache
    Content-Type: application/x-www-form-urlencoded
  • 响应

[
    {
        uid: "2013.12.13.0711189",
        channel: {
            site: "bola",
            section: ""
        },
        title: "Awal Januari, Trofi Piala Dunia Tiba di Indonesia",
        description: "Coca-Cola sebagai official sponsor of the FIFA World Cup™ ...",
        media: {
            image: {
                thumb: "http://assets.kompas.com/data/photo/2013/12/13/1458220455320155t.jpg",
                content: "http://assets.kompas.com/data/photo/2013/12/13/1458220455320155780x390.jpg"
            }
        },
        url: {
            permalink: "http://bola.kompas.com/read/2013/12/13/0711189/Awal.Januari.Trofi.Piala.Dunia.Tiba.di.Indonesia"
        },
        service: "kompascom",
        published_date: "2013-12-13 07:11:18"
    },
    ...
]

Kompascom: 最受评论

需要授权

请求体

  • HTTP 请求 GET http://apis.kompas.com/rss/kompascom/mostcommented

  • 参数 需要查询参数

    • access_token [必须值为授权响应中的 access token]

    可选路径参数

    • siteId [整数]
    • sectionId [整数]

    可选查询参数

    • filterBySite [字符串,用逗号分隔。例如:nasional,megapolitan]
  • 示例:所有最受评论内容

    GET /rss/kompascom/mostcommented?access_token=xxx HTTP/1.1
    Host: apis.kompas.com
    Cache-Control: no-cache
    Content-Type: application/x-www-form-urlencoded
  • 示例:带有过滤网站的最受评论内容

    GET /rss/kompascom/mostcommented?access_token=xxx&filterBySite=nasional,megapolitan HTTP/1.1
    Host: apis.kompas.com
    Cache-Control: no-cache
    Content-Type: application/x-www-form-urlencoded
  • 示例:特定网站的最受评论内容

    GET /rss/kompascom/mostcommented/1?access_token=xxx HTTP/1.1
    Host: apis.kompas.com
    Cache-Control: no-cache
    Content-Type: application/x-www-form-urlencoded
  • 示例:特定版块的特定网站最受评论内容

    GET /rss/kompascom/mostcommented/1/1?access_token=xxx HTTP/1.1
    Host: apis.kompas.com
    Cache-Control: no-cache
    Content-Type: application/x-www-form-urlencoded
  • 响应

[
    {
        uid: "2013.12.13.0711189",
        channel: {
            site: "bola",
            section: ""
        },
        title: "Awal Januari, Trofi Piala Dunia Tiba di Indonesia",
        description: "Coca-Cola sebagai official sponsor of the FIFA World Cup™ ...",
        media: {
            image: {
                thumb: "http://assets.kompas.com/data/photo/2013/12/13/1458220455320155t.jpg",
                content: "http://assets.kompas.com/data/photo/2013/12/13/1458220455320155780x390.jpg"
            }
        },
        url: {
            permalink: "http://bola.kompas.com/read/2013/12/13/0711189/Awal.Januari.Trofi.Piala.Dunia.Tiba.di.Indonesia"
        },
        service: "kompascom",
        published_date: "2013-12-13 07:11:18",
        statistics: {
            comment_count: 279
        }
    },
    ...
]

Kompascom: 最受欢迎

需要授权

请求体

  • HTTP 请求 GET http://apis.kompas.com/rss/kompascom/mostpopular

  • 参数 需要查询参数

    • access_token [必须值为授权响应中的 access token]

    可选路径参数

    • siteId [整数]
    • sectionId [整数]

    可选查询参数

    • filterBySite [字符串,用逗号分隔。例如:nasional,megapolitan]
  • 示例:所有最受欢迎内容

    GET /rss/kompascom/mostpopular?access_token=xxx HTTP/1.1
    Host: apis.kompas.com
    Cache-Control: no-cache
    Content-Type: application/x-www-form-urlencoded
  • 示例:带有过滤网站的最受欢迎内容

    GET /rss/kompascom/mostpopular?access_token=xxx&filterBySite=nasional,megapolitan HTTP/1.1
    Host: apis.kompas.com
    Cache-Control: no-cache
    Content-Type: application/x-www-form-urlencoded
  • 示例:特定网站的最受欢迎内容

    GET /rss/kompascom/mostpopular/1?access_token=xxx HTTP/1.1
    Host: apis.kompas.com
    Cache-Control: no-cache
    Content-Type: application/x-www-form-urlencoded
  • 示例:特定版块的特定网站最受欢迎内容

    GET /rss/kompascom/mostpopular/1/1?access_token=xxx HTTP/1.1
    Host: apis.kompas.com
    Cache-Control: no-cache
    Content-Type: application/x-www-form-urlencoded
  • 响应

[
    {
        uid: "2013.12.13.0711189",
        channel: {
            site: "bola",
            section: ""
        },
        title: "Awal Januari, Trofi Piala Dunia Tiba di Indonesia",
        description: "Coca-Cola sebagai official sponsor of the FIFA World Cup™ ...",
        media: {
            image: {
                thumb: "http://assets.kompas.com/data/photo/2013/12/13/1458220455320155t.jpg",
                content: "http://assets.kompas.com/data/photo/2013/12/13/1458220455320155780x390.jpg"
            }
        },
        url: {
            permalink: "http://bola.kompas.com/read/2013/12/13/0711189/Awal.Januari.Trofi.Piala.Dunia.Tiba.di.Indonesia"
        },
        service: "kompascom",
        published_date: "2013-12-13 07:11:18",
        statistics: {
            read_count: 279
        }
    },
    ...
]

许可证

MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件