如何从AppMaker调用REST Google Cloud API? [英] How do I call a REST Google Cloud API from AppMaker?

查看:104
本文介绍了如何从AppMaker调用REST Google Cloud API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从AppMaker调用Google Cloud AutoML API,但是很难弄清楚该怎么做.如何从AppMaker向Google Cloud进行REST调用?

I want to call the Google Cloud AutoML API from AppMaker, but it's hard to figure out how to do that. How do I make a REST call to Google Cloud from AppMaker?

推荐答案

首先,按照说明此处生成服务帐户并下载私钥. (我还假设您已经为项目启用了API.)

First, follow the instructions here to generate a service account and download the private key. (I'm also assuming you already enabled the APIs for your project.)

然后,按照附录:无OAuth的服务帐户授权"一节中的说明进行操作,但是您将需要实现自己的JWT编码算法,如下所示:

Then, follow the instructions under the section "Addendum: Service account authorization without OAuth", but you will need to implement your own JWT encoding algorithm as follows:

var base64Encode = function (str) {
    var encoded = Utilities.base64EncodeWebSafe(str);
    // Remove padding
    return encoded.replace(/=+$/, '');
};

// https://developers.google.com/identity/protocols/OAuth2ServiceAccount
// https://wtfruby.com/gas/2018/11/21/jwt-app-scripts.html

var getJWT = function (secret) {
    var header = JSON.stringify({
        typ: 'JWT',
        alg: 'RS256',
        kid: '...'
    });
    var encodedHeader = base64Encode(header);
    var iat = new Date().getTime() / 1000;
    var exp = iat + 3600;
    var payload = JSON.stringify({
      iss: "...",
      sub: "...",
      aud: "https://automl.googleapis.com/",
      iat: iat,
      exp: exp
    });
    var encodedPayload = base64Encode(payload);
    var toSign = [encodedHeader, encodedPayload].join('.');
    var signature = Utilities.computeRsaSha256Signature(toSign, secret);
    var encodedSignature = base64Encode(signature);
    return [toSign, encodedSignature].join('.');
};

  • 从Google API中的服务定义文件中获取API的服务名称和API名称. GitHub存储库
  • 对于标题中的kid字段,请指定服务帐户的私钥 ID.您可以在自己的private_key_id字段中找到此值 服务帐户JSON文件.
  • 对于iss和sub字段,请指定您的 服务帐户的电子邮件地址.您可以在 您的服务帐户JSON文件的client_email字段.
  • 对于音频 字段中,使用的值指定 https://SERVICE_NAME/. 服务定义文件.
  • 对于iat字段,指定当前的Unix 时间,并在exp字段中,准确指定时间为3600秒 稍后,当JWT到期时.
    • Get the API's service name and API name from the service definition file in the Google APIs GitHub repository
    • For the kid field in the header, specify your service account's private key ID. You can find this value in the private_key_id field of your service account JSON file.
    • For the iss and sub fields, specify your service account's email address. You can find this value in the client_email field of your service account JSON file.
    • For the aud field, specify https://SERVICE_NAME/, using the values from the service definition file.
    • For the iat field, specify the current Unix time, and for the exp field, specify the time exactly 3600 seconds later, when the JWT will expire.
    • 使用RSA-256对JWT进行签名 在您的服务帐户JSON文件中找到私钥.

      Sign the JWT with RSA-256 using the private key found in your service account JSON file.

      然后按如下所示进行REST调用:

      Then make the REST call as follows:

      function makeRestCall() {
      
        var jwt = getJWT();
        var options = {
          'method' : 'post',
          'contentType': 'application/json',
          'headers': {
            'Authorization': 'Bearer ' + jwt, 
          },
          'muteHttpExceptions': true,
          'payload' : ...
        };
      
        var url = 'https://automl.googleapis.com/...';  
        return JSON.parse(UrlFetchApp.fetch(url, options).getContentText());
      }
      

      这篇关于如何从AppMaker调用REST Google Cloud API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆