adachsoft / social-post-pinterest
Pinterest API v5 adapter for provider-independent social post publishing and board management.
Requires
- php: ^8.3
- adachsoft/social-post-contracts: ^0.1
- psr/clock: ^1
- psr/http-client: ^1
- psr/http-factory: ^1
Requires (Dev)
- adachsoft/php-code-style: ^0.7.4
- friendsofphp/php-cs-fixer: ^3.95
- guzzlehttp/guzzle: ^7
- phpstan/phpstan: ^2.3
- phpunit/phpunit: ^13
- rector/rector: ^2.9
- symplify/phpstan-rules: ^14.19
Suggests
None
Provides
None
Conflicts
None
Replaces
None
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
- Use a Pinterest business account and create an app at https://developers.pinterest.com/apps/.
- Register this redirect URI exactly, including the trailing slash:
http://localhost:8085/. - Grant the app these OAuth scopes:
boards:readboards:writepins:readpins:writeuser_accounts:read
- Check the app's access level. Trial apps must use Pinterest's sandbox environment (
https://api-sandbox.pinterest.com/v5) for write requests. Use--sandboxwith the authorization CLI andPinterestEnvironmentEnum::SANDBOXin 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 input | Limit |
|---|---|
| Pin title | 100 characters |
| Description, including rendered hashtags | 800 characters |
| Alt text | 500 characters |
| Destination link | 2048 characters |
| Base64 image upload | image/jpeg or image/png only |
| Board ID | Numeric 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:
| Exception | Meaning |
|---|---|
AuthenticationFailedException | Authorization is missing or rejected, a token is invalid or expired, or Pinterest denies access because of scopes or app access level. |
InvalidPostException | A post or board identifier fails local validation, or Pinterest rejects a request with HTTP 400. |
RateLimitedException | Pinterest returns HTTP 429; the exception includes a retry delay when the response provides one. |
ChannelNotFoundException | The requested Pinterest board is not found during a board-dependent operation. |
PostNotFoundException | The pin to delete is not found. |
UnsupportedFeatureException | A requested publishing capability is not supported by the selected publisher. |
PublishTransportException | A 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.