Code that makes it easy to get started with the Pinterest API. For full details see the getting started documentation.
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.
Set up the environment with your credentials (app ID and secret). This configuration works with the code in all of the language-specific directories.
$ 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 ../..
http://localhost:8085/
.$ . ./common/scripts/api_env
$ env | grep PINTEREST_APP
PINTEREST_APP_ID=<number>
PINTEREST_APP_SECRET=<string>
Pick one of the language directories (currently bash, nodejs, php, and python) and follow the directions in the README file in the directory:
cd ./nodejs
cd ./python
cd ./bash
cd ./php
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:
common/oauth_tokens/access_token.json
. To generate this file, use the language-specific get_access_token
script with the -w
argument. You can also use the -a
argument to specify the name of the token, which is the same as the name of the JSON file. For example, set up the python environment and then run python/scripts/get_access_token.py -w -a my_access_token -s READ_USERS
. The contents of this file can be as simple as this JSON: {"access_token": "<access token retrieved from OAuth flow>"}
. The complete set of JSON object keys are:
access_token
: The access token returned by the OAuth flow. [required]name
: A textual description of the access token. (e.g. "API test account #2")refresh_token
: The refresh token returned by the OAuth flow.scopes
: The OAuth scopes associated with the tokenACCESS_TOKEN
, so running the command export ACCESS_TOKEN=<access_token_retrieved_via_oauth>
will work in most situations. This method for specifying an access token is intended to be the easiest, fastest way to use an externally-generated access token with the code in this repo.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.
chmod 600 common/oauth_tokens/*
common/scripts/api_app_credentials
), and access tokens (common/oauth_tokens
) are listed in the .gitignore
file to help avoid checking this material into a git repo.common
directory stores code and files that work with all of the language-specific directories.bash
: shell scriptsnodejs
: JavaScript code and demonstration scripts intended to be run with Node.jsphp
: PHP example applicationpython
: structured python code and demonstration scripts. Since the python code is implemented first, python typically has the most functionality.python
or bash
) may have one or more of these subdirectories:
scripts
are executable files that demonstrate one or more use cases.src
contains code that is used by the scripts and that you can incorporate into your own applications.tests
contains unit and integration tests.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.