jurapapuniysky/fork-allegro-api-client-php

dev-master 2021-12-08 12:39 UTC

This package is auto-updated.

Last update: 2024-09-08 18:34:29 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);

代码注意事项:最佳实践是使用依赖注入使用Authenticator和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