Integrating Pinterest API in Laravel

In this tutorial, I’ll show you how to integrate and use Pinterest API in your Laravel Application. At first, I wanted to use dirkgroenen/pinterest-api-php which would work flawlessly for any PHP project but I faced an issue when trying to integrate it into Laravel. I added this package, wrote a service provider and a facade to statically call methods on the Pinterest class. This package would have worked fine if I would have used the dependency injection technique where you would type-hint a class on constructor to resolve and inject it into your controller. Here’s an example Service Provider and Controller.

But instead, I wanted to use Facade. Here is an example of Facade and Controller.

When I called my controller method, it rendered an error saying that auth cannot be called statically. It was then that I realized that Facade classes can only redirect method calls to the underlying class. So I checked and there were no getter setters available to access $auth or $request data members on the Pinterest class. This is where this package failed for my specific use case. So I forked this package, made all the necessary changes required to make it work with Laravel Facades, updated docs and published it as waleedahmad/laravel-pinterest.

Getting Started

After creating a new Laravel Project, run php artisan make:auth to generate authentication scaffold. Now install these dependencies.

If your Laravel project doesn’t support Package auto-discovery, add these service providers to $providers[]  array in config/app.php

Now run

to publish Pinterest configuration to your Laravel config folder. Also, add SocialiateWasCalled event to $listen[] array in EventServiceProvider.

Register a Pinterest Application

Create a new Application in by visiting developers.pinterest.com. Fill in App name and description.

Create and copy app id and secret and update env variables in .env file.

Make sure you update app settings and enter same redirect URI in web section.

Authentication

Since Pinterest API doesn’t return email address of the authorized user, we’ll register the user with conventional form based authentication and then later ask them to authorize with Pinterest OAuth service. Create PinterestController under Auth Controller directory by running.

You can remove or add your own scopes to Socialite scopes method. Read more about here. On callback, we’ll create a new record on pinterest table and create or update token. Create a migration for pinterest table.

Edit User model and add this method to it to create a one to one relation between users and pinterest table.

Also update $fillable[]  array on your Pinterest model.

Update your routes and add index route.

Render index view for it and add this code to your index view.

We’ll render a login link if a user doesn’t have a record on pinterest table. Otherwise, we’ll render user details.

Middleware

You need to set user token before using API via Pinterest Facade. Write a middleware.

php artisan make:middleware PinterestMiddleware

We’re setting user access token by calling setOAuthToken() if user has a pinterest record. Register this middleware by adding it to  app\Http\Kernel.php  $routeMiddleware[] array.

Now add pinterest middleware to your authenticated route group in routes file.

API Usage

Update your routes file.

Get User

By default, API will only return a few user attributes. You can ask it for more by passing an array to  me() method with.

Get Pins

You can get user pins by calling getMePins() method. By default API return only 25 pins and paginate results for you. You can check if it has a next page by calling $pins->hasNextPage(). To get next page results, you can pass cursor to get getMePins()  method returned from the last collection.

Get Boards

getMeBoards()  will return user boards. If you’ve more than 25 boards, API will automatically paginate results for you and you can fetch next page like we did with  getMePins()  method.

Create Pin

You can create a pin from an external source, storage path, and base64 encoded string.

Edit Pin

You can update pin caption by passing a note to edit()  method.

Get Following Users

You can retrieve following users by calling users() chained method via following() object. Just like pins and routes, results will be paginated with a cursor to next page.

Get Following Boards

 

You can retrieve user following boards by calling following()->boards().

Get Following Interests

You can retrieve user interests by calling following()->interests() .

Follow/Unfollow User, Boards and Interests

You can follow or unfollow User, boards, and interests by following methods. You can wrap them in an if condition to check if your action was successful.

There are so many more methods available to perform action on user behalf. Read package docs for more info. If API starts to throw 429 error, it means that you ran out of requests.

Effective April 16 2018, all unapproved apps are allowed 10 calls per hour for each unique user token.

After you successfully submit an app for approval, each app (with a unique app ID) is allowed 1000 calls per hour for each unique user token.

In both cases, the 60-minute window is a sliding window based on when you make your first request. If you hit your rate limit, you’ll have to wait a minimum of 1 hour to get a few more requests.

Read more about rate limiting here. I’ve also set up a demo repository.  If you ran into an issue or my package threw an error, you can comment here or send me a message on Facebook page. I’ll try to help you with it.

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.