#!/usr/bin/env bash # # This script uses curl, openssl, and some helper utilities to demonstrate how to # use OAuth authentication with the Pinterest API. # # To see the communications at any stage of this script, use echo to show the # relevant variable. For example, echo "$REDIRECT_SESSION" to see the complete # web browser session for the redirect. # # Prerequisites: curl, openssl, base64, jq, grep, cut # # Get configuration from environment or defaults. : "${REDIRECT_PORT:=8085}" : "${PINTEREST_API_URI:=https://api.pinterest.com}" : "${PINTEREST_OAUTH_URI:=https://www.pinterest.com}" : "${REDIRECT_LANDING_URI:=https://developers.pinterest.com/apps/${PINTEREST_APP_ID}}" REDIRECT_URI="http://localhost:${REDIRECT_PORT}/" # Note that the application id and secrect have no defaults, # because it is best practice not to store credentials in code. B64AUTH=$(echo -n "${PINTEREST_APP_ID}:${PINTEREST_APP_SECRET}" | base64) # Get the authorization code by starting a browser session and handling the redirect. echo 'getting auth_code...' # Specify the scopes for the user to authorize via OAuth. # This example requests typical read-only authorization. # For more information, see: # https://developers.pinterest.com/docs/getting-started/authentication-and-scopes/#requesting-the-right-scopes-for-your-app SCOPE="user_accounts:read" # This call opens the browser with the oauth information in the URI. open "${PINTEREST_OAUTH_URI}/oauth/?consumer_id=${PINTEREST_APP_ID}&redirect_uri=${REDIRECT_URI}&scope=${SCOPE}&response_type=code" & # 1. Use netcat (nc) to run the web browser to handle the redirect from the oauth call. # 2. Wait for the response. # 3. Redirect using a HTTP 301 response to the landing URI. REDIRECT_SESSION=$(nc -l localhost ${REDIRECT_PORT} 2>&1 <