Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

httprequest - Is there any place or way to store private key in google app script?

I'm working on a small project using Google App Script now. What I need to do is make both GET and POST request to API. I'm working on a GET request for now and wrote the code below.

var myApiKey = "123456789"
const requestServer = (path, method, params = {}) => {
  if (method.toUpperCase() === "GET"){
    var url = "https://api.xyz.com/" + path + "apikey=" + myApiKey; 
    var response = UrlFetchApp.fetch(url);
    var json = response.getContentText();
    var data = JSON.parse(json);
  } else if (method.toUpperCase() === "POST"){

  }
}

I used fake url and apikey for asking this question. My question is if there is a place or a way to store myApiKey in Google App Script, like .env file? If so how should I store and make my key hidden...

question from:https://stackoverflow.com/questions/65949296/is-there-any-place-or-way-to-store-private-key-in-google-app-script

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In order to achieve your goal, how about the following 3 patterns.

Pattern 1:

In this pattern, how about using Properties Service? Ref When the script is run, the value can be put and retrieved using Properties Service.

In this case, at first, it is required to input the value to Properties Service.

Sample script:
var scriptProperties = PropertiesService.getScriptProperties();

// Put value.
scriptProperties.setProperty("key", "value");

// Get value.
var value = scriptProperties.getProperty("key");

Pattern 2:

In this pattern, how about using the custom file properties? Ref This can be put and retrieve the value using Drive API.

In this case, at first, it is required to input the value to the custom file properties.

Sample script:
var fileId = ScriptApp.getScriptId();

// Put value.
Drive.Files.update({properties: [{key: "key", value: "value"}]}, fileId);

// Get value.
var savedKey = "key";
var obj = Drive.Files.get(fileId).properties.filter(({key}) => key == savedKey);
if (obj.length == 0) throw new Error("No property for the inputted key");
var value = obj[0].value;

Pattern 3:

In this pattern, how about using a temporal file? In this case, this can be used like .env file. At first, the value is put to the file (in this case, I think that the text file and Google Docs file can be used.). And, when the script is run, the value is retrieved from the file and use it in the script.

Sample script:
// Put value.
DriveApp.createFile("sample.txt", "value");

// Get value.
var value = DriveApp.getFileById("###fileId###").getBlob().getDataAsString().trim();

References:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...