Helper model and connector classes for allegro integration.
Parts of the library is automatically generated by the OpenAPI Generator project using swagger definition from allegro:
https://developer.allegro.pl/swagger.yaml
composer require coffeedesk/allegro-api-client-php
This package was implemented according to:
https://developer.allegro.pl/documentation/
Implementation OAuth according to https://developer.allegro.pl/auth/
<?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 Stage: Get tokens from callback after authorization - e.g. http://uri.where.is.sent.callback/authorization_callback
Make controller under your callback url, allegro will send you tokens after login.
$authorizationToken = $_GET['code'];
$tokens = $authenticator->getAuthenticationTokensFromAuthorizationToken($authorizationToken);
<?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);
?>
To refresh allegro token you might need get current token from your repository
<?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);
Code Notice: Best way to use authenticator and AllegroApi classes is Dependency Injection. To define configuration(\AllegroApi\Configuration) you can use factory pattern.
To generate new clients and models run:
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
For the moment swagger.yaml is taken from allegro:
https://developer.allegro.pl/swagger.yaml
but we met issues with discriminator for definition CategoryParameterList when we generate classes using OpenAPI Generator.
Therefore we replaced original allegro definition for CategoryParameterList:
CategoryParameterList:
type: object
properties:
parameters:
type: array
items:
discriminator:
propertyName: type
mapping:
INTEGER: '#/components/schemas/IntegerCategoryParameter'
FLOAT: '#/components/schemas/FloatCategoryParameter'
STRING: '#/components/schemas/StringCategoryParameter'
DICTIONARY: '#/components/schemas/DictionaryCategoryParameter'
oneOf:
- $ref: '#/components/schemas/IntegerCategoryParameter'
- $ref: '#/components/schemas/FloatCategoryParameter'
- $ref: '#/components/schemas/StringCategoryParameter'
- $ref: '#/components/schemas/DictionaryCategoryParameter'
and CategoryParameter:
CategoryParameter:
properties:
id:
type: string
name:
type: string
type:
type: string
example: 'INTEGER, FLOAT, STRING, DICTIONARY'
required:
type: boolean
unit:
type: string
options:
$ref: '#/components/schemas/CategoryParameterOptions'
In the future we can try to use this fork to generate schema without our changes in swagger.yml
https://github.com/netfarma/openapi-generator/commit/8ac80203ec557a7198e48adc66e9c1961c4cd6ce