Google Sheet脚本出现权限问题,用于AI SEO网站审核。

huangapple go评论149阅读模式
英文:

Permission problems with Google Sheet Script for ai SEO website audit

问题

我正在尝试实现一篇文章中的代码片段,以帮助我进行一些家庭/爱好SEO项目。该脚本应该抓取一个网站,运行审核并提供建议。

我遇到了一个权限问题,谷歌正在阻止请求。一切都是在我的个人帐户内进行的,所以我不确定需要修改哪些权限(或如何修改)。

以下是代码片段:

/**
 * 对指定的URL执行内容审核,使用OpenAI的语言模型提供质量评级和可操作的建议。
 *
 * @param {string} url 要审核的URL。
 * @return 由语言模型提供的内容质量评级和建议。
 * @customfunction
 */
function performContentAudit(url) {
  // 获取活动工作表和要审核的值范围
  var sheet = SpreadsheetApp.getActiveSheet();
  var range = sheet.getDataRange();
  var values = range.getValues();

  // 遍历值范围中的每个URL并执行审核
  for (var i = 0; i < values.length; i++) {
    if (values[i][0] === url) { // 检查这是否是要审核的URL
      var prompt = "我想要你审核内容...: " + url;

      // 调用OpenAI API执行审核
      var response = UrlFetchApp.fetch("https://api.openai.com/v1/engines/davinci-codex/completions", {
        'method': 'post',
        'headers': {
          'Content-Type': 'application/json',
          'Authorization': 'Bearer openai API密钥'
        },
        'payload': JSON.stringify({
          'prompt': prompt,
          'temperature': 0.5,
          'max_tokens': 1024
        })
      });

      // 解析API响应并获取审核结果
      var json = JSON.parse(response.getContentText());
      var answer = json.choices[0].text;

      // 返回审核结果
      return answer;
    }
  }

  // 如果指定的URL未在表中找到,返回错误消息
  return "错误:表中未找到URL";
}

我收到了权限请求,但然后收到以下错误:

此应用已被阻止
此应用尝试访问您的Google帐户中的敏感信息。为了保护您的帐户安全,Google已阻止此访问。

任何想法都会受到欢迎。

英文:

I am trying to implement a code snippet from an article to help with some home / hobby SEO projects. The script should pull a website, run an audit, and make suggestions.
I run into a permission issue where Google is blocking the request. Everything is being done within my personal account, so I'm not sure where what permissions need to be modified (or how).
Code snippet is as follows:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

/**
 * Performs a content audit on the specified URL, using OpenAI&#39;s language model to provide a quality rating and actionable recommendations.
 *
 * @param {string} url The URL to audit.
 * @return The content quality rating and recommendations as provided by the language model.
 * @customfunction
 */
function performContentAudit(url) {
  // Get the active sheet and the range of values to audit
  var sheet = SpreadsheetApp.getActiveSheet();
  var range = sheet.getDataRange();
  var values = range.getValues();

  // Loop through each URL in the range and perform an audit
  for (var i = 0; i &lt; values.length; i++) {
    if (values[i][0] === url) { // check if this is the URL we want to audit
      var prompt = &quot;I would like you to audit content ...: &quot; + url;

      // Call the OpenAI API to perform the audit
      var response = UrlFetchApp.fetch(&quot;https://api.openai.com/v1/engines/davinci-codex/completions&quot;, {
        &#39;method&#39;: &#39;post&#39;,
        &#39;headers&#39;: {
          &#39;Content-Type&#39;: &#39;application/json&#39;,
          &#39;Authorization&#39;: &#39;Bearer openai API KEY &#39;
        },
        &#39;payload&#39;: JSON.stringify({
          &#39;prompt&#39;: prompt,
          &#39;temperature&#39;: 0.5,
          &#39;max_tokens&#39;: 1024
        })
      });

      // Parse the API response and get the audit results
      var json = JSON.parse(response.getContentText());
      var answer = json.choices[0].text;

      // Return the audit results
      return answer;
    }
  }

  // Return an error message if the specified URL was not found
  return &quot;Error: URL not found in sheet&quot;;
}

<!-- end snippet -->

I'm prompted to allow permissions but then receive the error:

> This app is blocked
This app tried to access sensitive info in your Google Account. To keep your account safe, Google blocked this access.

Any ideas are appreciated.

答案1

得分: 1

根据类似的案例,可以在您的Google Cloud控制台中创建一个新项目,并将项目ID分配给Apps Script项目,以便无需问题进行授权。

步骤如下:

  1. 转到https://console.cloud.google.com/

  2. 创建一个新项目

  3. 转到侧边栏菜单中的“OAuth Consent Screen”。

    • 配置项目设置,对于免费的Gmail帐户,请选择“external”用户类型。
    • 设置“应用名称”、“用户支持电子邮件”和“开发者联系信息”。
    • 保存并继续。
  4. 转到侧边栏菜单,然后转到“API和服务” > “库”,搜索Drive API和Sheets API,启用这两项服务。

  5. 单击右上角的3个点,选择“项目设置”,复制“项目编号”。

  6. 返回到您的Apps Script项目,转到侧边栏菜单,然后转到“项目设置”。

  7. 转到“Google Cloud Platform (GCP)项目”部分,并插入步骤5中的项目编号。

  8. 最后再次运行脚本,您将被要求进行授权,这次应该可以让您继续而不出现问题。

您可以在这里查看更详细的步骤。

英文:

According to similar cases, creating a new project in your Google Cloud console and assign the project ID to the Apps Script project allows you to authorize without issues.

The steps are the following:

  1. Go to https://console.cloud.google.com/

  2. Create a new project

  3. Go to the ‘OAuth Consent Screen’ on the sidebar menu.

    • Configure the project settings, for free Gmail accounts, select the ‘external’ user type.
    • Set the App name, User support email and Developer contact information.
    • Save and continue.
  4. Go to the sidebar menu and go to APIs &amp; Services > Library, search for Drive API, Sheets API and enable both services.

  5. Click the 3 dots on the top right and select "Project settings", copy the Project number.

  6. Go back to your Apps Script project, go to the sidebar menu and go to Project Settings.

  7. Go to the Google Cloud Platform (GCP) Project section and insert the project number from step (5).

  8. Finally run the script again, you'll be asked to authorize and this time it should let you continue without issues.

You can see more detailed steps here.

huangapple
  • 本文由 发表于 2023年3月10日 01:06:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75687828.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定