You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Google Apps Script调用Google PaLM API?

能否用Google Apps Script调用PaLM API?

可以直接通过Google Apps Script向PaLM API发送HTTP请求,无需等待官方推出专属库。以下是具体实现步骤和示例代码:

前置准备

  • 获取有效的PaLM API密钥(可在Google AI Studio中创建)
  • 确保你的Google账号已开通PaLM API访问权限

示例代码

function callPaLMAPI() {
  const apiKey = "YOUR_API_KEY"; // 替换为你的API密钥
  const apiUrl = "https://generativelanguage.googleapis.com/v1/models/text-bison-001:generateText?key=" + apiKey;
  
  const payload = {
    prompt: {
      text: "请写一段关于春天的短诗"
    },
    temperature: 0.7,
    maxOutputTokens: 256
  };
  
  const options = {
    method: "post",
    contentType: "application/json",
    payload: JSON.stringify(payload)
  };
  
  try {
    const response = UrlFetchApp.fetch(apiUrl, options);
    const data = JSON.parse(response.getContentText());
    console.log(data.candidates[0].output);
  } catch (error) {
    console.error("请求出错:", error.message);
  }
}

说明

  • 代码使用Apps Script内置的UrlFetchApp服务发送POST请求,无需额外依赖
  • 需将代码中的YOUR_API_KEY替换为你自己的PaLM API密钥
  • 可根据需求调整payload内的参数:temperature控制生成内容的随机性,maxOutputTokens限制输出文本长度
  • 请求失败时,可通过error对象查看具体报错信息,常见问题包括API密钥无效、权限不足或请求格式错误

内容的提问来源于stack exchange,提问作者Linda Lawton - DaImTo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 21:43:09