adoy / oauth2
OAuth 2.0 协议的轻量级 PHP 封装(基于 OAuth 2.0 授权协议草案 draft-ietf-oauth-v2-15)
1.3.1
2023-03-10 14:58 UTC
Requires
- php: >=5.3.0
This package is auto-updated.
Last update: 2024-09-10 18:39:04 UTC
README
我如何使用它?
<?php require('Client.php'); require('GrantType/IGrantType.php'); require('GrantType/AuthorizationCode.php'); const CLIENT_ID = 'your client id'; const CLIENT_SECRET = 'your client secret'; const REDIRECT_URI = 'http://url/of/this.php'; const AUTHORIZATION_ENDPOINT = 'https://graph.facebook.com/oauth/authorize'; const TOKEN_ENDPOINT = 'https://graph.facebook.com/oauth/access_token'; $client = new OAuth2\Client(CLIENT_ID, CLIENT_SECRET); if (!isset($_GET['code'])) { $auth_url = $client->getAuthenticationUrl(AUTHORIZATION_ENDPOINT, REDIRECT_URI); header('Location: ' . $auth_url); die('Redirect'); } else { $params = array('code' => $_GET['code'], 'redirect_uri' => REDIRECT_URI); $response = $client->getAccessToken(TOKEN_ENDPOINT, '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'); } } }
调用 OAuth 客户端 getAccessToken 方法,使用您在 GRANT_TYPE 常量中定义的授权类型,如下所示
$response = $client->getAccessToken(TOKEN_ENDPOINT, 'my_custom_grant_type', $params);
许可证
此代码在 GNU LGPL 下发布
请勿更改文件的头信息。
此库是自由软件;您可以在自由软件基金会发布的 GNU Lesser General Public License 的条款下重新分发和/或修改它;许可证版本 2 或(根据您的选择)任何更高版本。
此库是在希望它有用的希望下分发的,但没有任何保证;甚至没有关于其可销售性或特定用途适用性的暗示保证。
有关更多详细信息,请参阅 GNU Lesser General Public License。