evert / oauth2
本包最新版本(1.2.0)没有提供许可证信息。
OAuth 2.0 协议的轻量级 PHP 封装(基于 OAuth 2.0 授权协议草案-ietf-oauth-v2-15)
1.2.0
2013-07-15 22:28 UTC
Requires
- php: >=5.3.0
This package is not auto-updated.
Last update: 2024-09-09 14:28:05 UTC
README
作者 & 联系方式
Charron Pierrick - pierrick@webstart.fr
Berejeb Anis - anis.berejeb@gmail.com
文档 & 下载
最新版本可在 github 上找到:- https://github.com/adoy/PHP-OAuth2
文档可在:- https://github.com/adoy/PHP-OAuth2 查找
许可证
本代码发布在 GNU LGPL 许可下
请勿更改文件头。
本库是自由软件;您可以在自由软件基金会发布的 GNU 较小通用公共许可证的条款下重新分发和/或修改它;许可证版本为2,或者(根据您的选择)任何更高版本。
本库的目的是希望它是有用的,但没有任何保证;甚至没有适销性或特定用途的隐含保证。
有关详细信息,请参阅 GNU 较小通用公共许可证。
使用说明
<?php include 'vendor/autoload.php'; $clientId = 'your client id'; $clientSecret = 'your client secret'; $redirectUri = 'http://url/of/this.php'; $authorizationEndPoint = 'https://graph.facebook.com/oauth/authorize'; $tokenEndPoint = 'https://graph.facebook.com/oauth/access_token'; $client = new OAuth2\Client($clientId, $clientSecret); if (!isset($_GET['code'])) { $auth_url = $client->getAuthenticationUrl($authorizationEndPoint, $redirectUri); header('Location: ' . $auth_url); die('Redirect'); } else { $params = array('code' => $_GET['code'], 'redirect_uri' => $redirectUri); $response = $client->getAccessToken($tokenEndPoint, 'authorization_code', $params); parse_str($response['result'], $info); $client->setAccessToken($info['access_token']); $response = $client->fetch('https://graph.facebook.com/me'); var_dump($response, $response['result']); }
添加新的授权类型
只需在 OAuth2\GrantType 命名空间中编写一个新类。您可以将类文件放在 GrantType 目录下。以下是一个示例
<?php namespace OAuth2\GrantType; /** * MyCustomGrantType Grant Type */ class MyCustomGrantType implements IGrantType { /** * Defines the Grant Type * * @var string Defaults to 'my_custom_grant_type'. */ const GRANT_TYPE = 'my_custom_grant_type'; /** * Adds a specific Handling of the parameters * * @return array of Specific parameters to be sent. * @param mixed $parameters the parameters array (passed by reference) */ public function validateParameters(&$parameters) { if (!isset($parameters['first_mandatory_parameter'])) { throw new \Exception('The \'first_mandatory_parameter\' parameter must be defined for the Password grant type'); } elseif (!isset($parameters['second_mandatory_parameter'])) { throw new \Exception('The \'seconde_mandatory_parameter\' parameter must be defined for the Password grant type'); } } }
使用您在 GRANT_TYPE 常量中定义的授权类型调用 OAuth 客户端 getAccessToken,如下所示
$response = $client->getAccessToken(TOKEN_ENDPOINT, 'my_custom_grant_type', $params);