Repository navigation
Expand file tree
/
Copy pathsetup-linkedin.js
More file actions
executable file
·277 lines (226 loc) · 9.09 KB
/
Copy pathsetup-linkedin.js
File metadata and controls
executable file
·277 lines (226 loc) · 9.09 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const os = require('os');
const { execSync } = require('child_process');
class LinkedInSetup {
constructor() {
// Store config in user's home directory to persist between sessions
// but avoid version control
this.configPath = path.join(os.homedir(), '.hax-linkedin-config.json');
}
displayInstructions() {
console.log(`
🎯 LinkedIn API Setup for HAX Issue Automation
📋 Prerequisites:
1. LinkedIn Developer Account
2. LinkedIn App with proper permissions
🔧 Setup Steps:
1️⃣ Create LinkedIn App:
• Go to: https://www.linkedin.com/developers/apps
• Click "Create app"
• Fill in app details:
- App name: "HAX Productivity Tracker"
- LinkedIn Page: Your personal/company page
- App use: "Other"
2️⃣ Configure App Permissions:
• Products tab → Request "Sign In with LinkedIn using OpenID Connect"
• Products tab → Request "Share on LinkedIn"
• Auth tab → Add redirect URL: http://localhost:3000/callback
3️⃣ Get App Credentials:
• Auth tab → Copy "Client ID" and "Client Secret"
4️⃣ Get Access Token:
• Use LinkedIn OAuth flow (we'll help with this)
5️⃣ Get Person ID:
• Call LinkedIn API to get your person URN
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Ready to start setup? Press Enter to continue...
`);
}
async promptForCredentials() {
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
const question = (prompt) => new Promise(resolve => {
readline.question(prompt, resolve);
});
console.log('🔑 LinkedIn App Credentials\n');
const clientId = await question('Enter Client ID: ');
const clientSecret = await question('Enter Client Secret: ');
readline.close();
return { clientId, clientSecret };
}
generateAuthUrl(clientId) {
// Updated scopes for LinkedIn API v2 with OpenID Connect
const scopes = 'openid,profile,w_member_social';
const redirectUri = 'http://localhost:3000/callback';
const state = Math.random().toString(36).substring(7);
const authUrl = `https://www.linkedin.com/oauth/v2/authorization?` +
`response_type=code&` +
`client_id=${clientId}&` +
`redirect_uri=${encodeURIComponent(redirectUri)}&` +
`state=${state}&` +
`scope=${encodeURIComponent(scopes)}`;
return { authUrl, state, redirectUri };
}
async startCallbackServer(expectedState, clientId, clientSecret, redirectUri) {
const http = require('http');
const url = require('url');
return new Promise((resolve, reject) => {
const server = http.createServer(async (req, res) => {
const parsedUrl = url.parse(req.url, true);
if (parsedUrl.pathname === '/callback') {
const { code, state, error } = parsedUrl.query;
if (error) {
res.writeHead(400);
res.end(`Error: ${error}`);
server.close();
return reject(new Error(error));
}
if (state !== expectedState) {
res.writeHead(400);
res.end('Invalid state parameter');
server.close();
return reject(new Error('Invalid state'));
}
try {
// Exchange code for access token
const tokenResponse = await fetch('https://www.linkedin.com/oauth/v2/accessToken', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: new URLSearchParams({
grant_type: 'authorization_code',
code,
client_id: clientId,
client_secret: clientSecret,
redirect_uri: redirectUri
})
});
if (!tokenResponse.ok) {
throw new Error(`Token exchange failed: ${tokenResponse.status}`);
}
const tokenData = await tokenResponse.json();
// Get user profile info using OpenID Connect userinfo endpoint
const profileResponse = await fetch('https://api.linkedin.com/v2/userinfo', {
headers: {
'Authorization': `Bearer ${tokenData.access_token}`
}
});
if (!profileResponse.ok) {
throw new Error(`Profile fetch failed: ${profileResponse.status}`);
}
const profileData = await profileResponse.json();
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(`
<html>
<body style="font-family: Arial, sans-serif; text-align: center; padding: 50px;">
<h2>✅ LinkedIn Authentication Successful!</h2>
<p>You can close this window and return to the terminal.</p>
<div style="background: #f0f0f0; padding: 20px; border-radius: 8px; margin: 20px;">
<h3>Profile Info:</h3>
<p><strong>Name:</strong> ${profileData.name || `${profileData.given_name} ${profileData.family_name}` || 'N/A'}</p>
<p><strong>ID:</strong> ${profileData.sub}</p>
</div>
</body>
</html>
`);
server.close();
resolve({
accessToken: tokenData.access_token,
refreshToken: tokenData.refresh_token,
expiresIn: tokenData.expires_in,
personId: profileData.sub,
name: profileData.name || `${profileData.given_name} ${profileData.family_name}` || 'LinkedIn User'
});
} catch (error) {
res.writeHead(500);
res.end(`Error: ${error.message}`);
server.close();
reject(error);
}
} else {
res.writeHead(404);
res.end('Not found');
}
});
server.listen(3000, () => {
console.log('🌐 Callback server started on http://localhost:3000');
});
server.on('error', reject);
});
}
async saveConfig(config) {
const configData = {
clientId: config.clientId,
clientSecret: config.clientSecret,
accessToken: config.accessToken,
refreshToken: config.refreshToken,
personId: config.personId,
name: config.name,
expiresIn: config.expiresIn,
createdAt: new Date().toISOString()
};
fs.writeFileSync(this.configPath, JSON.stringify(configData, null, 2));
console.log(`✅ Configuration saved to ${this.configPath}`);
}
async run() {
try {
this.displayInstructions();
// Wait for user to press Enter
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
await new Promise(resolve => {
readline.question('', resolve);
});
readline.close();
// Get credentials
const { clientId, clientSecret } = await this.promptForCredentials();
if (!clientId || !clientSecret) {
console.error('❌ Client ID and Client Secret are required');
process.exit(1);
}
// Generate auth URL
const { authUrl, state, redirectUri } = this.generateAuthUrl(clientId);
console.log(`\n🔗 Authorization URL:`);
console.log(authUrl);
console.log(`\n📋 Instructions:`);
console.log(`1. Copy the URL above and open it in your browser`);
console.log(`2. Grant permissions to your LinkedIn app`);
console.log(`3. You'll be redirected to localhost:3000/callback`);
console.log(`4. Return here - the setup will complete automatically\n`);
// Start callback server and wait for auth
const authResult = await this.startCallbackServer(
state,
clientId,
clientSecret,
redirectUri
);
// Save complete configuration
await this.saveConfig({
clientId,
clientSecret,
...authResult
});
console.log(`\n🎉 Setup Complete!`);
console.log(`📊 Profile: ${authResult.name}`);
console.log(`🔑 Access token expires in: ${authResult.expiresIn} seconds`);
console.log(`\n▶️ Next steps:`);
console.log(` 1. Run: node post-to-linkedin.js`);
console.log(` 2. Your weekly productivity posts will be automated!`);
} catch (error) {
console.error('❌ Setup failed:', error.message);
console.log(`\n🔧 Troubleshooting:`);
console.log(` • Verify your LinkedIn app configuration`);
console.log(` • Check that redirect URI is set to: http://localhost:3000/callback`);
console.log(` • Ensure "Share on LinkedIn" product is added to your app`);
process.exit(1);
}
}
}
const setup = new LinkedInSetup();
setup.run().catch(console.error);