如何使用 Laravel 的服務提供者進行外部 API 使用(利用基於使用者的憑證)
P粉465675962
P粉465675962 2023-12-13 21:03:48
0
1
371

所以我正在開發一個 Laravel 管理應用程序,它使用外部 API,我們稱之為 PlatformAPI。平台的工作方式是,我的應用程式的用戶在平台上有一個帳戶。我的 Laravel 應用程式將充當管理儀表板,因此使用者可以查看一些從 PlatformAPI 取得的基本報告。

我的應用程式中的每個用戶都必須添加他們的客戶端 ID 和客戶端密鑰,他們可以在平台中創建這些內容。這樣,我的應用程式將能夠使用使用者的憑證代表他們向 PlatformAPI 執行請求。

我讀過一些文章和教程,基本上介紹如何為服務設定憑證/令牌並將該服務綁定到 ServiceProvider,如下所示:

<?php

namespace AppServicesPlatformAPI;

class Client
{
    protected string $clientId;
    protected string $clientSecret;

    public function __construct(string $clientId, string $clientSecret)
    {
        $this->clientId = $clientId;
        $this->clientSecret = $clientSecret;
    }

    public function getSales(string $month)
    {
        // ...
    }
}


<?php

use AppServicesPlatformApiClient;

class PlatformApiServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->singleton(Client::class, function ($app) {
            return new Client(
                clientId: config('services.platform-api.client-id'),
                clientSecret: config('services.platform-api.client-secret'),
            );
        });
    }
}

這樣,您就不必每次想要使用 PlatformApi 時都設定客戶端憑證,我可以像這樣呼叫該服務:

<?php

namespace AppHttpControllers;

use AppServicesPlatformApiClient;

class RandomController extends Controller
{

    protected Client $client;    

    public function __construct(Client $client)
    {
        $this->client = $client;
    }

    public function index()
    {
        $this->client->getSales(string $month);
    }
}

但是,由於我需要代表我的應用程式的使用者使用他們提供的憑證(並儲存在我的應用程式的資料庫中)向PlatformApi 執行請求,所以我不確定這種相同的方法是否有效,因為單例只能實例一次?

此外,為了使用 PlatformApi,我需要使用使用者的憑證來取得存取權杖。該存取權杖也需要儲存在某個地方(我想在快取中)。

我有點不知道該如何解決這個問題。任何指示將不勝感激。

P粉465675962
P粉465675962

全部回覆(1)
P粉523625080

我假設您的所有應用程式都將使用此客戶端服務。如果是這樣,您可以繼續使用單例設計模式(以停止進一步的 oauth 請求),但請嘗試將邏輯與提供者的 register 方法分開。您可以在呼叫傳回有效access_token 的私有方法後實例化Client 類別(如果存在有效令牌,則檢查DB / Cache expires_in 時間戳值並傳回它,或向使用者要求一個新的client/secret 並傳回它)

/**
     * Register any application services.
     *
     * @return void
     */
    public function register(): void
    {
        $this->app->singleton(Client::class, function () {
            return new Client(
                accessToken: $this->getUserToken()->access_token
            );
        });
    }

    /**
     * Tries to get the user token from the database.
     *
     * @return ClientCredentials
     */
    private function getUserToken(): ClientCredentials
    {
        $credentials = ClientCredentials::query()
            ->latest()
            ->find(id: auth()->user()->id);

        if ($credentials === null || now() > $credentials->expires_at) {
            $credentials = $this->requestUserToken();
        }

        return $credentials;
    }

    /**
     * Requests a new token for the user & stores it in the database.
     *
     * @return ClientCredentials
     */
    private function requestUserToken(): ClientCredentials
    {
        $tokenResponse = API::requestToken(
            client: auth()->user()->client,
            secret: auth()->user()->secret,
        );
        
        return ClientCredentials::query()->create(
            attributes: [
                'user_id' => auth()->user()->id,
                'access_token' => $tokenResponse['access_token'],
                'refresh_token' => $tokenResponse['refresh_token'],
                'token_type' => 'Bearer',
                'expires_at' => new DateTime(datetime: '+' . $tokenResponse['expires_in'] . ' seconds')
            ],
        );
    }
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!