outlandishideas / oauth2
基于 OAuth 2.0 协议的轻量级 PHP 封装(基于 OAuth 2.0 授权协议草案-ietf-oauth-v2-15)
1.6.0
2023-01-31 12:25 UTC
Requires
- php: >=5.3.0
This package is auto-updated.
Last update: 2024-08-29 06:13:07 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。