coffeedesk/allegro-api-client-php

3.1.1 2020-12-07 08:14 UTC

README

用于 Allegro 集成的助手模型和连接类。

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

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