使用Google Docs Api-PowerShell更新Google Team Drive内部的文本文件

发布于 2025-02-05 15:25:18 字数 2691 浏览 0 评论 0 原文

我目前有一个PowerShell脚本,该脚本可以从团队驱动器上从Google表中获取数据,然后根据该数据在环境中创建Active Directory用户。我还没有完全测试这部分,但不相信这会给我带来任何问题。

我遇到问题的地方是我想使用团队共享的Google Drive文档来包含我的所有记录以进行自动化。

我没有任何认证的问题,但似乎无法正确批评文档本身。我能够毫无问题地获得修订ID。在我的请求中,我似乎无法找出在哪里传递“ batchupdate”对象的地方。我不必担心文本一旦添加到页面上存储在哪里,只是想将文本添加到文件中。

文档:

这是我在此处尝试更新文档

function Get-Auth{
    #Obtains access token for the Google API's
    #Runs everytime to ensure there is a fresh access token available to the service account
    $refresh_body = @{

        client_id='338139966542-qaa4me7l4fs0l1ltl2e6kjidjr9tf3up.apps.googleusercontent.com';
        client_secret='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';
        refresh_token='1//04GjkX_q30ioyCgYIARAAGAQSNwF-L9IrKggdZo-Pq9E_t3qEbmhHmQe8JVVDW4vaXGoeFiRxxJ5KRu7f7TpQVDsHrwMdmc7Xqn4';
        grant_type="refresh_token";

    }
    #Makes a request to obtain the access token
    $refresh_token = Invoke-RestMethod -Uri "https://www.googleapis.com/oauth2/v4/token" -Method POST -Body $refresh_body

    $access_token = $refresh_token.access_token    
}

的地方是我尝试

$document = Invoke-RestMethod -Headers @{Authorization = "Bearer $access_token"} -ContentType "application/json" -Method GET -Uri "https://docs.googleapis.com/v1/documents/1K8_q_VCTWin_s8aVb0D16DNujqA72eravHojMfM9cyo" 

$revId = $document.revisionId

$updateObject = {
  requests = [
    {
      insertText= {
        text= "The Red Dog Crosses the Road";
        endOfSegmentLocation= {
          segmentId= ""
        }
      }
    }
  ]
  writeControl= {
    requiredRevisionId= $revId;
  }
}


Invoke-RestMethod -Headers @{Authorization = "Bearer $access_token"} -ContentType "application/json" -Method POST -Uri "https://docs.googleapis.com/v1/documents/1K8_q_VCTWin_s8aVb0D16DNujqA72eravHojMfM9cyo:batchUpdate" -Body $updateObject

在此尝试更新文档的地方。这个请求。

Invoke-RestMethod : The remote server returned an error: (400) Bad Request.
At line:39 char:1
+ Invoke-RestMethod -Headers @{Authorization = "Bearer $access_token"}  ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebExceptio 
   n
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

我认为我只需要找到正确的位置,即可将我的 batchupdate 对象放在请求中,但是我可以完全关闭。任何帮助都将不胜感激。

I have a powershell script currently that pulls in data from a Google Sheet on a team drive and then creates Active Directory users in our environment based on that data. I haven't gotten to fully test this part yet, but don't believe it will cause me any problems.

Where I am having issues is I would like to use a Team-Shared Google Drive Document to contain all of my logging for this automation.

I have had no issues authenticating but cannot seem to get the POST request correct for batchUpdating the document itself. I am able to obtain the revision Id without any problem. I cannot seem to find out where to pass the "batchUpdate" object in my request. I am not worried about where the text is stored on the page once it is added, just looking to add the text to the file.

Documentation:
https://developers.google.com/docs/api/reference/rest/v1/documents/batchUpdate#http-request

Here is how I am obtaining my access token

function Get-Auth{
    #Obtains access token for the Google API's
    #Runs everytime to ensure there is a fresh access token available to the service account
    $refresh_body = @{

        client_id='338139966542-qaa4me7l4fs0l1ltl2e6kjidjr9tf3up.apps.googleusercontent.com';
        client_secret='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';
        refresh_token='1//04GjkX_q30ioyCgYIARAAGAQSNwF-L9IrKggdZo-Pq9E_t3qEbmhHmQe8JVVDW4vaXGoeFiRxxJ5KRu7f7TpQVDsHrwMdmc7Xqn4';
        grant_type="refresh_token";

    }
    #Makes a request to obtain the access token
    $refresh_token = Invoke-RestMethod -Uri "https://www.googleapis.com/oauth2/v4/token" -Method POST -Body $refresh_body

    $access_token = $refresh_token.access_token    
}

Here is where I am attempting to update the document

$document = Invoke-RestMethod -Headers @{Authorization = "Bearer $access_token"} -ContentType "application/json" -Method GET -Uri "https://docs.googleapis.com/v1/documents/1K8_q_VCTWin_s8aVb0D16DNujqA72eravHojMfM9cyo" 

$revId = $document.revisionId

$updateObject = {
  requests = [
    {
      insertText= {
        text= "The Red Dog Crosses the Road";
        endOfSegmentLocation= {
          segmentId= ""
        }
      }
    }
  ]
  writeControl= {
    requiredRevisionId= $revId;
  }
}


Invoke-RestMethod -Headers @{Authorization = "Bearer $access_token"} -ContentType "application/json" -Method POST -Uri "https://docs.googleapis.com/v1/documents/1K8_q_VCTWin_s8aVb0D16DNujqA72eravHojMfM9cyo:batchUpdate" -Body $updateObject

Here is the error I receive when trying to make this request.

Invoke-RestMethod : The remote server returned an error: (400) Bad Request.
At line:39 char:1
+ Invoke-RestMethod -Headers @{Authorization = "Bearer $access_token"}  ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebExceptio 
   n
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

I think I just need to find the correct place to put my batchUpdate object in the request, but I could be completely off. Any help would be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疾风者 2025-02-12 15:25:18

在您的脚本中,下面的修改如何?我认为您的问题的原因是由于 $ updateObject 的值以及 boby $ updateObject 的请求主体。在这种情况下,请求主体必须为字符串值。我认为这可能是您问题的原因。

修改后的脚本:

$document = Invoke-RestMethod -Headers @{Authorization = "Bearer $access_token"} -Method GET -Uri "https://docs.googleapis.com/v1/documents/1K8_q_VCTWin_s8aVb0D16DNujqA72eravHojMfM9cyo"
$revId = $document.revisionId
$updateObject = @{
  requests = @(
    @{
      insertText = @{
        text = "The Red Dog Crosses the Road";
        endOfSegmentLocation = @{
          segmentId = "";
        }
      }
    }
  )
  writeControl = @{
    requiredRevisionId = $revId;
  }
}
Invoke-RestMethod -Headers @{Authorization = "Bearer $access_token"} -ContentType "application/json" -Method POST -Uri "https://docs.googleapis.com/v1/documents/1K8_q_VCTWin_s8aVb0D16DNujqA72eravHojMfM9cyo:batchUpdate" -Body (ConvertTo-Json -Depth 4 $updateObject)

注意:

  • 运行此脚本时,我确认“红狗越过道路”的文本将附加到Google文档上。

参考:

In your script, how about the following modification? I thought that the reason of your issue is due to the value of $updateObject and the request body of -Body $updateObject. In this case, the request body is required to be the string value. I thought that this might be the reason of your issue.

Modified script:

$document = Invoke-RestMethod -Headers @{Authorization = "Bearer $access_token"} -Method GET -Uri "https://docs.googleapis.com/v1/documents/1K8_q_VCTWin_s8aVb0D16DNujqA72eravHojMfM9cyo"
$revId = $document.revisionId
$updateObject = @{
  requests = @(
    @{
      insertText = @{
        text = "The Red Dog Crosses the Road";
        endOfSegmentLocation = @{
          segmentId = "";
        }
      }
    }
  )
  writeControl = @{
    requiredRevisionId = $revId;
  }
}
Invoke-RestMethod -Headers @{Authorization = "Bearer $access_token"} -ContentType "application/json" -Method POST -Uri "https://docs.googleapis.com/v1/documents/1K8_q_VCTWin_s8aVb0D16DNujqA72eravHojMfM9cyo:batchUpdate" -Body (ConvertTo-Json -Depth 4 $updateObject)

Note:

  • When this script is run, I confirmed that the text of "The Red Dog Crosses the Road" is appended to the Google Document.

Reference:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文