【问题标题】:How to access cloud datastore from apps script with a service account如何使用服务帐户从应用程序脚本访问云数据存储
【发布时间】:2018-03-05 14:02:30
【问题描述】:

尝试使用这样的服务帐户从谷歌应用脚​​本访问谷歌云数据存储。我从here 得到了样本 我不确定 SCOPE 是否正常或是否需要另一个范围。 运行函数 run() 会出现错误,例如“检索令牌时出错:invalid_scope,https://www.googleapis.com/auth/userinfo.email 不是有效的受众字符串。”

// testing Cloud Datastore access via service account
var PRIVATE_KEY = "-----BEGIN PRIVATE KEY----------END PRIVATE KEY-----\n";
var CLIENT_EMAIL = "xxxxxxxxxxx@appspot.gserviceaccount.com";
var USER_EMAIL = "myemailaddress@mydomain.com";
var CLIENT_ID = "104548139575444821912";
var SCOPE = "https://www.googleapis.com/auth/datastore/v1"; 

/**
 * Authorizes and makes a request to the Cloud Datastore 
 */
function run() {
  var service = getService();
  if (service.hasAccess()) {
    var url = SCOPE;
    var response = UrlFetchApp.fetch(url, {
      headers: {
        Authorization: 'Bearer ' + service.getAccessToken()
      }
    });

    var result = JSON.parse(response.getContentText());
    Logger.log(JSON.stringify(result, null, 2));
  } else {
    Logger.log(service.getLastError());
  }
}

/**
 * Reset the authorization state, so that it can be re-tested.
 */
function reset() {
  getService().reset();
}

/**
 * Configures the service.
 */
function getService() {
  return OAuth2.createService('CloudDatastore:' + USER_EMAIL)
      // Set the endpoint URL.
      .setTokenUrl('https://accounts.google.com/o/oauth2/token')

      // Set the private key and issuer.
      .setPrivateKey(PRIVATE_KEY)
      .setIssuer(CLIENT_EMAIL)
      // .setClientId(CLIENT_ID)

      // Set the name of the user to impersonate. This will only work for
      // Google Apps for Work/EDU accounts whose admin has setup domain-wide
      // delegation:
      // https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority
      // .setSubject(USER_EMAIL)

      // Set the property store where authorized tokens should be persisted.
      .setPropertyStore(PropertiesService.getScriptProperties())

      // Set the scope. This must match one of the scopes configured during the
      // setup of domain-wide delegation.
      .setScope('https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/datastore/v1');
}

【问题讨论】:

    标签: google-apps-script google-cloud-platform google-cloud-datastore


    【解决方案1】:

    曾经尝试过不请求示例中未说明的范围吗?请求范围https://www.googleapis.com/auth/datastore。而 GoogleDrive 示例有 PRIVATE_KEYCLIENT_EMAILUSER_EMAIL(需要分配服务帐户的 PK 和电子邮件地址)......虽然它指出:

    // Set the name of the user to impersonate. This will only work for
    // Google Apps for Work/EDU accounts whose admin has setup domain-wide delegation:
    // https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority
    .setSubject(USER_EMAIL)
    

    如果您的帐号既不用于工作 (GSuite) 也不用于教育,您将无法模拟服务帐号;这给您留下了两个选择:a) 不要尝试使用服务帐户模拟任何人,或 b) 创建具有域范围委派的 GSuite 域。显然不能在类似gmail.com 的域上做到这一点,委派域外帐户也行不通;如果它是GSuite 域,请参阅domain-wide delegation

    曾经尝试过var SCOPES="https://www.googleapis.com/auth/cloud-platform, https://www.googleapis.com/auth/datastore"; 然后.setScope(SCOPES)?请求invalid_scope 不会导致任何结果,这意味着无效或未配置委派。 它指出,

    这必须与设置域范围委派期间配置的范围之一匹配。

    Perform G Suite Domain-Wide Delegation of Authority 解释一下。创建service account 时需要启用域范围的委派 - 然后在Admin Console 中相应委派。 OAuth2ServiceAccount是ID协议。

    在“高级设置”>“身份验证”>“管理 API 客户端访问”下,需要添加带有相应 API 范围的服务帐户的电子邮件地址(该错误消息就在前面的输入中,可能需要两个范围) :

    在 Cloud Console 上,大致相同:

    首先查看API documentation。连接后,请参阅console

    rest/v1/projects 的示例(也可以在那里构建/测试请求)添加到我的 GitHub:

    查看CloudDatastore.gs(它从Google Drive加载配置.json)。

    【讨论】:

    • 看起来很有希望。谢谢。我会试试看。
    • 我现在有了您提供的解决方案的工作版本。
    猜你喜欢
    • 1970-01-01
    • 2019-06-12
    • 1970-01-01
    • 2023-01-19
    • 1970-01-01
    • 2016-06-22
    • 2022-10-06
    • 1970-01-01
    • 2022-11-08
    相关资源
    最近更新 更多