janyksteenbeek / mollie-connect-oauth2
为PHP League的OAuth2-Client提供的Mollie Connect OAuth 2.0客户端提供者
v1.0
2019-03-25 20:47 UTC
Requires
- php: >=5.6.0
- league/oauth2-client: ^2.0
README
本软件包为PHP League的OAuth 2.0客户端提供Mollie Connect OAuth 2.0支持。
安装
安装时请使用Composer
composer require janyksteenbeek/mollie-connect-oauth2
为了使用Mollie Connect API,您需要一个Mollie账户 (推荐链接)以及一个已注册的应用。
使用方法
使用方法与The League的OAuth客户端相同,使用JanykSteenbeek\MollieConnect\OAuth2\Client\Provider\MollieConnect
作为提供者。
授权码流程
<?php session_start(); $provider = new \JanykSteenbeek\MollieConnect\OAuth2\Client\Provider([ 'clientId' => '{mollie-connect-client-id}', 'clientSecret' => '{mollie-connect-client-secret}', 'redirectUri' => 'https://example.com/callback-url', ]); if (!isset($_GET['code'])) { // If we don't have an authorization code, we will fetch one from Mollie $authUrl = $provider->getAuthorizationUrl(); $_SESSION['oauth2_state'] = $provider->getState(); header('Location: ' . $authUrl); exit; // Check given state against previously stored one to mitigate CSRF attacks. } elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2_state'])) { unset($_SESSION['oauth2_state']); exit('Invalid state'); } else { // Try to get an access token (using the authorization code grant) $token = $provider->getAccessToken('authorization_code', [ 'code' => $_GET['code'] ]); // Optional: Now you have a token you can look up a organization's profile data try { // We got an access token, let's now get the organization's details $account = $provider->getResourceOwner($token); // Use these details to create a new profile printf('Welcome %s!', $account->getName()); } catch (Exception $e) { // Failed to get user details exit('Something went wrong fetching your organization details.'); } // Use this to interact with an API on the organization's behalf $mollieToken = $token->getToken(); }
作用域
您可以在此处找到可用作用域的列表。
鸣谢
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。