1

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)
}
Cleber Marques
  • 426
  • 5
  • 20

1 Answers1

3
  • 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.

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:

halfer
  • 19,824
  • 17
  • 99
  • 186
Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • I did the changes you suggested, and previously my code was deployed as Web App, but now it is deployd as API Executable, so I stopped getting the error, but still not work. the function `myfunction` on google script project is not triggered my the Powershell code. The function `myfunction` on Google Script should write to a Google Sheet and it does not, but it does work when I run the function it directly on the Google App Script Dashboard. I have inserted the scope [link]https://www.googleapis.com/auth/spreadsheetsComments[link] and it seems to me only this scope should be enough – Cleber Marques Apr 05 '20 at 17:06
  • @Cleber Marques Thank you for replying. Unfortunately, from your replying and question, I cannot understand about your detail situation. This is due to my poor skill. I deeply apologize for this. I think that at first, in order to confirm that your settings for Apps Script API, how about testing to run using a simple Google Apps Script? Because your question is to run the function with Apps Script API. So for example, `function myFunction() {return "ok"}`. When `ok` is returned, it indicates that your setting is correct. How about this? – Tanaike Apr 05 '20 at 21:43
  • 1
    Your suggestion is great! I have done that and worked right. It means the communication between powershell and GAS is fine. I have created another post to discuss about the issue w writing to Spreadhsheet, so everyone can follow from there is the link: https://stackoverflow.com/questions/61049670/how-to-authorize-scope-for-gas-to-edit-google-spreadsheets?noredirect=1#comment108005449_61049670 – Cleber Marques Apr 05 '20 at 22:20
  • @Cleber Marques Thank you for replying. I'm glad that you could use Apps Script API. I would like to think of your new question. – Tanaike Apr 05 '20 at 22:22