bggardner/oauth2-digikey

PHP League OAuth2-Client 的 Digi-Key OAuth 2.0 客户端提供者

dev-master 2023-03-25 15:30 UTC

This package is auto-updated.

Last update: 2024-09-25 19:15:08 UTC


README

本软件包为 PHP League 的 OAuth 2.0 客户端 提供Digi-Key OAuth 2.0 支持,以便在访问 Digi-Key API 解决方案 时使用。

安装

要安装,请使用 composer

composer require bggardner/oauth2-digikey:dev-master

使用方法

使用方法与 The League 的 OAuth 客户端相同,使用 \Bggardner\OAuth2\Client\Provider\DigiKey 作为提供者。

授权码流

$provider = new Bggardner\OAuth2\Client\Provider\DigiKey([
    'clientId'          => '{digikey-client-id}',
    'clientSecret'      => '{digikey-client-secret}',
    'redirectUri'       => 'https://example.com/callback-url',
    'isSandbox'         => true, // Optional, defaults to false. When true, client uses sandbox urls.
    'localeSite'        => 'US', // Optional, defaults to null. Two letter code for Digi-Key product website to search on.
    'localeLanguage'    => 'en', // Optional, defaults to null. Two letter code for language to search on. Language must be supported by the selected site.
    'localeCurrency'    => 'USD', // Optional, defaults to null. Three letter code for Currency to return part pricing for. Currency must be supported by the selected site.
    'customerId'        => '{digikey-customer-id}', // Optional, defaults to null. Your Digi-Key Customer id. If your account has multiple Customer Ids for different regions, this allows you to select one of them.
]);

有关本软件包的进一步使用,请参阅 “授权码许可”的核心软件包文档

刷新令牌

$provider = new Bggardner\OAuth2\Client\Provider\DigiKey([
    'clientId'          => '{digikey-client-id}',
    'clientSecret'      => '{digikey-client-secret}',
    'redirectUri'       => 'https://example.com/callback-url'
]);

$accessToken = getAccessTokenFromYourDataStore();

if ($accessToken->hasExpired()) {
    $accessToken = $provider->getAccessToken('refresh_token', [
        'refresh_token' => $accessToken->getRefreshToken()
    ]);

    // Purge old access token and store new access token to your data store.
}

有关本软件包的进一步使用,请参阅 “刷新令牌”的核心软件包文档

执行 API 调用

以下示例向 产品详细信息 API 发送请求

$request = $provider->getAuthenticatedRequest(
    'GET',
    'https://api.digikey.com/Search/v3/Products/{digikey-part-number}',
    $accessToken
);

# Response returns an associative array
$response = $provider->getParsedResponse($request);

许可证

MIT 许可证 (MIT)。请参阅 许可证文件 获取更多信息。