hashtages/allegro-api-client-php

3.1.3 2023-04-07 12:53 UTC

README

用于allegro集成的辅助模型和连接器类。

库的部分由 OpenAPI Generator 项目自动生成,使用来自allegro的swagger定义。

https://developer.allegro.pl/swagger.yaml

使用composer安装

composer require coffeedesk/allegro-api-client-php

API文档

本包根据以下规范实现

https://developer.allegro.pl/documentation/

使用方法

OAuth

根据 https://developer.allegro.pl/auth/ 实现OAuth

1 阶段:从回调获取代码

<?php

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

$authenticator = new \AllegroApi\Authentication\Authenticator(
    new \GuzzleHttp\Client(),
    'https://allegro.pl.allegrosandbox.pl',
    'ALLEGRO_CLIENT_ID', // after add this app to allegro sandbox panel you will get this
    'ALLEGRO_CLIENT_SECRET', // after add this app to allegro sandbox panel you will get this
    'http://uri.where.is.sent.callback/authorization_callback' // after authorization callback will be sent on url (you can use serveo.net localy)
);

echo $authenticator->getAuthorizeUrl();

// This shows url to allegro OAuth, you can redirect in your app to this url in your controller.

?>

2 阶段:在授权后从回调获取令牌 - 例如 http://uri.where.is.sent.callback/authorization_callback

在你的回调URL下创建控制器,allegro登录后会发送令牌给你。

$authorizationToken = $_GET['code'];
$tokens = $authenticator->getAuthenticationTokensFromAuthorizationToken($authorizationToken);

3 阶段:在每次请求中使用收到的访问令牌

<?php

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

$configuration = (new \AllegroApi\Configuration())
    ->setHost('https://api.allegro.pl.allegrosandbox.pl')
    ->setAccessToken('access.token.received.from.callback.authorize.url');

$offerManagmentApi = new \AllegroApi\Client\OfferManagementApi(
    new \GuzzleHttp\Client(),
    $configuration
);

$categories = $offerManagmentApi->getCategoriesUsingGET();

var_dump($categories);

?>

刷新令牌

要刷新allegro令牌,你可能需要从你的存储库获取当前令牌


<?php

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

$authenticator = new \AllegroApi\Authentication\Authenticator(
    new \GuzzleHttp\Client(),
    'https://allegro.pl.allegrosandbox.pl',
    'ALLEGRO_CLIENT_ID', // after add this app to allegro sandbox panel you will get this
    'ALLEGRO_CLIENT_SECRET', // after add this app to allegro sandbox panel you will get this
    'http://uri.where.is.sent.callback/authorization_callback' // after authorization callback will be sent on url (you can use serveo.net localy)
);

// your repository 
$tokenData = $allegroTokenRepository->getCurrentToken();

$newTokenData = $authenticator->getNewTokensFromCurrentTokens($tokenData['refresh_token']);

// your repository 
$allegroTokenRepository->allegroTokenRepository->saveToken($newTokenData);

代码提示:使用认证器和AllegroApi类最佳方式是依赖注入。要定义配置(\AllegroApi\Configuration),可以使用工厂模式。

开发

使用openapi-generator生成PHP类

注意!

请查看此存储库中的提交070d13ed2d031c8361597aead4071a78b6317ba6。包openapi-generator总是生成错误的CategoryParameters.php,每次重新生成代码时都需要修复它。

要生成新的客户端和模型,运行

docker run --rm \
-v ${PWD}:/local/project \
-v ${PWD}/src:/local/out/php/lib \
openapitools/openapi-generator-cli generate \
-i /local/project/swagger.yaml \
-g php \
-o /local/out/php \
--invoker-package AllegroApi \
--api-package Client \
--model-package Model