GitHub - pinterest/api-quickstart: Code that makes it easy to get started with the Pinterest API. (original) (raw)

pinterest/api-quickstart

Code that makes it easy to get started with the Pinterest API. For full details see the getting started documentation.

Purpose

This repository has code that is intended to provide a quick start for working with the Pinterest API v5. There is currently python code that implements a number of use cases, JavaScript (nodejs) code with essentially the same functionality, a bash script that demonstrates the OAuth authentication and authorization flow, and an example application in PHP.

This quickstart used to support Pinterest API version v3 and v4, but that code has been removed so that everyone can focus on v5. If you're interested in code that shows the differences between using v3/v4 and v5, see version 1.1 of this repo.

Quick Start

  1. Set up the environment with your credentials (app ID and secret). This configuration works with the code in all of the language-specific directories.
    • Get an application ID and secret by hitting the "Connect app" button at the apps dashboard. You may first need to follow the steps required to request trial access to the Pinterest API. You can also find step-by-step instructions on the Glitch-based tutorial.
    • Once your app is connected, hit the Manage button for the app on the apps dashboard to see your App id and App secret key. (Click the Show key button to see the App secret key.)
    • Put your App ID and App secret key in an environment script file.
    $ cd common/scripts  
    $ cp api_app_credentials.template api_app_credentials  
    # edit api_app_credentials and enter your app id and secret in the specified locations  
    $ cd ../..  
    • Configure the OAuth2 redirect URI required by this code.
      1. Click on the Manage button for your application at the apps dashboard.
      2. In the box labeled "Redirect link," enter http://localhost:8085/.
      3. Hit the return (enter) key or the Add button to save redirect URI (link).
    • Run the environment set-up script and verify the results.
    $ . ./common/scripts/api_env  
    $ env | grep PINTEREST_APP  
    PINTEREST_APP_ID=<number>  
    PINTEREST_APP_SECRET=<string>  
  2. Pick one of the language directories (currently bash, nodejs, php, and python) and follow the directions in the README file in the directory:

OAuth 2.0 Authorization

Access to Pinterest APIs via User Authorization requires following a flow based on OAuth 2.0. To learn about how to use OAuth 2.0 with the Pinterest API, check out the Glitch-based tutorial. For details regarding OAuth, please refer to our v5 developer docs. The code in this repo demonstrates how to initiate the flow by starting a browser, and then handling the OAuth redirect to the development machine (localhost). The browser is used to obtain an authorization code, and then the code invoked by the redirect exchanges the authorization code for an access token.

An access token is used to authenticate most API calls. In general, access tokens are valid for relatively long periods of time, in order to avoid asking users to go through the OAuth flow too often. When an access token expires, it is possible to refresh the token -- a capability that the code in this repo also demonstrates.

Like users, most developers do not want to have to go through the OAuth flow too often. So, the python code supports (and soon, other languages will support) two methods for storing OAuth access token:

The precedence order in this repo for obtaining an access token is: environment, file, execute the OAuth 2.0 flow.

Code that implements OAuth is available for each language in this repo. The location of the code is as follows.

Security Notes

Repository Layout

Code Conventions

The README file in each language-specific directory provides details for the code conventions for the associated programming language.

All shell scripts in this respository are written for bash and are linted with ShellCheck.

Other Resources