Spaces:
Build error
Build error
Create app.js
Browse files
app.js
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
const express = require('express');
|
2 |
+
const axios = require('axios');
|
3 |
+
const qs = require('qs');
|
4 |
+
const app = express();
|
5 |
+
|
6 |
+
// Google OAuth 2.0の設定
|
7 |
+
const client_id = '1033286471224-n9mv8l869fqikubj2e8q92n8ige3qr6r.apps.googleusercontent.com';
|
8 |
+
const client_secret = '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';
|
9 |
+
const redirect_uri = 'YOUR_REDIRECT_URI'; // 自分のリダイレクトURIに変更
|
10 |
+
|
11 |
+
app.get('/auth/google', (req, res) => {
|
12 |
+
const authUrl = `https://accounts.google.com/o/oauth2/auth?client_id=${client_id}&redirect_uri=${redirect_uri}&response_type=code&scope=https://www.googleapis.com/auth/drive.file`;
|
13 |
+
res.redirect(authUrl);
|
14 |
+
});
|
15 |
+
|
16 |
+
app.get('/auth/google/callback', async (req, res) => {
|
17 |
+
const code = req.query.code;
|
18 |
+
|
19 |
+
// Googleからアクセストークンを取得
|
20 |
+
const tokenUrl = 'https://oauth2.googleapis.com/token';
|
21 |
+
const params = {
|
22 |
+
code: code,
|
23 |
+
client_id: client_id,
|
24 |
+
client_secret: client_secret,
|
25 |
+
redirect_uri: redirect_uri,
|
26 |
+
grant_type: 'authorization_code',
|
27 |
+
};
|
28 |
+
|
29 |
+
try {
|
30 |
+
const response = await axios.post(tokenUrl, qs.stringify(params), {
|
31 |
+
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
32 |
+
});
|
33 |
+
|
34 |
+
const accessToken = response.data.access_token;
|
35 |
+
res.json({ accessToken }); // フロントエンドにアクセストークンを返す
|
36 |
+
} catch (error) {
|
37 |
+
res.status(500).json({ error: 'Failed to exchange code for token' });
|
38 |
+
}
|
39 |
+
});
|
40 |
+
|
41 |
+
app.listen(3000, () => {
|
42 |
+
console.log('Server is running on port 3000');
|
43 |
+
});
|