onlime/laravel-bexio-api-client 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

onlime/laravel-bexio-api-client

Composer 安装命令:

composer require onlime/laravel-bexio-api-client

包简介

Client library for Bexio API using Laravel HTTP Client

README 文档

README

The bexio API Client Library enables you to work with the bexio API. This is a wrapper around onlime/bexio-api-client for easier Laravel integration using Laravel HTTP Client. You could use this in combination with my zero-configuration Laravel HTTP Client Global Logger for detailed request/response logging.

See onlime/bexio-api-client README and the official bexio API documentation for more information how to use the API.

Installation

You can use Composer to integrate the library into your Laravel project:

$ composer require onlime/laravel-bexio-api-client

Sample Usage

NOTE: I am just documenting Laravel project integration here. Please consult the onlime/bexio-api-client README for Bexio API Client library documentation.

I recommend to make this thing configurable in config/bexio.php:

<?php
return [
    /*
    |--------------------------------------------------------------------------
    | Bexio API Credentials
    |--------------------------------------------------------------------------
    */
    'api' => [
        'clientId' => env('BEXIO_API_CLIENT_ID', 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'),
        'clientSecret' => env('BEXIO_API_CLIENT_SECRET', 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
        ),
        'tokensFile' => env('BEXIO_API_TOKENS_FILE', 'bexio_client_tokens.json'),
        'scopes' => explode(' ', env('BEXIO_API_SCOPES',
            'openid profile contact_edit offline_access kb_invoice_edit article_edit note_edit'
        )),
    ],
];

Put your Bexio API credentials from Bexio Developer Portal into your .env:

BEXIO_API_CLIENT_ID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
BEXIO_API_CLIENT_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Create some secure storage to store the client (access and refresh) tokens, config/filesystems.php:

    'disks' => [
        // ...
        'secure' => [
            'driver' => 'local',
            'root' => storage_path('app/secure'),
            'visibility' => 'private',
            'throw' => false,
        ],

You could then define these two routes in your routes/web.php:

  • GET /admin/bexio/auth – authenticate against Bexio and generate your access and refresh tokens
  • GET /admin/bexio/demo – demo page to test Bexio API with the previously generated access token
// routes/web.php
<?php
Route::middleware('auth')->group(function () {
    Route::group(['prefix' => 'admin'], function () {
        Route::controller(BexioController::class)->prefix('bexio')->group(function () {
            Route::redirect('/', '/admin/bexio/auth');
            Route::get('/auth', 'authenticate')->name('bexio.auth');
            Route::get('/demo', 'demo')->name('bexio.demo');
        });
    });
});

WARNING: Make sure you protect those rules and only make it available to your admin users!

Now create the controller, php artisan make:controller BexioController:

<?php

namespace App\Http\Controllers;

use Bexio\Resource\Contact as BexioContact;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Inertia\Inertia;
use LaravelBexio\Client;

class BexioController extends Controller
{
    public function authenticate(Request $request)
    {
        $client = $this->getBexioClient();
        $client->authenticate(config('bexio.api.scopes'), route('bexio.auth'));
        $client->persistTokens($this->getTokensFile());

        return view('bexio.auth');
    }

    public function demo(Request $request)
    {
        $client = $this->getBexioClient();
        $client->loadTokens($this->getTokensFile());

        return view('bexio.demo', [
            'contacts' => (new BexioContact($client))->getContacts(),
        ]);
    }

    protected function getBexioClient(): Client
    {
        return new Client(
            config('bexio.api.clientId'),
            config('bexio.api.clientSecret')
        );
    }

    protected function getTokensFile(): string
    {
        return Storage::disk('secure')->path(config('bexio.api.tokensFile'));
    }
}

NOTE: Make sure you add http://localhost:8080/admin/bexio/auth to the redirect URLs in Bexio Developer Portal

Now fire up your application:

$ php artisan serve

Then, access the Bexio authentication page:

This will ask you to confirm the requested scopes and you will need to login with your Bexio credentials in case you haven't done this yet.

Once you have authenticated, you'll see the contents of the bexio.auth view, which could display some confirmation message and ask you to proceed to the demo page. The demo page on http://localhost:8080/admin/bexio/demo will then use the Bexio API client tokens which were stored in <project-root>/storage/app/secure/bexio_client_tokens.json, and display all Bexio contacts (as an example, to verify all is working).

Authors

Author of this awesome package is Philip Iezzi (Onlime GmbH).

License

This package is licenced under the MIT license however support is more than welcome.

统计信息

  • 总下载量: 448
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 1

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-03-24

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固