Skip to content

Instantly share code, notes, and snippets.

@lewisrodgers
Last active December 24, 2017 17:29
Show Gist options
  • Save lewisrodgers/7bcc8ac4484b7130cb99cb340a3271ce to your computer and use it in GitHub Desktop.
Save lewisrodgers/7bcc8ac4484b7130cb99cb340a3271ce to your computer and use it in GitHub Desktop.
#!/bin/bash
# This script is used to update a chrome extension that's already
# been uploaded to the Chrome Web Store. You'll need to know the
# chrome extension's ID that you want to update.
# To find the app ID see: https://developer.chrome.com/webstore/publish#get-the-app-id
#
# usage:
# ./chrome-web-store-api-update-utility.sh $APP_ID
# Get a set of OAuth 2.0 credentials for an "installed" application type:
#
# 1. Visit the Google Developers Console.
# 2. Create a new project or select an existing one.
# 3. In the sidebar on the left, select APIs & auth.
# 4. In the sidebar on the left, select Credentials.
# 5. Find the lines labeled Client ID and Client secret. Note that there may be a client ID without a client secret for use with Compute Engine and App Engine. In that case, create a new client ID and client secret.
# 6. To create the client ID and client secret, click on Create New Client ID, select Installed Application, and Other under Installed application type.
# 7. Download the json file
#
# This is the path to that file
CREDENTIALS="./credentials.json"
CLIENT_ID=`jq -r ".installed.client_id" $CREDENTIALS`
CLIENT_SECRET=`jq -r ".installed.client_secret" $CREDENTIALS`
# Get a refresh token.
# See how to do this: https://developer.chrome.com/webstore/using_webstore_api#beforeyoubegin
REFRESH_TOKEN="1/a9fFOF..."
# Exchange the refresh token for an access token.
TOKEN=`curl "https://accounts.google.com/o/oauth2/token" -d \
"client_id=${CLIENT_ID}&client_secret=${CLIENT_SECRET}&refresh_token=${REFRESH_TOKEN}&grant_type=refresh_token&redirect_uri=urn:ietf:wg:oauth:2.0:oob" | jq -r ".access_token"`
# You can now use the token to call the API.
# Path to the chrome extension `manifest.json` file.
MANIFEST="./app/manifest.json"
# The ID of the chrome extension you want to update.
# Hardcode the ID if you don't want to pass it as a command line argument.
APP_ID=$1
# The Chrome Web Store expects a packaged zip file.
echo "Packaging chrome extension..."
FILE_NAME=crx.zip
zip -r $FILE_NAME ./app
# Make an upload request to the Chrome Web Store Upload API.
echo "Uploading to Chrome Web Store..."
curl \
-H "Authorization:Bearer ${TOKEN}" \
-H "x-goog-api-version:2" \
-X PUT \
-T ${FILE_NAME} \
-v "https://www.googleapis.com/upload/chromewebstore/v1.1/items/${APP_ID}"
# Clean up.
rm $FILE_NAME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment