-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
67 lines (48 loc) · 1.59 KB
/
index.html
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
<head>
<script src="https://apis.google.com/js/api.js"></script>
<link rel="stylesheet" href="some.css"/>
</head>
<body>
<script>
function initGoogleApi () {
const client_id = '447838384773-9n5pf96vipeuedm8ls2und2uu0ekteqt.apps.googleusercontent.com';
const apiKey = 'SqgxaJRRDVB4s3x9zEbkaR4B';
const scope = 'https://www.googleapis.com/auth/userinfo.email';
function loadAuth2 () {
return new Promise(resolve => {
gapi.load('client:auth2', resolve);
});
}
function setApiKey () {
gapi.client.setApiKey(apiKey);
return Promise.resolve()
}
function initApi () {
return gapi.auth2.init({client_id, scope}).then(() => {});
}
return loadAuth2()
.then(setApiKey)
.then(initApi)
.catch(console.error);
}
function signInToGoogle() {
return gapi.auth2.getAuthInstance().signIn();
}
function makeGoogleApiCall() {
return signInToGoogle()
.then(() => gapi.client.request({path: 'https://www.googleapis.com/oauth2/v2/userinfo'}))
.then(({result}) => console.log('Retrieved email address from Google API', result.email, '(skipped service worker, but why?)'))
}
function makeOtherApiCall() {
return fetch('https://pokeapi.co/api/v2/pokemon/1/')
.then(res => res.json())
.then(data => console.log(data));
}
// start
navigator.serviceWorker.register('service-worker.js', {scope: './'});
initGoogleApi();
</script>
<h1>Open console to see fetch events</h1>
<button onclick="makeOtherApiCall();">Make non-Google API call</button>
<button onclick="makeGoogleApiCall();">Make Google API call</button>
</body>