Toggle navigation
Toggle navigation
This project
Loading...
Sign in
신지원
/
LineMusicChatbot
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Graphs
Network
Create a new issue
Commits
Issue Boards
Authored by
엄민용
2021-05-31 01:10:33 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
e97b110468ffe9e245d001ea519665729268bbe7
e97b1104
1 parent
e7c6805d
Youtube API Sample for Project
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
103 additions
and
0 deletions
Youtube API/oauth2.js
Youtube API/playlist.js
Youtube API/oauth2.js
0 → 100644
View file @
e97b110
const
{
google
}
=
require
(
"googleapis"
);
var
http
=
require
(
'http'
);
var
opn
=
require
(
'opn'
);
var
destroyer
=
require
(
'destroyer'
);
var
url
=
require
(
'url'
);
var
enableDestroy
=
require
(
'server-destroy'
);
const
oauth2Client
=
new
google
.
auth
.
OAuth2
(
"394965702516-q817fn2gj8r8sqoudhp29k1rd71ku42j.apps.googleusercontent.com"
,
"xgo5tcTwsmg2alNumeY1Drj_"
,
"http://localhost:3031/api/oauth2callback"
);
const
scopes
=
[
'https://www.googleapis.com/auth/youtube'
,
'https://www.googleapis.com/auth/youtube.force-ssl'
,
'https://www.googleapis.com/auth/youtube.readonly'
,
'https://www.googleapis.com/auth/youtubepartner'
];
let
refreshToken
=
''
;
async
function
authenticate
()
{
return
new
Promise
((
resolve
,
reject
)
=>
{
if
(
refreshToken
!==
""
)
{
oauth2Client
.
setCredentials
({
refresh_token
:
refreshToken
});
oauth2Client
.
getAccessToken
((
err
,
ttoken
)
=>
{
if
(
err
)
{
reject
(
e
);
}
resolve
(
oauth2Client
);
});
}
else
{
const
authorizeUrl
=
oauth2Client
.
generateAuthUrl
({
access_type
:
'offline'
,
scope
:
scopes
.
join
(
' '
),
});
const
server
=
http
.
createServer
(
async
(
req
,
res
)
=>
{
try
{
if
(
req
.
url
.
indexOf
(
'/api/oauth2callback'
)
>
-
1
)
{
const
qs
=
new
url
.
URL
(
req
.
url
,
'http://localhost:3031'
).
searchParams
;
res
.
end
(
'Completed'
);
enableDestroy
(
server
);
server
.
destroy
();
const
{
tokens
}
=
await
oauth2Client
.
getToken
(
qs
.
get
(
'code'
));
refreshToken
=
tokens
.
refresh_token
;
oauth2Client
.
credentials
=
tokens
;
resolve
(
oauth2Client
);
}
}
catch
(
e
)
{
reject
(
e
);
}
}).
listen
(
3031
,
()
=>
{
opn
(
authorizeUrl
,
{
wait
:
false
}).
then
(
cp
=>
cp
.
unref
());
});
destroyer
(
server
);
}
});
}
module
.
exports
.
refreshClient
=
authenticate
;
\ No newline at end of file
Youtube API/playlist.js
0 → 100644
View file @
e97b110
var
{
google
}
=
require
(
'googleapis'
);
var
service
=
google
.
youtube
(
'v3'
);
const
oauth2Service
=
require
(
'./oauth2'
);
function
getPlaylistData
(
oauth2Client
,
channelId
)
{
service
.
playlists
.
list
({
auth
:
oauth2Client
,
part
:
'id, snippet'
,
fields
:
'items(id, snippet(title, description, thumbnails(default(url))))'
,
channelId
:
channelId
},
function
(
err
,
response
)
{
if
(
err
)
{
console
.
log
(
'The API returned an error: '
+
err
);
return
;
}
var
channels
=
response
.
data
.
items
;
if
(
channels
.
length
==
0
){
console
.
log
(
'No channel found.'
)
}
else
{
console
.
log
(
JSON
.
stringify
(
channels
,
null
,
4
));
}
});
}
oauth2Service
.
refreshClient
()
.
then
((
client
)
=>
{
getPlaylistData
(
client
,
'UCx6jsZ02B4K3SECUrkgPyzg'
);
})
.
catch
(
console
.
error
);
\ No newline at end of file
Please
register
or
login
to post a comment