title: Cloud Functions for Firebase - subscribe a user to a topic
body: <p>Hi there I want to subscribe a user to a specific topic using Cloud Functions for Firebase in a Node.js environment, I've followed this video <a href="https://www.youtube.com/watch?v=GNR9El3XWYo" rel="nofollow noreferrer">https://www.youtube.com/watch?v=GNR9El3XWYo</a>, where a Firebase expert shows how to subscribe a user when a write to a node in Firebase is made, here´s my complete code:</p>
  
  <pre><code>//For subscriptions
  exports.privateEventSubs = functions.database.ref("Events/{eventID}/guest_mod/assistants/{guestEmail}").onWrite((event) =&gt; {
      let data = event.data;
      let eventID = data.ref.parent.parent.parent.key;
      console.log("The eventID is: " + eventID);
      let guestEmail = data.ref.key;
      console.log("The guestEmail is: " + guestEmail);
  
      //Add or remove a subscription
      let action = data.exists() ? "batchAdd" : "batchRemove";
      console.log("The action is: " + action);
  
  
      //Get the device token from each user
      admin.database().ref("Users/" + guestEmail + "/chat_data/firebaseToken").once("value").then(function (snapshot) {
          console.log("The user token is: " + snapshot.val());
  
          //Send messages to users that have subscribed to that event
          functions.config().firebase.credential.getAccessToken().then(function (oauthToken) {
              console.log("The oauthToken is: " + oauthToken.access_token);
              //Lets configure and request
              request({
                  url: "https://iid.googleapis.com/iid/v1:" + action, //URL to hit
                  method: 'POST',
                  json: true,
                  headers: {
                      Authorization: "Bearer " + oauthToken.access_token,
                      access_token_auth: true,
                  },
                  body: {
                      to: "/Events/" + eventID,
                      registration_tokens: snapshot.val()
                  }
              }, function (err, res, body) {
                  if (err) {
                      console.log(err);
                  } else {
                      console.log(res.statusCode, body);
                  }
              });
          });
      });
  });
  </code></pre>
  
  <p>The error I get is this one: </p>
  
  <pre><code>   400 { error: 'InvalidListOfTokens' }
  </code></pre>
  
  <p>What am I doing wrong? Please help! :(</p>