Search code examples
node.jssurveymonkey

Survey Monkey API Node server GET request returns Unauthorized 401 error


I'm building a small server using SurveyMonkey's API and I'm getting a 401 status error. The response is only returning the following:

  {
    "size": 0,
    "timeout": 0
  }

Here is the code I am using currently for the route.

const router = require("express").Router();
const fetch = require("node-fetch");
const TOKEN = process.env.SM_ACCESS_TOKEN;
const BASEURL = process.env.SM_BASEURL;
const options = {
  headers: {
    Authorization: `Bearer ${TOKEN}`,
    "Content-Type": "application/json"
  }
};

/*
GET a list of surveys
*/
router.get("/", async (req, res) => {
  try {
    const surveys = await fetch(`${BASEURL}surveys`, options);
    if (surveys) {
      return res.status(200).json(surveys);
    }
  } catch (err) {
    console.log(err);
    res.status(500).send({ message: "Server error", err });
  }
});

Some extra information: 1. The env variables are coming through fine. 2. I have manually created one survey through surveymonkey. 3. I haven't done anything with their oAuth system yet, other than create my own account. 4. BASEURL = "https://api.surveymonkey.com/v3/"


Solution

  • Turned out that my issue was related to the bad wording in their dev app Settings. I'm creating a private app so I don't require Oauth, just the Token that I get from my app. This however means that I still needed to go into the apps settings and check "Required" on all of the scopes. The wording they use is centralized around needing this if you're using Oauth which doesn't make sense for someone who is not. Changing the scopes however fixed my issue.