API

Fields Builder for Google APIs: Visualizing Partial Responses

Gists

Abstract

Fields Builder for Google APIs is a client-side web application that streamlines constructing the fields parameter. It parses Discovery documents into interactive trees, enabling developers to visually select nested resources, implement Partial Response, and optimize API payload sizes without manual syntax errors.

Introduction

FieldsBuilderForGoogleAPIs is a specialized Web Application designed to streamline the construction of the fields parameter for Google APIs.

Since the update of the official Google API Explorer, developers have found it increasingly difficult to visually construct the fields parameter. This tool bridges that gap by parsing official Discovery documents to build an interactive, navigable tree view of the response schema.

Shortening a Long URL using Firebase Dynamic Links API with Google Apps Script

Gists

This is a sample script for shortening a long URL using Firebase Dynamic Links API with Google Apps Script.

IMPORTANT

Before you use this script, please create a new Firebase project and link it to your Google Cloud Platform Project. Ref And, please enable Firebase Dynamic Links API at the API console. And then, please create your API key from your Google Cloud Platform Project.

Sample script

const apiKey = "###"; // Please set your API key.
const longUrl = "###"; // Please set the long URL you want to shorten.
const yourDynamicLinkDomain = "###"; // Please set your dynamic link domain.

const url =
  "https://firebasedynamiclinks.googleapis.com/v1/shortLinks?key=" + apiKey;
const options = {
  payload: JSON.stringify({
    dynamicLinkInfo: {
      dynamicLinkDomain: yourDynamicLinkDomain,
      link: longUrl,
    },
  }),
  contentType: "application/json",
};
const res = UrlFetchApp.fetch(url, options);
const { shortLink } = JSON.parse(res.getContentText());

console.log(shortLink);
  • When this script is run, longUrl is shortened.

Note

If the warning of Setup URL patterns to whitelist in the Firebase Dynamic Links console. is shown, please include the URL to the whitelist to “Allowlist URL pattern”. Ref By this, the warning can be removed.

Requesting to Gate API v4 using Google Apps Script

Gists

This is a sample script for requesting to Gate API v4 using Google Apps Script.

The official document of Gate API v4 is here. Recently, I answered this thread. In that case, in order to convert the sample python script to Google Apps Script, the script for retrieving the signature might be a bit complicated. So, here, I would like to introduce this.

Sample python script from official document

This is a sample python script from official document.

Web Apps: Fields Builder For Google APIs

Overview

FieldsBuilderForGoogleAPIs is a Web Application for building the fields value for using Google APIs. This is mainly used for developing the scripts for using Google APIs.

Demo

Description

After the API explorer was updated, the fields of APIs cannot be created by the web interface. But it is important for using the fields property. Because when the fields property is used, only the required values can be retrieved. This leads to the low cost. And even when * is used, there are some cases that all fields cannot be retrieved. So I created this as a Web Application. This web application can run with the standalone. No authorization and no scopes are required. This only creates the fields value for Google APIs.

Specification of Google Apps Script Project was Changed at April 8, 2019

Gists

At April 8, 2019, the specification of Google Apps Script Project was changed. Various specification was changed. Please see the detail at Google Cloud Platform Projects. Here, I would like to introduce one change which might be useful for users. The official document says as follows.

When you enable an advanced service for your script project in the Apps Script editor, it is automatically enabled in the default GCP project when the script project is saved.

Cryptopia API for Google Apps Script

Gists

By the Google’s update at June 19, 2018, finally, Utilities.computeDigest(), Utilities.computeHmacSha256Signature() and Utilities.computeHmacSignature() got to be able to use the byte arrays. By this, using only native Google Apps Script, the result can be retrieved without using jsSHA. So Cryptopia API can be created using only Google Apps Script. If this is useful for you, I’m glad.

Sample script :

When you use this, at first, please input the requiring values. Now the sample values are used.

Copying and Overwriting GAS Project

Gists

Pattern 1

This is a sample script for copying GAS project to a container-bound script of Google Docs (Spreadsheet, Document and Form (and Slides)). The project is created as a new project.

In order to use this sample, please do the following installation flow.

If you use this sample script, at first, please test using a new project and new Google Docs. By this, please understand the work of this script.

Zaif API for Google Apps Script

Gists

This sample script is for using Zaif API by Google Apps Script.

The following go script is a sample at Zaif API.

package main

import (
    "fmt"
    "time"
    "strconv"
    "crypto/hmac"
    "crypto/sha512"
    "io/ioutil"
    "net/http"
    "encoding/hex"
    "net/url"
    "strings"
)

var key = "<your_key>"
var secret = "<your_secret>"

func main() {

    uri := "https://api.zaif.jp/tapi"
    values := url.Values{}
    values.Add("method", "get_info")
    values.Add("nonce", strconv.FormatInt(time.Now().Unix(), 10))

    encodedParams := values.Encode()
    req, _ := http.NewRequest("POST", uri, strings.NewReader(encodedParams))

    hash := hmac.New(sha512.New, []byte(secret))
    hash.Write([]byte(encodedParams))
    signature := hex.EncodeToString(hash.Sum(nil))

    req.Header.Add("Key", key)
    req.Header.Add("Sign", signature)
    client := new(http.Client)
    resp, _ := client.Do(req)
    defer resp.Body.Close()

    byteArray, _ := ioutil.ReadAll(resp.Body)
    fmt.Println(string(byteArray))
}

When this is converted to GAS, the script is as follows.

Bittrex API for Google Apps Script

Gists

This sample script is for using Bittrex API by Google Apps Script.

The following PHP script is a sample at bittrex.com.

$apikey='xxx';
$apisecret='xxx';
$nonce=time();
$uri='https://bittrex.com/api/v1.1/market/getopenorders?apikey='.$apikey.'&nonce='.$nonce;
$sign=hash_hmac('sha512',$uri,$apisecret);
$ch = curl_init($uri);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('apisign:'.$sign));
$execResult = curl_exec($ch);
$obj = json_decode($execResult);

When this is converted to GAS, the script is as follows.

function main() {
  var apikey = '#####'; // Please input your key.
  var apisecret = '#####'; // Please input your secret.

  var nonce = Number(new Date().getTime() / 1000).toFixed(0);
  var uri = 'https://bittrex.com/api/v1.1/market/getopenorders?apikey=' + apikey + '&nonce=' + nonce;
  var sign = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512, uri, apisecret);
  sign = sign.map(function(e) {return ("0" + (e < 0 ? e + 256 : e).toString(16)).slice(-2);}).join("");
  var params = {
    method: "get",
    headers: {Apisign: sign}
  }
  var res = UrlFetchApp.fetch(uri, params);
  Logger.log(res.getContentText())
}

Binance API for Google Apps Script

Gists

This sample script is for using Binance API by Google Apps Script. This script encryptes “signature” like samples. In this script, “Example 1: As a query string” is used, and it retrieves “All orders (SIGNED)” by “GET”.

function main() {
    var key = '#####'; // Please input your key.
    var secret = '#####'; // Please input your secret.
    var api = "/api/v3/allOrders"; // Please input API Endpoint you want.
    var timestamp = Number(new Date().getTime()).toFixed(0);
    var string = "symbol=LTCBTC&timestamp=" + timestamp; // Please input query parameters for the inputterd API.

    var baseUrl = "https://api.binance.com";
    var signature = Utilities.computeHmacSha256Signature(string, secret);
    signature = signature.map(function(e) {
        var v = (e < 0 ? e + 256 : e).toString(16);
        return v.length == 1 ? "0" + v : v;
    }).join("");
    var query = "?" + string + "&signature=" + signature;
    var params = {
        'method': 'get',
        'headers': {'X-MBX-APIKEY': key},
        'muteHttpExceptions': true
    };
    var data = UrlFetchApp.fetch(baseUrl + api + query, params);
    Logger.log(data.getContentText())
}

Bitfinex API for Google Apps Script

Gists

This sample script is converted this sample script (javascript) to Google Apps Script. The point for converting is signature as shown in the following sample script.

  • At Bitfinex API, after "/api/" + apiPath + nonce + rawBody is encrypted using HMAC SHA-384, the data of byte array is converted to HEX.
    • In Google Apps Script, there is no the method for this.
    • The data which was encrypted by Utilities.computeHmacSignature() is the bytes array of the signed hexadecimal.
    • On the other hand, at this sample script for javascript, the data which was encrypted by crypto.createHmac('sha384', apiSecret).update(signature).digest('hex') is the string of the unsigned hexadecimal.

In order to achieve above, I made the method of bytesToHex().