# Authentication for java webapplication

**URL:** https://community.homey.app/t/authentication-for-java-webapplication/145892
**Category:** Questions & Help
**Tags:** homey-pro
**Created:** [November 25, 2025, 2:56pm UTC](https://community.homey.app/t/authentication-for-java-webapplication/145892 "2025-11-25T14:56:33Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Wouter\_Verbraak](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/wouter_verbraak/32/58663_2.png) [@Wouter\_Verbraak](https://community.homey.app/u/Wouter_Verbraak)
#### Post date: [November 25, 2025, 2:56pm UTC](https://community.homey.app/t/authentication-for-java-webapplication/145892/1 "2025-11-25T14:56:33Z")

</div>

Hi, I am trying to retrieve device from my Homey Pro data for a java based web application. Please help me to setup authentication for cloud login. I setup a client\_ID and client\_SECRET via Web API, but Claude tells me I need to get these for “HomeyScript”? I can’t find the project page to do so? Bit stuck at the moment. Is Claude leading me astray? Grateful for help! Your app is registered as **“Web App”** → only allows browser OAuth redirects Server needs **“HomeyScript”** → allows password authentication

### The Fix (2 Minutes)

1. **Go to:** [https://tools.developer.homey.app/api/projects](https://tools.developer.homey.app/api/projects)

2. **Create new project**

3. **Select type: “HomeyScript”** ← Critical!

4. **Copy** new Client ID & Secret

---

<div class="post-metadata">

### Author: ![smarthomesven](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/smarthomesven/32/79712_2.png) [@smarthomesven](https://community.homey.app/u/smarthomesven)
#### Post date: [November 25, 2025, 3:30pm UTC](https://community.homey.app/t/authentication-for-java-webapplication/145892/2 "2025-11-25T15:30:05Z")

</div>

That isn’t the correct wat to create one. Go here to create an api client:

> **[Homey Developer Tools](https://tools.developer.homey.app/api/clients)**

---

<div class="post-metadata">

### Author: ![Wouter\_Verbraak](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/wouter_verbraak/32/58663_2.png) [@Wouter\_Verbraak](https://community.homey.app/u/Wouter_Verbraak)
#### Post date: [November 25, 2025, 4:02pm UTC](https://community.homey.app/t/authentication-for-java-webapplication/145892/3 "2025-11-25T16:02:02Z")

</div>

That is what I used, but I get the error: Error updating Homey sensors: Authentication failed after 2 attempts: The grant type is unauthorised for this client\_id

for (let attempt = 1; attempt \<= maxRetries; attempt++) {  
try {  
// Create Cloud API instance (same as setup-homey.js)  
const cloudApi = new AthomCloudAPI({  
clientId: process.env.HOMEY\_CLIENT\_ID,  
clientSecret: process.env.HOMEY\_CLIENT\_SECRET,  
});

```auto
  console.log(`Attempt ${attempt}/${maxRetries}: Calling authenticateWithPassword...`);

  // Authenticate with username/password
  // Wrap in a promise with timeout
  await Promise.race([
    cloudApi.authenticateWithPassword(
      process.env.HOMEY_USERNAME,
      process.env.HOMEY_PASSWORD
    ),
    new Promise((_, reject) => 
      setTimeout(() => reject(new Error('Authentication timeout after 25 seconds')), 25000)
    )
  ]);

```

---

<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: [November 25, 2025, 4:09pm UTC](https://community.homey.app/t/authentication-for-java-webapplication/145892/4 "2025-11-25T16:09:30Z")

</div>

Why are we looking at Javascript code when the post title says “ **java** webapplication”?

---

<div class="post-metadata">

### Author: ![smarthomesven](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/smarthomesven/32/79712_2.png) [@smarthomesven](https://community.homey.app/u/smarthomesven)
#### Post date: [November 25, 2025, 4:12pm UTC](https://community.homey.app/t/authentication-for-java-webapplication/145892/5 "2025-11-25T16:12:51Z")

</div>

Did you import the library as well?

```auto
const AthomCloudAPI = require('homey-api/lib/AthomCloudAPI');

```

Everything you need is explained here:

> **[Welcome to the Web API documentation 👋 | Homey Web API](https://api.developer.homey.app/)**
>
> 🌍 Learn how to build great integrations for Homey Pro & Homey Cloud.

---

<div class="post-metadata">

### Author: ![Wouter\_Verbraak](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/wouter_verbraak/32/58663_2.png) [@Wouter\_Verbraak](https://community.homey.app/u/Wouter_Verbraak)
#### Post date: [November 25, 2025, 4:15pm UTC](https://community.homey.app/t/authentication-for-java-webapplication/145892/6 "2025-11-25T16:15:32Z")

</div>

YES:

- Vercel Serverless Function: Homey Cloud API Proxy (FIXED v2)
- 
- This version uses the ACTUAL homey-api library (same as setup-homey.js)
- but with optimizations for serverless environments
- 
- Environment variables required in Vercel:
- 
  - HOMEY\_CLIENT\_ID (from [Homey Developer Tools](https://tools.developer.homey.app/api/projects) )

- 
  - HOMEY\_CLIENT\_SECRET

- 
  - HOMEY\_USERNAME (your Homey account email)

- 
  - HOMEY\_PASSWORD (your Homey account password)

- 
  - HOMEY\_DEVICE\_ID\_TEMP (outdoor temperature sensor)

- 
  - HOMEY\_DEVICE\_ID\_HUMIDITY (outdoor humidity sensor, optional)  
\*/

const AthomCloudAPI = require(‘homey-api/lib/AthomCloudAPI’);

// Cache for Homey API session (persists across function invocations in same container)  
let cachedHomeyApi = null;  
let cacheTimestamp = null;  
const CACHE\_DURATION = 30 \* 60 \* 1000; // 30 minutes (conservative for serverless)

/\*\*

- Get authenticated Homey API with retry logic  
\*/  
async function getHomeyApiWithRetry(maxRetries = 2) {  
const now = Date.now();

// Return cached connection if still valid  
if (cachedHomeyApi && cacheTimestamp && (now - cacheTimestamp) \< CACHE\_DURATION) {  
console.log(‘✅ Using cached Homey API session’);  
try {  
// Quick test to ensure session is still valid  
await cachedHomeyApi.system.getInfo();  
return cachedHomeyApi;  
} catch (error) {  
console.warn(‘⚠ Cached session invalid, re-authenticating…’);  
cachedHomeyApi = null;  
cacheTimestamp = null;  
}  
}

console.log(‘🔐 Authenticating with Homey Cloud API…’);

for (let attempt = 1; attempt \<= maxRetries; attempt++) {  
try {  
// Create Cloud API instance (same as setup-homey.js)  
const cloudApi = new AthomCloudAPI({  
clientId: process.env.HOMEY\_CLIENT\_ID,  
clientSecret: process.env.HOMEY\_CLIENT\_SECRET,  
});

```auto
  console.log(`Attempt ${attempt}/${maxRetries}: Calling authenticateWithPassword...`);

  // Authenticate with username/password
  // Wrap in a promise with timeout
  await Promise.race([
    cloudApi.authenticateWithPassword(
      process.env.HOMEY_USERNAME,
      process.env.HOMEY_PASSWORD
    ),
    new Promise((_, reject) => 
      setTimeout(() => reject(new Error('Authentication timeout after 25 seconds')), 25000)
    )
  ]);

  console.log('✅ Authentication successful, getting user...');

  // Get user and Homey
  const user = await Promise.race([
    cloudApi.getAuthenticatedUser(),
    new Promise((_, reject) => 
      setTimeout(() => reject(new Error('Get user timeout after 10 seconds')), 10000)
    )
  ]);

  console.log('✅ User retrieved, getting Homey...');

  const homey = await Promise.race([
    user.getFirstHomey(),
    new Promise((_, reject) => 
      setTimeout(() => reject(new Error('Get Homey timeout after 10 seconds')), 10000)
    )
  ]);

  console.log('✅ Homey found, creating session...');

  // Create session on Homey
  const homeyApi = await Promise.race([
    homey.authenticate(),
    new Promise((_, reject) => 
      setTimeout(() => reject(new Error('Session creation timeout after 15 seconds')), 15000)
    )
  ]);

  console.log('✅ Homey API session created successfully');

  // Cache the session
  cachedHomeyApi = homeyApi;
  cacheTimestamp = now;

  return homeyApi;

} catch (error) {
  console.error(`❌ Attempt ${attempt}/${maxRetries} failed:`, error.message);
  
  if (attempt === maxRetries) {
    throw new Error(`Authentication failed after ${maxRetries} attempts: ${error.message}`);
  }
  
  // Wait before retry (exponential backoff)
  const waitTime = Math.min(1000 * Math.pow(2, attempt - 1), 5000);
  console.log(`⏳ Waiting ${waitTime}ms before retry...`);
  await new Promise(resolve => setTimeout(resolve, waitTime));
}

```

}

throw new Error(‘Authentication failed: Max retries exceeded’);  
}

---

<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: [November 25, 2025, 8:29pm UTC](https://community.homey.app/t/authentication-for-java-webapplication/145892/7 "2025-11-25T20:29:19Z")

</div>

Please, at least post code formatted as code (between three backticks), or use the `</> Preformatted text` button
