Repository navigation
Expand file tree
/
Copy pathoauth.js
More file actions
61 lines (55 loc) · 1.43 KB
/
Copy pathoauth.js
File metadata and controls
61 lines (55 loc) · 1.43 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
const removeCachedAuthToken = (accessToken) => {
// Remove token from the token cache.
chrome.identity.removeCachedAuthToken(
{
token: accessToken,
},
() => {
alert("finish revoke");
}
);
};
const printContacts = (token) => {
fetch("https://people.googleapis.com/v1/contactGroups/all?maxMembers=20", {
method: "GET",
async: true,
headers: {
Authorization: "Bearer " + token,
"Content-Type": "application/json",
},
contentType: "json",
})
.then((response) => response.json())
.then((data) => {
console.log("contacts data:", data);
});
};
const revokeAuthToken = (accessToken) => {
// Make a request to revoke token
var xhr = new XMLHttpRequest();
xhr.open(
"GET",
"https://accounts.google.com/o/oauth2/revoke?token=" + accessToken
);
xhr.send();
removeCachedAuthToken(accessToken);
};
window.onload = () => {
let accessToken = "";
document.querySelector("#auth").addEventListener("click", () => {
chrome.identity.getAuthToken(
{ interactive: true },
(token, grantedScopes) => {
alert("auth success");
console.log("got token", token, grantedScopes);
accessToken = token;
}
);
});
document.querySelector("#revoke").addEventListener("click", () => {
revokeAuthToken(accessToken);
});
document.querySelector("#show").addEventListener("click", () => {
printContacts(accessToken);
});
};