PHP code example of stansoft / oauth2-firefly-iii

1. Go to this page and download the library: Download stansoft/oauth2-firefly-iii library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

stansoft / oauth2-firefly-iii example snippets




tanSoft\OAuth2\Client\Provider\FireflyIII;

session_start();

try {
	$provider = new FireflyIII([
		'fireflyUri' => 'https://REPLACE.WITH.YOUR.FIREFLY.INSTANCE.URL',
		'clientId' => 'REPLACE_WITH_CLIENT_ID',
		'clientSecret' => 'REPLACE_WITH_CLIENT_SECRET',
		'redirectUri' => 'http://REPLACE.WITH.YOUR.WEB.APP.URL'
	]);
} catch (Exception $e) {
	echo $e->getMessage();
	exit;
}


if (!isset($_GET['code'])) {
    // If we don't have an authorization code then get one
    $authUrl = $provider->getAuthorizationUrl();
    $_SESSION['oauth2state'] = $provider->getState();
    header('Location: ' . $authUrl);
    exit;
}

if (isset($_GET['code']) && !isset($_SESSION['token'])) {
    try {
        // Try to get an access token (using the authorization code grant)
        $token = $provider->getAccessToken('authorization_code', [
            'code' => $_GET['code']
        ]);
        $_SESSION['token'] = $token;

    } catch (Exception $e) {
        print($e->getMessage());
        exit;
    }
}
if (isset($_SESSION['token'])) {
	try {
		$user = $provider->getCurrentUser($_SESSION['token']);
		printf('Hello %d!', $user->getId());
	} catch (UnexpectedValueException $e) {
		echo $e->getMessage();
		exit;
	}
    return;
}