For devices with no browser (smart TVs, CLI tools, IoT). The user authenticates on a separate device (phone or computer).
Your Device OAuth Server User's Phone
| | |
|── POST /device_code ──►| |
|◄── device_code ────────| |
| user_code | |
| |◄── User visits URL ──|
| | enters user_code |
|── poll /token ────────►| |
| (every interval) | |
|◄── access_token ───────| (once approved) |
=== “Node.js”
```js
const SCGAuth = require('scg-auth');
const client = new SCGAuth({
clientId: 'my-tv-app',
tokenUrl: 'https://your-server.com/token',
deviceCodeUrl: 'https://your-server.com/device_code',
});
// Step 1: Get device code
const deviceResp = await client.deviceCode(['openid', 'email']);
console.log(`Go to: ${deviceResp.verification_uri}`);
console.log(`Enter: ${deviceResp.user_code}`);
// Step 2: Poll until user approves (or timeout)
const tokens = await client.pollDeviceToken(deviceResp, {
onPending: () => console.log('Waiting for user...'),
onError: (e) => console.error(e.message),
});
console.log(tokens.access_token);
```
=== “Python”
```python
from scg_auth import SCGAuth
client = SCGAuth(
client_id='my-tv-app',
token_url='https://your-server.com/token',
device_code_url='https://your-server.com/device_code',
)
# Step 1: Get device code
device_resp = client.device_code(['openid', 'email'])
print(f"Go to: {device_resp['verification_uri']}")
print(f"Enter: {device_resp['user_code']}")
# Step 2: Poll
tokens = client.poll_device_token(
device_resp,
on_pending=lambda: print('Waiting...'),
)
print(tokens['access_token'])
```