Search by

adachsoft / social-post-pinterest

Arkadiusz Adach

Pinterest API v5 adapter for provider-independent social post publishing and board management.

Package info

gitlab.com/a.adach/social-post-pinterest

Issues

pkg:composer/adachsoft/social-post-pinterest

Statistics

Installs: 5

Dependents: 1

Suggesters: 0

Stars: 0

v0.1.0 2026-11-26 22:06 UTC

This package is auto-updated.

Last update: 2026-11-26 21:09:49 UTC


README

A Pinterest API v5 adapter for the adachsoft/social-post-contracts publishing interfaces. It publishes image pins, manages Pinterest boards, advertises supported publishing capabilities, and stores OAuth tokens securely with automatic refresh.

Requirements

  • PHP 8.3 or later
  • Composer
  • A PSR-18 HTTP client and PSR-17 request and stream factories; the examples use Guzzle

Installation

Requires PHP 8.3 or later. Install the adapter and Guzzle, which is used by the authorization CLI and the examples below:

composer require adachsoft/social-post-pinterest guzzlehttp/guzzle

The adapter accepts PSR-18 HTTP clients and PSR-17 request and stream factories, so Guzzle may be replaced by another compatible implementation.

Pinterest developer setup

  1. Use a Pinterest business account and create an app at https://developers.pinterest.com/apps/.
  2. Register this redirect URI exactly, including the trailing slash: http://localhost:8085/.
  3. Grant the app these OAuth scopes:
    • boards:read
    • boards:write
    • pins:read
    • pins:write
    • user_accounts:read
  4. Check the app's access level. Trial apps must use Pinterest's sandbox environment (https://api-sandbox.pinterest.com/v5) for write requests. Use --sandbox with the authorization CLI and PinterestEnvironmentEnum::SANDBOX in the publisher configuration. Standard-access apps can use the production environment.

One-time authorization

Set the app credentials in the environment and authorize once. The CLI starts a local callback listener on port 8085 by default; use --port to select another port. The registered redirect URI must match the selected port.

export PINTEREST_CLIENT_ID='your-client-id'
export PINTEREST_CLIENT_SECRET='your-client-secret'
bin/social-post-pinterest-authorize --token-file=/secure/path/pinterest-tokens.json

Add --sandbox for a Trial app, or --port=8085 to specify the default port explicitly. The CLI opens the Pinterest authorization flow and, on success, reports the token-file path; it never prints access-token or refresh-token values. Keep the token file outside the web root and do not commit it.

Usage

Quick start

<?php

declare(strict_types=1);

use AdachSoft\SocialPostContracts\Model\Hashtag;
use AdachSoft\SocialPostContracts\Model\HashtagCollection;
use AdachSoft\SocialPostContracts\Model\HttpUrl;
use AdachSoft\SocialPostContracts\Model\MediaSource;
use AdachSoft\SocialPostContracts\Model\SocialPostDto;
use AdachSoft\SocialPostPinterest\Api\PinterestEnvironmentEnum;
use AdachSoft\SocialPostPinterest\Auth\FileTokenStore;
use AdachSoft\SocialPostPinterest\Auth\PinterestOAuthCredentials;
use AdachSoft\SocialPostPinterest\PinterestPublisher;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\HttpFactory;

$factory = new HttpFactory();
$publisher = PinterestPublisher::create(
    new PinterestOAuthCredentials(
        getenv('PINTEREST_CLIENT_ID') ?: throw new RuntimeException('Set PINTEREST_CLIENT_ID.'),
        getenv('PINTEREST_CLIENT_SECRET') ?: throw new RuntimeException('Set PINTEREST_CLIENT_SECRET.'),
    ),
    new FileTokenStore('/secure/path/pinterest-tokens.json'),
    new Client(),
    $factory,
    $factory,
    PinterestEnvironmentEnum::PRODUCTION, // Use SANDBOX for a Trial app.
);

$board = $publisher->findChannelByName('travel')
    ?? $publisher->createChannel('travel', 'Places to visit');
$post = new SocialPostDto(
    $board->id,
    MediaSource::imageUrl(new HttpUrl('https://images.example.com/pin.jpg')),
    'A scenic hike through the mountains.',
    'Mountain hiking trail',
    new HttpUrl('https://example.com/trails/mountain-hike'),
    'A mountain trail beneath a clear sky.',
    new HashtagCollection([new Hashtag('travel'), new Hashtag('hiking')]),
);

$published = $publisher->publish($post);
echo $published->url?->value . PHP_EOL;

The adapter also supports base64 image uploads, listing boards with pagination, creating boards, and deleting pins. PinterestPublisher::capabilities() and PinterestPublisher::limits() expose the supported capabilities and schema limits.

Token rotation and storage

The authorization-code exchange requests continuous refresh. On API requests, stored access tokens are refreshed automatically five minutes before expiry; any rotated tokens are persisted for subsequent requests. FileTokenStore writes atomically, creates its directory with owner-only permissions where possible, and enforces mode 0600 on the token file. Protect the containing directory and host account as well, and never store the token file in a public web directory or source control.

Pinterest limits and validation

Field or inputLimit
Pin title100 characters
Description, including rendered hashtags800 characters
Alt text500 characters
Destination link2048 characters
Base64 image uploadimage/jpeg or image/png only
Board IDNumeric string

Image URLs and base64 uploads are supported. Pinterest board IDs returned by the API are used as numeric channel IDs.

Error handling

The adapter reports provider-independent exceptions from adachsoft/social-post-contracts:

ExceptionMeaning
AuthenticationFailedExceptionAuthorization is missing or rejected, a token is invalid or expired, or Pinterest denies access because of scopes or app access level.
InvalidPostExceptionA post or board identifier fails local validation, or Pinterest rejects a request with HTTP 400.
RateLimitedExceptionPinterest returns HTTP 429; the exception includes a retry delay when the response provides one.
ChannelNotFoundExceptionThe requested Pinterest board is not found during a board-dependent operation.
PostNotFoundExceptionThe pin to delete is not found.
UnsupportedFeatureExceptionA requested publishing capability is not supported by the selected publisher.
PublishTransportExceptionA network request fails or Pinterest returns an unsuccessful status or invalid response that has no more specific mapping.

Running tests

The default PHPUnit run is network-free when the integration-test environment variables are unset: the integration test skips itself. Run only the unit suite with:

vendor/bin/phpunit --testsuite unit

To run the live Pinterest integration test, provide a dedicated test app and token file. By default it targets production; set PINTEREST_TEST_SANDBOX=yes to use the sandbox. The test finds or creates the adachsoft-integration-test board, publishes one pin, and deletes that pin in a finally block.

export PINTEREST_TEST_CLIENT_ID='your-test-client-id'
export PINTEREST_TEST_CLIENT_SECRET='your-test-client-secret'
export PINTEREST_TEST_TOKEN_FILE='/secure/path/pinterest-test-tokens.json'
export PINTEREST_TEST_SANDBOX='yes'
vendor/bin/phpunit --testsuite integration

Do not provide production credentials unless you intend to create and delete a real Pinterest pin. Unset the integration variables to keep the default test run network-free.