wittestier / oauth2-meetup
Meetup.com OAuth 2.0 客户端
v1.0.0
2018-02-12 21:19 UTC
Requires
- php: >=7.1
- league/oauth2-client: ^2.3
Requires (Dev)
- phpunit/phpunit: ^7.0
- squizlabs/php_codesniffer: ^3.2
This package is auto-updated.
Last update: 2024-09-14 02:48:15 UTC
README
此包为PHP League的OAuth 2.0 客户端提供Meetup.com OAuth 2.0 支持。
要求
以下版本的PHP受到支持。
- PHP 7.1
- PHP 7.2
安装
要安装,请使用composer
composer require wittestier/oauth2-meetup
用法
授权码流
$provider = new \WitteStier\OAuth2\Client\Provider\Meetup([
'clientId' => '{meetup-consumer-key}',
'clientSecret' => '{meetup-consumer-secret}',
'redirectUri' => '{meetup-consumer-redirect-uri}',
]);
// If we don't have an authorization code then get one.
if (isset($_GET['code']) === false) {
// Fetch the authorization URL from the provider; this returns the urlAuthorize option and generates and applies
// any necessary parameters (e.g. state).
$authorizationUrl = $provider->getAuthorizationUrl();
// Get the state generated for you and store it to the session.
$_SESSION['oauth2state'] = $provider->getState();
// Redirect the user to the authorization URL.
header('Location: ' . $authorizationUrl);
exit;
// Check given state against previously stored one to mitigate CSRF attack.
} elseif (empty($_GET['state']) === true || ($_GET['state'] !== $_SESSION['oauth2state'])) {
unset($_SESSION['oauth2state']);
exit('Invalid state');
} else {
try {
// Try to get an access token using the authorization code grant.
$grant = new League\OAuth2\Client\Grant\AuthorizationCode();
$accessToken = $provider->getAccessToken($grant, [
'code' => $_GET['code']
]);
} catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) {
// Failed to get the access token or user details.
exit($e->getMessage());
}
// We have an access token, which we may use in authenticated requests against the service provider's API.
echo $accessToken->getToken() . "\n";
echo $accessToken->getRefreshToken() . "\n";
echo $accessToken->getExpires() . "\n";
echo ($accessToken->hasExpired() ? 'expired' : 'not expired') . "\n";
try {
// Using the access token, we may look up details about the resource owner.
$resourceOwner = $provider->getResourceOwner($accessToken);
var_export($resourceOwner->toArray());
} catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) {
// Failed to get the access token or user details.
exit($e->getMessage());
}
}
管理作用域
在创建Meetup授权URL时,您可以指定应用程序可能授权的状态和作用域。
$options = [
'state' => 'OPTIONAL_CUSTOM_CONFIGURED_STATE',
'scope' => [ 'ageless', 'basic', 'event_management', 'group_edit', 'group_content_edit', 'group_join', 'messaging', 'profile_edit', 'reporting', 'rsvp' ],
];
$authorizationUrl = $provider->getAuthorizationUrl($options);
如果两者均未定义,则提供者将使用内部默认值。
在编写此文档时,以下作用域可用。(链接至作用域信息)
刷新令牌
$provider = new \WitteStier\OAuth2\Client\Provider\Meetup([
'clientId' => '{meetup-consumer-key}',
'clientSecret' => '{meetup-consumer-secret}',
'redirectUri' => '{meetup-consumer-redirect-uri}',
]);
// Fetch your token from a datastore.
$token = fetchAccessToken();
$refreshToken = $token->getRefreshToken();
if ($token->hasExpired() === true) {
$grant = new League\OAuth2\Client\Grant\RefreshToken();
$newAccessToken = $provider->getAccessToken($grant, [
'refresh_token' => $token->getRefreshToken()
]);
// Purge old access token and store new access token to your data store.
}
测试
$ ./vendor/bin/phpunit
# Or
$ composer test
$ ./vendor/bin/phpcs src --standard=psr2 -sp
# Or
$ composer check
贡献
请参阅贡献指南以获取详细信息。
致谢
许可证
MIT许可证(MIT)。请参阅许可证文件以获取更多信息。