Google API

GoogleApi

Google API provides the ability to work with Google API.

Method Summary

Apperyio.GoogleApi has the following method(s):

MethodDescription
getAccessToken(SERVICE_ACCOUNT_JSON, scopes)This method provides ability to get google access token for service account.

The method returns access token with provided scopes for service account.

Apperyio.GoogleApi.getAccessToken(SERVICE_ACCOUNT_JSON, scopes)

params is a JSON object with the following fields:

ParameterDescription
SERVICE_ACCOUNT_JSONService account json
scopesRequested scopes

Response

Access token string.

Examples

The following example get Google access token with scopes https://www.googleapis.com/auth/spreadsheets https://www.googleapis.com/auth/drive.file and used it in order to get content of Google Sheet with id 1J2Fw8F8g7HQQOJ_BqGN348DmBYca_FJdYC06hyIEPtw from sheet with name Sheet1 by range A1:Z1000

// Insert the value of your service account
var SERVICE_ACCOUNT = {
    "type": "service_account",
    "project_id": "skilful-ethos-429813-q1",
    "private_key_id": "127f61f42fc5710c867973a6de1cf94fb84e6626",
    "private_key": "-----BEGIN PRIVATE KEY---CONTENT DELETED-----END PRIVATE KEY-----\n",
    "client_email": "[email protected]",
    "client_id": "117228200878370587658",
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://oauth2.googleapis.com/token",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/apperyadmin%40skilful-ethos-429813-q1.iam.gserviceaccount.com",
    "universe_domain": "googleapis.com"
};

try {
    var accessToken = Apperyio.GoogleApi.getAccessToken(SERVICE_ACCOUNT, "https://www.googleapis.com/auth/spreadsheets https://www.googleapis.com/auth/drive.file");
    
    var XHRResponse = XHR2.send("GET", "https://sheets.googleapis.com/v4/spreadsheets/1J2Fw8F8g7HQQOJ_BqGN348DmBYca_FJdYC06hyIEPtw/values/Sheet1!A1:Z1000", {
        "headers": {
            "authorization": "Bearer " + accessToken
        }
    });

    Apperyio.response.success(XHRResponse.body, "application/json");
} catch (e) {
    Apperyio.response.error(e);
}