# Automating preauthorization using the API

**URL:** <https://hub.mender.io/t/automating-preauthorization-using-the-api/3152>\
**Category:** General Discussions\
**Tags:** raspberry-pi-4, preauthorization\
**Created:** [January 28, 2021, 10:38pm UTC](https://hub.mender.io/t/automating-preauthorization-using-the-api/3152 "2021-01-28T22:38:00Z")\
**Posts on this page:** 5\
**Page:** 3

<div class="post-metadata">

**Author:** ![bradw](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/bradw/32/933_2.png) [@bradw](https://hub.mender.io/u/bradw)\
**Post date:** [February 17, 2021, 4:03pm UTC](https://hub.mender.io/t/automating-preauthorization-using-the-api/3152/41 "2021-02-17T16:03:20Z")

</div>

```
const axios = require('axios');
const functions = require('firebase-functions');

module.exports = functions.pubsub.topic('menderPreauthorize').onPublish((messag=> {
  const deviceId = message.data ? Buffer.from(message.data, 'base64').toString() : null;
  let response
  if (deviceId) {
    const sn = deviceId.split('y')[1]
    const inputBody = {
      "identity_data": {
      "sku": deviceId,
      "sn": sn
    },
      "pubkey": process.env.MENDER_PUBKEY,
    };
    const headers = {
      'content-type':'application/json',
      'Accept':'application/json',
      'Authorization':`Bearer ${process.env.MENDER_API_KEY}` // JWT from $(curl -X POST -u $MENDER_SERVER_USER $MENDER_SERVER_URI/api/management/v1/useradm/auth/login)
    };
    response = axios({
      method: 'post',
      url: 'https://hosted.mender.io/api/management/v2/devauth/devices',
      body: JSON.stringify(inputBody), // I've tried data here as well per axios docs and both with and without JSON.stringify() 
      headers: headers
    })
    .then(res => {
        const id = res.headers.get('Location').substring(res.headers.get('Location').indexOf('/') + 1);
        console.log(`deviceId: ${id}`);
        return Promise.resolve(id);
    })
    .catch((err) => {
      if (err.response) {
        console.log(`error data: ${JSON.stringify(err.response.data)}`)
        console.log(`error status: ${err.response.status}`)
        console.log(`error headers: ${JSON.stringify(err.response.headers)}`)
      } else if (err.request) {
        console.log(`error response: Request was made but no response recieved, ${err.request}`)
      } else {
        console.log(`Error: ${err.message}`)
      }
   })
  } else {
    response = 'deviceId was null.'
    console.log('deviceId was null')
  }
  return response
})
```

---

<div class="post-metadata">

**Author:** ![tranchitella](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/tranchitella/32/606_2.png) [@tranchitella](https://hub.mender.io/u/tranchitella)\
**Post date:** [February 17, 2021, 6:28pm UTC](https://hub.mender.io/t/automating-preauthorization-using-the-api/3152/42 "2021-02-17T18:28:47Z")

</div>

This request works for me:

```
const axios = require('axios');

function test() {
  let response
    const inputBody = {
      "identity_data": {
        "sku": "001",
        "sn": "001"
      },
      "pubkey": "-----BEGIN PUBLIC KEY-----\n...\n-----END PUBLIC KEY-----\n"
    };
    const headers = {
      'Accept':'application/json',
      'Authorization':`Bearer ....`
    };
    response = axios({
      method: 'post',
      url: 'https://hosted.mender.io/api/management/v2/devauth/devices',
      data: inputBody,
      headers: headers
    })
    .then(res => {
        const id = res.headers.get('Location').substring(res.headers.get('Location').indexOf('/') + 1);
        console.log(`deviceId: ${id}`);
        return Promise.resolve(id);
    })
    .catch((err) => {
      if (err.response) {
        console.log(`error data: ${JSON.stringify(err.response.data)}`)
        console.log(`error status: ${err.response.status}`)
        console.log(`error headers: ${JSON.stringify(err.response.headers)}`)
      } else if (err.request) {
        console.log(`error response: Request was made but no response recieved, ${err.request}`)
      } else {
        console.log(`Error: ${err.message}`)
      }
   })
  return response
}

test()
```

---

<div class="post-metadata">

**Author:** ![bradw](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/bradw/32/933_2.png) [@bradw](https://hub.mender.io/u/bradw)\
**Post date:** [February 17, 2021, 10:16pm UTC](https://hub.mender.io/t/automating-preauthorization-using-the-api/3152/43 "2021-02-17T22:16:56Z")

</div>

@tranchitella @drewmoseley ,

I copy and pasted @tranchitella’s call to my Firebase Function, modifying it only where I grab the `pubkey` and `Authorization` I added some `console.log` just to be certain of what is being passed in from Firebase Functions’ `process.env`. I also made the call to the API successfully using `curl` with the `JWT` and `pubkey` I’m using here. I still get the same `401 Unauthorized` error. See the modified code and Firebase log output below.

```
const axios = require('axios');
const functions = require('firebase-functions');

module.exports = functions.pubsub.topic('menderPreauthorize').onPublish((message) => {
  const deviceId = message.data ? Buffer.from(message.data, 'base64').toString() : null;
  console.log(`deviceId: ${deviceId}`)
  let response
  if (deviceId) {
    const sn = deviceId.split('y')[1]
    console.log(`sn: ${sn}`)
    console.log(`MENDER_PUBKEY typeof: ${typeof process.env.MENDER_PUBKEY}`)
    console.log(`MENDER_API_KEY typeof: ${typeof process.env.MENDER_API_KEY}`)
    const inputBody = {
      "identity_data": {
        "sku": deviceId,
        "sn": sn
      },
      "pubkey": process.env.MENDER_PUBKEY
    };
    const headers = {
      'Accept':'application/json',
      'Authorization': `Bearer ${process.env.MENDER_API_KEY}`
    };
    response = axios({
      method: 'post',
      url: 'https://hosted.mender.io/api/management/v2/devauth/devices',
      data: inputBody,
      headers: headers
    })
    .then(res => {
        const id = res.headers.get('Location').substring(res.headers.get('Location').indexOf('/') + 1);
        console.log(`deviceId: ${id}`);
        return Promise.resolve(id);
    })
    .catch((err) => {
      if (err.response) {
        console.log(`error data: ${JSON.stringify(err.response.data)}`)
        console.log(`error status: ${err.response.status}`)
        console.log(`error headers: ${JSON.stringify(err.response.headers)}`)
      } else if (err.request) {
        console.log(`error response: Request was made but no response recieved, ${err.request}`)
      } else {
        console.log(`Error: ${err.message}`)
      }
   })
 } else {
   response = null
 }
  return response
})

```

 ![Screen Shot 2021-02-17 at 2.05.22 PM](https://canada1.discourse-cdn.com/flex036/uploads/mender/original/2X/5/5f58c049c8626779f453b29011dc6252c68bd3f0.png)

---

<div class="post-metadata">

**Author:** ![tranchitella](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/tranchitella/32/606_2.png) [@tranchitella](https://hub.mender.io/u/tranchitella)\
**Post date:** [February 18, 2021, 4:16am UTC](https://hub.mender.io/t/automating-preauthorization-using-the-api/3152/44 "2021-02-18T04:16:14Z")

</div>

Sorry @bradw, I don’t know how to further help you dig into this issue. Does my code work for you locally? I’m not using process.env, I simply hard-coded the values in the script. I’m not a Javascript ninja, thus I don’t know if there can be any issue in getting those from env variables… If the very same code (excluding the “functions” and pubsub, which is firebase specific, works locally but not in firebase, then it must be something platform-specific.

---

<div class="post-metadata">

**Author:** ![bradw](https://yyz2.discourse-cdn.com/flex036/user_avatar/hub.mender.io/bradw/32/933_2.png) [@bradw](https://hub.mender.io/u/bradw)\
**Post date:** [February 18, 2021, 4:29pm UTC](https://hub.mender.io/t/automating-preauthorization-using-the-api/3152/45 "2021-02-18T16:29:04Z")

</div>

I hard coded the `MENDER_PUBKEY` and `MENDER_API_KEY` in my Firebase Functions call and it worked. So it is clearly an issue with the way the variables are being brought in from `process.env`. I will reach out to Firebase support about it and if I don’t get a good answer there I’ll post a question on Stack Overflow.  
Either way I’ll post the solution back here for anyone else who runs across this. Thanks for all the help!

[Previous page](https://hub.mender.io/t/automating-preauthorization-using-the-api/3152.md?page=2)
