Skip to main content

I can’t seem to figure out why Zapier keeps re-authenticating even though there aren’t any 401 errors. You can see that the test request comes back consistently with a 200 response, but is followed by new authentication requests. I’m hitting rate limits on the /api-token-auth/ endpoint because of this. Any ideas on what might be going on?

 

And here’s the code:

 

const test = async (z, bundle) => {

  const options = {

    url: `https://api.opensolar.com/api/orgs/${bundle.authData.orgId}`,

    method: 'GET',

    headers: {

      Authorization: `Bearer ${bundle.authData.sessionKey}`,

    },

  };

  return z.request(options).then((response) => {

    response.throwForStatus();

    const results = response.json;

    // You can do any parsing you need for results here before returning them

    return results;

  });

};

const perform = async (z, bundle) => {

  const options = {

    url: 'https://api.opensolar.com/api-token-auth/',

    method: 'POST',

    headers: {

      'content-type': 'application/json',

      accept: 'application/json',

    },

    body: {

      username: bundle.authData.username,

      password: bundle.authData.password,

    },

  };

  return z.request(options).then((response) => {

    response.throwForStatus();

    const results = response.json;

    return z

      .request({

        url: `https://api.opensolar.com/auth/users/${results.user.id}/`,

        method: 'PATCH',

        headers: {

          'Content-Type': 'application/json',

          Authorization: `Bearer ${results.token}`,

        },

        body: {

          is_machine_user: true,

        },

      })

      .then((r) => {

        const res = r.json;

        return {

          sessionKey: results.token,

          orgId: results.org_id,

        };

      });

  });

};

module.exports = {

  type: 'session',

  test: test,

  fields: <

    {

      computed: false,

      key: 'username',

      required: true,

      label: 'Username',

      type: 'string',

    },

    {

      computed: false,

      key: 'password',

      required: true,

      label: 'Password',

      type: 'password',

    },

  ],

  sessionConfig: { perform: perform },

  connectionLabel: '{{bundle.authData.username}}',

};