REST API failure: Invoke-WebRequest : 404

半腔热情 提交于 2020-04-15 17:18:17

问题


The Powershell code bellow writes and reads values to Google Sheets (works fine) and should run the function myfunction in an Apps Script project using API, but Invoke-WebRequest returns the error bellow:

Invoke-WebRequest : 404. That’s an error. The requested URL /v1/scripts/=ya29.a0Ae4lvC3k8aahOCPBgf-tRf4SRFxdcCE97fkbXLAJqZ4zRCLnBp9prwEcBYBAf
lYP6zyW3fLeD3u4iSw5jYtDAdgZiSsTjzQbCpj9e_ahCA0xwC_1NBTjYkPwqFdLli7LNpfFcuedFDhdUpfnKTRZdbBWIf2ZyxyuGc6p was not found on this server. That’s 
all we know.
No C:\Users\F76254C\Desktop\Nova pasta\Batch files\Available Projects\Latam HIL Lab Menu\libs\Google\WriteToGoogleSheets.ps1:64 caractere:13
+     $resp = Invoke-WebRequest -Uri "https://script.googleapis.com/v1/ ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

I am not sure if the JSON representation of request body is set correctly or if the error was caused for something else.

Powershell Code:

function doit{
    $json = ".\client_id.json"
    $jdata = get-content $json | convertfrom-json
    <#
    $jdata | ForEach-Object {
        $_.PSObject.Properties.Value
    }
    #>
    $ClientID = $jdata.web.client_id.ToString()
    $ClientSecret = $jdata.web.client_secret.ToString()
    $refreshToken = "1//04VvG_FTyDGhiCgYIARAAGAQSNwF-L9IrZ-o1kaZQQccvzL5m4TUTNz6b9Q4KCb16t4cH11gGCshWZWvgaCoMlg73FgpLAGOYTEk" 
    $grantType = "refresh_token" 
    $requestUri = "https://accounts.google.com/o/oauth2/token" 
    $GAuthBody = "refresh_token=$refreshToken&client_id=$ClientID&client_secret=$ClientSecret&grant_type=$grantType" 
    $GAuthResponse = Invoke-RestMethod -Method Post -Uri $requestUri -ContentType "application/x-www-form-urlencoded" -Body $GAuthBody


    $accessToken = $GAuthResponse.access_token

    $headers = @{"Authorization" = "Bearer $accessToken"          

                  "Content-type" = "application/json"}

    $DocumentID = "1htbeGlqZ4hojQBWl9fxE4nW_KZI9uVwi0ApzNOIbwnY"

    $currentDate = (Get-Date).ToString('MM/dd/yyyy')
    $currentTime = (Get-Date).ToString('HH:mm:sstt')

$json = @”
{
    "range": "HIL_APP!A1:G1",
    "majorDimension": "ROWS",
    "values":
                [[
                    "HIL_NAME",
                    "$env:ComputerName",
                    "$currentDate",
                    "$currentTime",
                    "$env:UserName",
                    "input from user",
                    "attempt"
                ],]
}
“@

    $write = Invoke-WebRequest -Uri "https://sheets.googleapis.com/v4/spreadsheets/$DocumentID/values/HIL_APP!A1:G1:append?valueInputOption=USER_ENTERED&access_token=$($accessToken)" -Method Post -ContentType "application/json" -Body $json
    $read = Invoke-WebRequest -Uri "https://sheets.googleapis.com/v4/spreadsheets/$DocumentID/values/HIL_APP!A1:G1?access_token=$($accessToken)"
    Write-Output "Response: " ($read.Content | ConvertFrom-Json)

$scriptId = "1eF7ZaHH-pw2-AjnRVhOgnDxBUpfr0wALk1dVFg7B220bg_KuwVudbALh"

$json = @”
{
  "function": "myfunction",
  "parameters": [
    "attempt" string
  ],
  "devMode": true
}
“@

    $resp = Invoke-WebRequest -Uri "https://script.googleapis.com/v1/scripts/$scriptId:run?access_token=$($accessToken)" -Method Post -ContentType "application/json" -Body $json
#    Write-Output "Response: " ($resp.Content | ConvertFrom-Json)
}

clear

doit

EDIT:

Google App Script code:

function toSpreadsheet(text2write)
  {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("HIL_APP");

  for (var i = 1; i < sheet.getLastRow(); i++)
  {
    sheet.getRange(i+1, 8, 1).setValue(text2write)
  }
  return "myreturn"
}

function myfunction(params)
{
  toSpreadsheet(params)
}

回答1:


  • You could confirm that the script for writing and reading values for Google Sheets worked fine.
  • You want to modify only the script for running the Google Apps Script using Apps Script API.
    • You have already been able to use Apps Script API.
    • Your access token can be used for running the Google Apps Script.
  • You want to achieve this using Invoke-WebRequest of powershell.

If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

Modification points:

  • From your error message and your script, I would like to propose the following modification points.
    1. From "https://script.googleapis.com/v1/scripts/$scriptId:run?access_token=$($accessToken)" to "https://script.googleapis.com/v1/scripts/${scriptId}:run"
      • In your script, the endpoint is https://script.googleapis.com/v1/scripts/. This is incomplete endpoint.
      • I think that the reason of your current error message is due to this.
    2. Please use the access token at the request header instead of the query parameter. Ref
      • I think that this can be also said for using Sheets API.
    3. I think that "attempt" string is "attempt".
    4. Please modify to ".

Modified script:

When the request to Apps Script API in your script is modified, it becomes as follows.

$scriptId = "1eF7ZaHH-pw2-AjnRVhOgnDxBUpfr0wALk1dVFg7B220bg_KuwVudbALh"

$json = @"
{
  "function": "myfunction",
  "parameters": ["attempt"],
  "devMode": true
}
"@

$resp = Invoke-WebRequest -Uri "https://script.googleapis.com/v1/scripts/${scriptId}:run" -Method Post -ContentType "application/json" -Body $json -Headers @{"Authorization"="Bearer ${accessToken}"}

Note:

  • In my environment, I could confirm that above modified script worked. Unfortunately, I cannot understand about your flow for setting to run the Google Apps Script with Apps Script API. So if in your environment, an error occurs, please confirm the settings for running the script with Apps Script API, again.
  • I think that "Bearer ${accessToken}" can be also modified to "Bearer $accessToken".

Reference:

  • Executing Functions using the Apps Script API

If I misunderstood your question and this was not the direct solution of your issue, I apologize.



来源:https://stackoverflow.com/questions/60942605/rest-api-failure-invoke-webrequest-404

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!