Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create watch as notification for response in Google Forms #346

Open
cklat opened this issue Jul 28, 2022 · 0 comments
Open

Create watch as notification for response in Google Forms #346

cklat opened this issue Jul 28, 2022 · 0 comments
Assignees

Comments

@cklat
Copy link

cklat commented Jul 28, 2022

Hi!

I'm trying to reproduce a sample in AppsScript that has been published in the Google Documentation for python and Node.
It is this method: https://developers.google.com/forms/api/guides/push-notifications#create_a_watch

So basically, I have a topic created in the Cloud Pub/Sub feature for my project and want to have some kind of notification process as when a new response of Google Form has been submitted. I cannot use an email notification but I'm open to any other solution that provides me the functionality of triggering a subprocess of a Node app.

Here's the use case: For MVP reasons and getting quick results, I have embedded the form as an iframe in my Node app. When users complete the form I need to start a new process with the information provided in the form as the input.
For the time being, I thought about having set up an AppsScript in the form that publishes to a Pub/Sub topic to which the app is subscribed.

However, using the following code:

 function createWatch() {
   Logger.log('Calling the Forms API!');
   var formId = FORMID';

   // Get OAuth Token
  var OAuthToken = ScriptApp.getOAuthToken();
  Logger.log('OAuth token is: ' + OAuthToken);
  var formsAPIUrl = 'https://forms.googleapis.com/v1beta/forms/' + formId + '/' + 'watches';
  Logger.log('formsAPIUrl is: ' + formsAPIUrl);
  var watchRequest = JSON.stringify({
    "watch": {
        "target": {
          "topic": {
            "topicName": "projects/xxx/topics/xxx"
          }
        },
        "eventType": "RESPONSES"
      }
    });
  var options = {
     'headers': {
       Authorization: 'Bearer ' + OAuthToken,
       Accept: 'application/json'
     },
     'method': 'post',
     'muteHttpExceptions': true,
     'payload': watchRequest
   };  

  var response = UrlFetchApp.fetch(formsAPIUrl, options);
  Logger.log('Response from forms.responses was: ' + response);
}

... I get the following response:

{
  "error": {
    "code": 400,
    "message": "Invalid JSON payload received. Unknown name \"{\"watch\":{\"target\":{\"topic\":{\"topicName\":\"projects/fachmarkt-manager/topics/employee-onboarding\"}},\"eventType\":\"RESPONSES\"}}\": Cannot bind query parameter. Field '{\"watch\":{\"target\":{\"topic\":{\"topicName\":\"projects/fachmarkt-manager/topics/employee-onboarding\"}},\"eventType\":\"RESPONSES\"}}' could not be found in request message.",
    "status": "INVALID_ARGUMENT",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.BadRequest",
        "fieldViolations": [
          {
            "description": "Invalid JSON payload received. Unknown name \"{\"watch\":{\"target\":{\"topic\":{\"topicName\":\"projects/fachmarkt-manager/topics/employee-onboarding\"}},\"eventType\":\"RESPONSES\"}}\": Cannot bind query parameter. Field '{\"watch\":{\"target\":{\"topic\":{\"topicName\":\"projects/fachmarkt-manager/topics/employee-onboarding\"}},\"eventType\":\"RESPONSES\"}}' could not be found in request message."
          }
        ]
      }
    ]
  }
}

I have also tried to drop the stringify of the payload but it results in the same error.

Any chances of fixing this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants