代码之家  ›  专栏  ›  技术社区  ›  Humphrey_wes

Appscript无法通过API连接到firebase

  •  0
  • Humphrey_wes  · 技术社区  · 9 月前

    我试图创建一个函数,将我的Appscript连接到Firebase数据库。 出于某种原因,它向我展示了:


    下午2:29:44错误
    类型错误: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() 但仍然不起作用。

    1 回复  |  直到 9 月前
        1
  •  0
  •   Patsytalk MicroAccess    9 月前

    .getBlob()方法

    你的代码是正确的,可以使用原生JavaScript。但是,如果您使用不同的方法,语法可能会有所不同。

    使用的脚本:

    function getServiceAccountKey() {
      var file = DriveApp.getFileById(CREDENTIALS_FILE_ID);
       JSON.parse(file.getBlob().getDataAsString());
    }
    

    参考:

    getBlob()