hannesvdvreken/php-oauth

此包已被废弃,不再维护。作者建议使用hannesvdvreken/php-oauth包。

PHP OAuth 服务

1.1.0 2015-06-03 21:08 UTC

This package is auto-updated.

Last update: 2020-06-17 12:57:55 UTC


README

Build Status Latest Stable Version Total Downloads Coverage Status License

使用方法

让我们直接开始。

设置

$service = new OAuth\Services\Github();

可以通过构造函数传递一些可能的配置,如下所示

$redirectUri = 'https://example.com/oauth/callback';

$credentials = array(
    'client_id'     => 'client-id',
    'client_secret' => '****',
);

$scopes = array('user', 'user:email');

$token = array(
    'access_token' => $accessToken;
);

$service = new OAuth\Services\Github($redirectUri, $credentials, $scopes, $token);

另一种方法是以下这样

$service = new OAuth\Services\Github;
$service
    ->setRedirectUri($redirectUri)
    ->setCredentials($credentials)
    ->setScopes($scopes)
    ->setToken($token);

服务类还有以下获取器

$redirectUri = $service->getRedirectUri();
$credentials = $service->getCredentials();
$scopes      = $service->getScopes();
$token       = $service->getToken();

底层的 GuzzleHttp\Client 可以这样访问

$service->setClient(new GuzzleHttp\Client);
$service->getClient();

请求 API

可以通过在服务类上调用相同的方法来调用内部的 GuzzleHttp\Client

$response = $service->get('users/self')->json();

或者

$body = ['status' => 'Tweeted with @hannesvdvreken/php-oauth'];
$status = $twitter->post('statuses/update', compact('body'))->json();

内部 Guzzle 客户端将使用正确的令牌在头部或 GET 参数中调用。你只需要用正确的凭据或令牌从持久层或会话中加载服务类。

Laravel 4

如果你正在使用 Laravel 4,可以自由注册包含的服务提供者(OAuth\Support\ServiceProvider)。注册 OAuth 类别名以使用以下语法获取完全配置的服务类

$twitter = OAuth::consumer('twitter');

要在 app/config/packages 中创建一个空配置文件,只需使用 artisan 命令

php artisan config:publish hannesvdvreken/php-oauth

OAuth 1.0a

对于 OAuth1.0a 功能,我们内部使用 Guzzle 的 OAuth1 订阅者。一个例子

$twitter = new OAuth\Services\Twitter($redirectUri, $credentials);

// Request token for redirecting the user (store it in session afterwards).
$token = $twitter->requestToken();

// Get the url to which we need to redirect the user.
$url = $twitter->authorizationUrl();

// Redirect the user.
header('Location: '. $url); exit;

简而言之,如果尚未这样做,将调用 authorizationUrl 调用 requestToken 方法

// Get the url to which we need to redirect the user.
$url = $twitter->authorizationUrl();

// Get the requestToken that has been requested.
$token = $twitter->getToken();
// And store it.

// And redirect the user.
header('Location: '. $url); exit;

在回调控制器...

// Give the stored token back to the service class.
$twitter->setToken($token);

// Exchange the get parameters for an access token.
$token = $twitter->accessToken($oauthToken, $oauthVerifier);

// Do a get request, just like you would do with a Guzzle Client.
$profile = $twitter->get('account/verify_credentials.json')->json();

OAuth 2

OAuth2 流程更简单。

$fb = new OAuth\Services\Facebook();

$url = $fb->authorizationUrl();

header('Location: '. $url);

在回调控制器...

$fb->accessToken($code);

$profile = $fb->get('me')->json();

支持的服务

  • Campaign Monitor
  • Dropbox
  • Facebook
  • Foursquare
  • GitHub
  • Google
  • Instagram
  • MailChimp
  • Twitter (OAuth1.0a)
  • Stack Exchange

Guzzle v3

如果你想继续使用此库的老版本,这些版本利用了 Guzzle v3(使用 Guzzle\Http\Client 而不是 GuzzleHttp\Client),那么你可能需要安装 0.1.* 版本。带有 Guzzle v3 兼容性的拉取请求应该针对 guzzle3 分支 提交。使用 0.1.*dev-guzzle3 安装最新版本的 Guzzle v3。

贡献

请随意提交拉取请求。一个新服务类可能只需要 22 行代码。请尽可能遵守 PSR-2。如果你放错了括号,那也没关系!

测试

安装依赖项(composer install)后,您只需运行 phpunit 即可运行整个测试套件。

许可证

MIT