# Homeyscript for bearer token

**URL:** <https://community.homey.app/t/homeyscript-for-bearer-token/47819>\
**Category:** Questions & Help\
**Created:** [May 26, 2021, 7:07pm UTC](https://community.homey.app/t/homeyscript-for-bearer-token/47819 "2021-05-26T19:07:00Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Le\_Cactus](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/le_cactus/32/9727_2.png) [@Le\_Cactus](https://community.homey.app/u/Le_Cactus)\
**Post date:** [May 26, 2021, 7:07pm UTC](https://community.homey.app/t/homeyscript-for-bearer-token/47819/1 "2021-05-26T19:07:01Z")

</div>

Hi,

I have a Homey script that I use to fetch my _Bearer Token_, I need that for the “Realtime Homey logger” and “Flow Viewer” webpages made by irritanterik.

I found the script through this wonderfull website: [Getting a bearer token - Homey (solweb.no)](https://homey.solweb.no/advanced-api-usage/bearertoken)

This worked flawlessly for ages, but now when I run it on the my.homey.app (the original homeyscript.homey.app page automaticly redirects you to my.homey.app) it gives me an error.

Anyone is having the same problem?  
Anyone knows of another (easy) way to get the bearer token?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![HansieNL](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/hansienl/32/10634_2.png) [@HansieNL](https://community.homey.app/u/HansieNL)\
**Post date:** [May 26, 2021, 7:45pm UTC](https://community.homey.app/t/homeyscript-for-bearer-token/47819/2 "2021-05-26T19:45:41Z")

</div>

- Go to website: [Homey Developer Tools](https://https://tools.developer.homey.app/tools/system)
- In browser open Web Console or use F12
- Select all in Web Console to view all data
- Login to Homey Developer Tools
- Use search option in Web Console to find bearer
- You’ll find **authorization: Bearer 123…789** and can copy it to your config.js

---

<div class="post-metadata">

**Author:** ![Le\_Cactus](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/le_cactus/32/9727_2.png) [@Le\_Cactus](https://community.homey.app/u/Le_Cactus)\
**Post date:** [May 26, 2021, 8:25pm UTC](https://community.homey.app/t/homeyscript-for-bearer-token/47819/3 "2021-05-26T20:25:11Z")

</div>

Thanks Hansie,

I am aware of that method, but was hoping for a simpler solution. The script was awsome; with a flow I would get my token automaticaly. (/me is lazy 😉 )

---

<div class="post-metadata">

**Author:** ![Dijker](https://avatars.discourse-cdn.com/v4/letter/d/b9bd4f/32.png) [@Dijker](https://community.homey.app/u/Dijker)\
**Post date:** [May 26, 2021, 9:01pm UTC](https://community.homey.app/t/homeyscript-for-bearer-token/47819/4 "2021-05-26T21:01:55Z")

</div>

Nice Script! haven’t tested it yet.  
Did you maybe enable 2 factor authentication?

---

<div class="post-metadata">

**Author:** ![Le\_Cactus](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/le_cactus/32/9727_2.png) [@Le\_Cactus](https://community.homey.app/u/Le_Cactus)\
**Post date:** [May 26, 2021, 9:43pm UTC](https://community.homey.app/t/homeyscript-for-bearer-token/47819/5 "2021-05-26T21:43:49Z")

</div>

No I didn’t.

I agree it’s a very nice, even ingenious script Andreas made. I guess Athom changed something to the [accounts.athom.com/authorise](http://accounts.athom.com/authorise) page (maybe due to the 2FA solution) but in my memory I has still worked for some time after 2FA was introduced.

After looking at the script again I doubt that it has anything to do with the redirection to my.homey.app.  
I’ve taken another look at it, but it is beyond my skills to repair the script 😓

Hansie’s method will do of course, but the script was an easy way to automaticaly update the bearer token in the config.js of the realtime logger config.js.

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [May 27, 2021, 5:34am UTC](https://community.homey.app/t/homeyscript-for-bearer-token/47819/6 "2021-05-27T05:34:36Z")

</div>

It looks like the issue is with `fetch()` behaving differently.

Here’s a fixed version:

```auto
// -------- o - Configure these parameters -------- o -------- o 
let email = 'REPLACEME'
let password = 'REPLACEME'
let client_id = REPLACEME
let client_secret = 'REPLACEME'
let redirect_url = 'http://localhost'
let cloudid = 'REPLACEME'
// -------- o -------- o -------- o -------- o -------- o 

const between = function(str, strf, strt) {
    return str.split(strf).pop().split(strt)[0].trim();
}

const authurl = 'https://accounts.athom.com/login'
console.log("POST authentication " + authurl)
const response2 = await fetch(authurl, {
  "headers": {
    "accept": "application/json, text/javascript, */*; q=0.01",
    "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
  },
  "referrerPolicy": "no-referrer-when-downgrade",
  "body": 'email=' +encodeURIComponent(email) + '&password=' + encodeURIComponent(password) + '&otptoken=',
  "method": "POST",
  "mode": "cors",
  "credentials": "omit"
})
const body2 = await response2.text()
const token = JSON.parse(body2)

const authorizeurl = 'https://accounts.athom.com/oauth2/authorise?client_id=' + client_id + 
    '&redirect_uri=' + encodeURIComponent(redirect_url) + '&response_type=code&user_token=' + token.token

console.log(" Response from accounts.athom.com/login ", body2)
console.log("GET Authorization " + authorizeurl)

const response3 = await fetch(authorizeurl, {
  "headers": {
  },
  "method": "GET",
  "mode": "cors",
  "credentials": "include"
})
const body3 = await response3.text()
let csrf = between(body3, 'name="_csrf" value="', '">')
let cookiecsrf = null;
let raw = response3.headers.raw()['set-cookie'];
for (let cookie of raw) {
  if (cookie.startsWith('_csrf=')) {
    cookiecsrf = cookie.match(/=(.+?);/)[1];
    break;
  }
}

let cookie4 = '_csrf=' + cookiecsrf
// console.log("Cookie4", cookie4)
console.log(" CSRF input parameter", csrf)
console.log(" CSRF cookie", cookiecsrf)

let authorizeurl2 = 'https://accounts.athom.com/authorise?client_id=' + client_id + '&redirect_uri=' + encodeURIComponent(redirect_url) + '&response_type=code&user_token=' + token.token
console.log("GET Authorization", authorizeurl2)
const response4 = await fetch(authorizeurl2, {
  "headers": {
    "content-type": "application/x-www-form-urlencoded",
    "cookie": cookie4
  },
  "redirect": "manual",
  "body": "resource=resource.homey." + cloudid + "&_csrf=" + csrf + "&allow=Allow",
  "method": "POST",
  "mode": "cors",
  "credentials": "include"
});

const body4 = await response4.text()

let code = response4.headers.get('location').split('=')[1]

console.log(" Response from authorization. Redirect to ", response4.headers.get('location'))
console.log(" Response content ", body4)
console.log(" Parsed the following code ", code)

let tokenendpoint = 'https://api.athom.com/oauth2/token'
console.log("POST token (resolve code to token) " + tokenendpoint)
const response5 = await fetch(tokenendpoint, {
  "headers": {
    "content-type": "application/x-www-form-urlencoded",
  },
  "body": 'client_id=' + encodeURIComponent(client_id) + '&client_secret=' + encodeURIComponent(client_secret) + 
    '&grant_type=authorization_code&code=' + encodeURIComponent(code),
  "method": "POST",
  "mode": "cors",
  "credentials": "include"
});

//console.log("Response5", response5)
const body5 = await response5.text()
let accesstoken = JSON.parse(body5)

let delegationEndpoint = 'https://api.athom.com/delegation/token?audience=homey'
const response6 = await fetch(delegationEndpoint, {
  "headers": {
    "content-type": "application/x-www-form-urlencoded",
    "authorization": "Bearer " + accesstoken.access_token
  },
  "referrerPolicy": "no-referrer-when-downgrade",
  "body": "client_id=" + client_id + " &client_secret=" + client_secret + "&grant_type=refresh_token&refresh_token=" + accesstoken.refresh_token,
  "method": "POST",
  "mode": "cors",
  "credentials": "include"
});

const body6 = await response6.json()
console.log(" JWT token is " + body6)

let endpoint7 = 'https://' + cloudid + '.connect.athom.com/api/manager/users/login'
console.log("POST login endpoint " + endpoint7)
const response7 = await fetch(endpoint7, {
  "headers": {
    "content-type": "application/json",
    //"authorization": "Bearer " + accesstoken.access_token
  },
  "body": JSON.stringify({"token": body6}),
  "method": "POST"
});

const body7 = await response7.json()
console.log(" Response status " + response7.status)
console.log(" Response: " + body7)

await setTagValue("accesstoken", {type: 'string', title:'Access token'}, body7)
return true

```

---

<div class="post-metadata">

**Author:** ![Le\_Cactus](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/le_cactus/32/9727_2.png) [@Le\_Cactus](https://community.homey.app/u/Le_Cactus)\
**Post date:** [May 27, 2021, 6:51am UTC](https://community.homey.app/t/homeyscript-for-bearer-token/47819/7 "2021-05-27T06:51:08Z")

</div>

Yay!!! That was quick! Many thanks RobertKlep. Really apreciated.

---

<div class="post-metadata">

**Author:** ![JPe4619](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jpe4619/32/9017_2.png) [@JPe4619](https://community.homey.app/u/JPe4619)\
**Post date:** [May 27, 2021, 4:58pm UTC](https://community.homey.app/t/homeyscript-for-bearer-token/47819/8 "2021-05-27T16:58:55Z")

</div>

> [@robertklep](#):
>
> `await setTagValue("accesstoken", {type: 'string', title:'Access token'}, body7)`

the format for setTagValue is also changed:  
Warning: setTagValue(id, opts, value) is deprecated, please use **tag(id, value)**

---

<div class="post-metadata">

**Author:** ![Fire69](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/fire69/32/8758_2.png) [@Fire69](https://community.homey.app/u/Fire69)\
**Post date:** [August 5, 2021, 9:24am UTC](https://community.homey.app/t/homeyscript-for-bearer-token/47819/9 "2021-08-05T09:24:27Z")

</div>

I need some help with this.  
I used @robertklep’s version to create the Homeyscript but when I run it I get:

```auto
❌ Script Error
⚠️ Error: invalid_type
    at Remote Process

```

When I use tag instead of setTagValue as remarked by @JPe4619 I still get the same error.

---

<div class="post-metadata">

**Author:** ![JPe4619](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jpe4619/32/9017_2.png) [@JPe4619](https://community.homey.app/u/JPe4619)\
**Post date:** [August 5, 2021, 9:58am UTC](https://community.homey.app/t/homeyscript-for-bearer-token/47819/10 "2021-08-05T09:58:32Z")

</div>

No problem here, with or without the change:  
 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/8/8/8836e2de32b91614ebfcca1acaf54f49f00fc578.png)

---

<div class="post-metadata">

**Author:** ![Fire69](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/fire69/32/8758_2.png) [@Fire69](https://community.homey.app/u/Fire69)\
**Post date:** [August 5, 2021, 12:24pm UTC](https://community.homey.app/t/homeyscript-for-bearer-token/47819/11 "2021-08-05T12:24:54Z")

</div>

It doesn’t return a 200 for me.

![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/5/5/55402d3c7f95e7c258907651c0ee7571ac3600fe.png)

Do you have MFA active?

[edit]  
Found the issue in the output of the script.

```auto
POST authentication https://accounts.athom.com/login
 Response from accounts.athom.com/login {"code":401,"error":"missing_otp","error_description":"The 2FA token is missing. Please append it to the password."}

```

So you can’t use this (in an automated way) with MFA?

---

<div class="post-metadata">

**Author:** ![HansieNL](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/hansienl/32/10634_2.png) [@HansieNL](https://community.homey.app/u/HansieNL)\
**Post date:** [August 5, 2021, 12:27pm UTC](https://community.homey.app/t/homeyscript-for-bearer-token/47819/12 "2021-08-05T12:27:16Z")

</div>

I changed _await setTagValue(“accesstoken”, {type: ‘string’, title:‘Access token’}, body7)_ to:  
**await tag(“accesstoken”, body7)** and have no error message.

---

<div class="post-metadata">

**Author:** ![JPe4619](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jpe4619/32/9017_2.png) [@JPe4619](https://community.homey.app/u/JPe4619)\
**Post date:** [August 5, 2021, 12:49pm UTC](https://community.homey.app/t/homeyscript-for-bearer-token/47819/13 "2021-08-05T12:49:58Z")

</div>

> [@Fire69](#):
>
> with MFA?

coulld be the problem, I dont use it.

---

<div class="post-metadata">

**Author:** ![Fire69](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/fire69/32/8758_2.png) [@Fire69](https://community.homey.app/u/Fire69)\
**Post date:** [August 5, 2021, 1:21pm UTC](https://community.homey.app/t/homeyscript-for-bearer-token/47819/14 "2021-08-05T13:21:05Z")

</div>

Yep, that was the problem. Added it to the password and it worked.

---

<div class="post-metadata">

**Author:** ![Peter\_Kawa](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/peter_kawa/32/173848_2.png) [@Peter\_Kawa](https://community.homey.app/u/Peter_Kawa)\
**Post date:** [October 3, 2023, 2:27pm UTC](https://community.homey.app/t/homeyscript-for-bearer-token/47819/15 "2023-10-03T14:27:20Z")

</div>

@Le_Cactus & @robertklep & homey.solweb.no  
Just found this script: Thanks a mil, guys!!  
This way it is a lot more comfortable / easy to retrieve the \*%$& token 🥳

---

<div class="post-metadata">

**Author:** ![diapolon](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/diapolon/32/82612_2.png) [@diapolon](https://community.homey.app/u/diapolon)\
**Post date:** [October 3, 2023, 5:15pm UTC](https://community.homey.app/t/homeyscript-for-bearer-token/47819/16 "2023-10-03T17:15:19Z")

</div>

This bearer token is equivalent api key for homey 2016/2019?

---

<div class="post-metadata">

**Author:** ![Peter\_Kawa](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/peter_kawa/32/173848_2.png) [@Peter\_Kawa](https://community.homey.app/u/Peter_Kawa)\
**Post date:** [October 3, 2023, 8:09pm UTC](https://community.homey.app/t/homeyscript-for-bearer-token/47819/17 "2023-10-03T20:09:57Z")

</div>

> [@diapolon](#):
>
> api key for homey 2016/2019

I guess the answer can be found in an other topic, from [this post](https://community.homey.app/t/connecting-to-the-homey-api/51633/13) onwards.
