stackunderflow
stackunderflow

Reputation: 1774

Submit Google Form via Google Apps Script giving 401

I try to submit Google Form using Google Apps Script. I found this tutorial and follow the instructions properly. Below is the code with a sample of public link I've generated from the Google Form with parameters added.

var url = "https://docs.google.com/forms/d/e/" + formid + "/formResponse?usp=pp_url&entry.1195595956=" + name + "&entry.1162154798=" + age + "&entry.1012135063=" + gender;
return UrlFetchApp.fetch(url).getContent();

But when I try to run it, I get below error

Exception: Request failed for https://docs.google.com returned code 401. Truncated server response: body{height:100%;margin:0;width:100%}@media (max-height:350px){.button{font-size:10px}.button-... (use muteHttpExceptions option to examine full response)

Upvotes: 0

Views: 52

Answers (1)

4thAnd1
4thAnd1

Reputation: 856

Submit a Google form response using Apps script

I managed to replicate the same error that you were getting by using an unpublished Google Form, in order for a response to be sent, you must first make sure that the form is published and that it is shared to your target responders. To publish and share your form, you may check this official support article

Sample Code

function sendViaScript() {

  var formid = "<your-form-id-here>";
  var name = "Bob";
  var age = "34";
  var gender = "Male";
  var plink = "https://docs.google.com/forms/d/e/"+formid+"/formResponse?usp=pp_url&entry.794469650="+name+"&entry.792476998="+age+"&entry.170348228="+gender;
  return UrlFetchApp.fetch(plink).getContent();
}

Sample Output

Timestaamp Name Age Gender
2/28/2025 16:48:59 Bob 34 Male

References:

Publish & share your form with responders

Upvotes: 2

Related Questions