stansoft/oauth2-firefly-iii

PHP League OAuth2-Client 的 Firefly-III OAuth 2.0 客户端提供者

v1.3.1 2021-01-04 05:31 UTC

This package is auto-updated.

Last update: 2024-09-04 14:06:24 UTC


README

Build Status

提供 Firefly-III OAuth 2.0 支持,作为 PHP League 的 OAuth 2.0 客户端 的实现

覆盖的 API

  • 关于
  • 账户
  • 附件
  • 自动完成
  • 可用预算
  • 账单
  • 预算
  • 图表
  • 类别
  • 配置
  • 货币
  • 导入
  • 链接
  • 猪猪储蓄罐
  • 偏好
  • 重复
  • 规则
  • 规则组
  • 搜索
  • 摘要
  • 标签
  • 交易
  • 用户
  • 数据

安装

安装时,使用 composer

composer require stansoft/oauth2-firefly-iii

用法

用法与 League 的 OAuth 客户端相同,使用 \StanSoft\OAuth2\Client\Provider\FireflyIII 作为提供者。

授权码流

<?php

require_once('./vendor/autoload.php');

use StanSoft\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;
}

测试

$ ./vendor/bin/phpunit

致谢