-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
105 lines (90 loc) · 3.11 KB
/
Copy pathscript.js
File metadata and controls
105 lines (90 loc) · 3.11 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
const CLIENT_ID = "";
const API_KEY = "";
const DISCOVERY_DOCS = ['https://www.googleapis.com/discovery/v1/apis/youtube/v3/rest'];
const SCOPES = "https://www.googleapis.com/auth/youtube.readonly";
const authorizeButton = document.getElementById('authorize-button');
const signoutButton = document.getElementById('signout-button');
const content = document.getElementById("content");
const channelForm = document.getElementById("channel-form");
const channelInput = document.getElementById("channel-input");
const videoContainer = document.getElementById("video-container");
const defaultChannel = "UCtKBgGqbVWkiPNTK1viTq0w";
let accessToken = '';
// GAPI client'ı yükleyip başlatıyoruz.
function gapiInit() {
gapi.load('client:auth2', () => {
gapi.client.init({
apiKey: API_KEY,
clientId: CLIENT_ID,
discoveryDocs: DISCOVERY_DOCS,
scope: SCOPES
}).then(() => {
gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus);
updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get());
});
});
}
// Kullanıcının kimlik doğrulama yanıtını işler.
function handleCredentialResponse(response) {
accessToken = response.getAuthResponse().id_token;
gapi.client.setToken({ access_token: accessToken });
updateSigninStatus(true);
}
// Kullanıcının oturum durumunu günceller.
function updateSigninStatus(isSignedIn) {
if (isSignedIn) {
authorizeButton.style.display = "none";
signoutButton.style.display = "block";
content.style.display = "block";
videoContainer.style.display = "block";
getUserChannels();
} else {
authorizeButton.style.display = "block";
signoutButton.style.display = "none";
content.style.display = "none";
videoContainer.style.display = "none";
}
}
// Kullanıcının kimlik doğrulamasını işler.
function handleAuthClick() {
gapi.auth2.getAuthInstance().signIn();
}
// Kullanıcının oturumunu kapatır.
function handleSignoutClick() {
gapi.auth2.getAuthInstance().signOut();
}
// Kullanıcının YouTube kanallarını getirir.
async function getUserChannels() {
try {
const response = await gapi.client.youtube.channels.list({
part: 'snippet,contentDetails,statistics',
mine: true
});
console.log(response);
} catch (error) {
console.error('Error fetching user channels:', error);
}
}
// Sayfa yüklendiğinde kimlik doğrulama ve GAPI başlatma işlemleri.
window.onload = function () {
google.accounts.id.initialize({
client_id: CLIENT_ID,
callback: handleCredentialResponse,
cancel_on_tap_outside: false,
context: "signin",
ux_mode: "popup",
use_fedcm_for_prompt: true
});
google.accounts.id.renderButton(
document.getElementById("authorize-button"),
{ theme: "outline", size: "large" }
);
gapiInit();
};
authorizeButton.onclick = handleAuthClick;
signoutButton.onclick = handleSignoutClick;
channelForm.onsubmit = function(event) {
event.preventDefault();
const channelName = channelInput.value;
getUserChannels(channelName);
};