我尝试创建一个函数将我的 Appscript 连接到 Firebase 数据库。 出于某种原因,它向我展示了:
2:29:44PM 错误
类型错误:file.blob 不是函数 getServiceAccountKey @ firebase/firebase.gs:7
这是我的代码:
const CREDENTIALS_FILE_ID = "Your File ID";
const PROJECT_ID = "Project ID";
function getServiceAccountKey() {
var file = DriveApp.getFileById(CREDENTIALS_FILE_ID);
JSON.parse(file.blob().getDataAsString());
}
function testFunc(){
console.log(getServiceAccountKey());
}
function getAccessToken() {
var serviceAccount = getServiceAccountKey();
var tokenUrl = "https://www.googleapis.com/oauth2/v4/token";
var payload = {
grant_type: "urn:ietf:params:oauth:grant-type:jwt-bearer",
assertion: createJWT(serviceAccount),
};
var options = {
method: "post",
payload: payload,
};
var response = UrlFetchApp.fetch(tokenUrl, options);
var token = JSON.parse(response.getContentText()).access_token;
return token;
}
我也尝试使用其他方法如
.stringify()
,但还是不行。
您的代码是正确的,如果您要使用本机 JavaScript,它将可以工作。但是,如果您使用的方法语法现在有所不同。
function getServiceAccountKey() {
var file = DriveApp.getFileById(CREDENTIALS_FILE_ID);
JSON.parse(file.getBlob().getDataAsString());
}