domenica 1 febbraio 2015

Experience with the Google Play Developer API

If the Android developer console is a powerful tool, the manage of a multi language Android app is oft a long time-consuming procedure.
So I've tested the Google Play Developer Publishing API.

As described in the Getting Started page, I've linked my Google Play Developer Console to an API project and created a OAuth2 Client in the API Access page on the Google Play Developer Console .

Here there's the code of a simple bash script:
#!/bin/sh

PACKAGE_NAME=<package name of the Android app>
BASE_URL=https://www.googleapis.com/androidpublisher/v2/applications/$PACKAGE_NAME

# see https://play.google.com/apps/publish/#ApiAccessPlace
CLIENT_ID=<CLIENT ID value>
CLIENT_SECRET=<CLIENT SECRET value>
REDIRECT_URL=urn:ietf:wg:oauth:2.0:oob

#-----------------------------------------------
# https://developers.google.com/accounts/docs/OAuth2InstalledApp
CODE=$1
if [ "$CODE" = "" ]
then
	echo "opening the browser to obtain a client code ..."
	xdg-open "https://accounts.google.com/o/oauth2/auth\
?scope=https://www.googleapis.com/auth/androidpublisher\
&response_type=code\
&access_type=offline\
&redirect_uri=$REDIRECT_URL\
&client_id=$CLIENT_ID"
	exit 255
fi

#-----------------------------------------------------------------------
# https://developers.google.com/android-publisher/authorization
REQUEST_URL="https://accounts.google.com/o/oauth2/token"
REQUEST_BODY="grant_type=authorization_code\
&code=$CODE\
&client_id=$CLIENT_ID\
&client_secret=$CLIENT_SECRET\
&redirect_uri=$REDIRECT_URL"
RESPONSE="/tmp/response-auth.json"
echo "Retrieving the authorization ..."

curl -s -o $RESPONSE -X POST -d "$REQUEST_BODY" $REQUEST_URL
AUTHORIZATION=`grep "access_token" $RESPONSE | cut -d '"' -f 4`
if [ "$AUTHORIZATION" = "" ]; then exit 255; fi

#-----------------------------------------------
# https://developers.google.com/android-publisher/edits/workflow
REQUEST_URL="$BASE_URL/edits"
REQUEST_BODY='{"id": "?","expiryTimeSeconds": 9}'
RESPONSE="/tmp/response-editid.json"

echo "Acquiring a edit id ..."
curl -s -o $RESPONSE -X POST -d \
-H "Content-Type: application/json" "$REQUEST_BODY" \
"$REQUEST_URL?access_token=$AUTHORIZATION"
EDIT_ID=`grep "id" $RESPONSE | cut -d '"' -f 4`
if [ "$EDIT_ID" = "" ]; then exit 255; fi

#-----------------------------------------------
# https://developers.google.com/android-publisher/api-ref/edits/listings/list
REQUEST_URL="$BASE_URL/edits/$EDIT_ID/listings"

echo "Retrieving the app description ..."
curl -s -X GET "$REQUEST_URL?access_token=$AUTHORIZATION"

#-----------------------------------------------
# https://developers.google.com/android-publisher/api-ref/edits/images/
REQUEST_URL="$BASE_URL/edits/$EDIT_ID/listings/en-US/phoneScreenshots"

echo "Retrieving the phone screenshots ..."
curl -s -X GET "$REQUEST_URL?access_token=$AUTHORIZATION"

#-----------------------------------------------
# https://developers.google.com/android-publisher/api-ref/edits/apks/list
REQUEST_URL="$BASE_URL/edits/$EDIT_ID/apks"

echo "Retrieving the APKs list ..."
curl -s -X GET "$REQUEST_URL?access_token=$AUTHORIZATION"