forked from giannisftaras/smartthings
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexchange_oauth_code.php
More file actions
85 lines (70 loc) · 2.66 KB
/
exchange_oauth_code.php
File metadata and controls
85 lines (70 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
/**
* Simple OAuth Token Exchange
*
* This script will help you exchange an authorization code for tokens
* when you already have the authorization code from the redirect.
*/
require __DIR__ . '/vendor/autoload.php';
echo "SmartThings OAuth Token Exchange\n";
echo "===============================\n\n";
// Load config
$config = parse_ini_file(__DIR__ . '/oauth_tokens.ini', true);
$client_id = $config['oauth_app']['client_id'];
$client_secret = $config['oauth_app']['client_secret'];
$redirect_uri = $config['oauth_app']['redirect_uri'];
echo "Current configuration:\n";
echo "Client ID: $client_id\n";
echo "Redirect URI: $redirect_uri\n\n";
echo "Authorization URL:\n";
$auth_url = 'https://api.smartthings.com/oauth/authorize?' . http_build_query([
'response_type' => 'code',
'client_id' => $client_id,
'redirect_uri' => $redirect_uri,
'scope' => 'r:devices:* x:devices:*',
'state' => bin2hex(random_bytes(16))
]);
echo "$auth_url\n\n";
echo "Instructions:\n";
echo "1. Open the URL above in your browser\n";
echo "2. Authorize the application\n";
echo "3. Copy the authorization code from the redirect URL\n";
echo "4. Enter it below\n\n";
echo "Authorization Code: ";
$auth_code = trim(fgets(STDIN));
if (empty($auth_code)) {
echo "No authorization code provided.\n";
exit(1);
}
// Exchange code for tokens
$client = new \GuzzleHttp\Client([
'timeout' => 30.0,
'http_errors' => false
]);
$response = $client->request('POST', 'https://api.smartthings.com/oauth/token', [
'form_params' => [
'grant_type' => 'authorization_code',
'client_id' => $client_id,
'client_secret' => $client_secret,
'redirect_uri' => $redirect_uri,
'code' => $auth_code
]
]);
$status_code = $response->getStatusCode();
$body = json_decode($response->getBody()->getContents(), true);
echo "\nResponse Status: $status_code\n";
echo "Response Body: " . json_encode($body, JSON_PRETTY_PRINT) . "\n\n";
if ($status_code === 200 && isset($body['access_token'])) {
echo "✅ Success! OAuth tokens received.\n";
echo "\nTokens received:\n";
echo "- Access Token: " . substr($body['access_token'], 0, 20) . "...\n";
echo "- Refresh Token: " . substr($body['refresh_token'], 0, 20) . "...\n";
echo "- Expires in: " . ($body['expires_in'] ?? 'unknown') . " seconds\n\n";
echo "⚠️ NOTE: This script is for testing purposes.\n";
echo "In production, tokens are stored per-user in individual files.\n";
echo "Use the main json.php endpoint for OAuth setup:\n";
echo "GET /json.php?setup=1&user_id=YOUR_UNIQUE_ID\n";
} else {
echo "❌ Failed to get tokens. Check the error response above.\n";
}
?>