-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemail.html
More file actions
144 lines (131 loc) · 4.47 KB
/
Copy pathemail.html
File metadata and controls
144 lines (131 loc) · 4.47 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<html>
<head>
<script type="text/javascript">
// Your Client ID can be retrieved from your project in the Google
// Developer Console, https://console.developers.google.com
var CLIENT_ID = '336322580164-k20rep3th6cv0i6gokb04u56f3lgvqpe.apps.googleusercontent.com';
var SCOPES = ['https://www.googleapis.com/auth/gmail.readonly', 'https://www.googleapis.com/auth/plus.login'];
/**
* Check if current user has authorized this application.
*/
function checkAuth() {
gapi.auth.authorize({
'client_id': CLIENT_ID,
'scope': SCOPES.join(' '),
'immediate': true
}, handleAuthResult);
}
/**
* Handle response from authorization server.
*
* @param {Object} authResult Authorization result.
*/
function handleAuthResult(authResult) {
var authorizeDiv = document.getElementById('authorize-div');
if (authResult && !authResult.error) {
// Hide auth UI, then load client library.
authorizeDiv.style.display = 'none';
loadGmailApi();
} else {
// Show auth UI, allowing the user to initiate authorization by
// clicking authorize button.
authorizeDiv.style.display = 'inline';
}
}
/**
* Initiate auth flow in response to user clicking authorize button.
*
* @param {Event} event Button click event.
*/
function handleAuthClick(event) {
gapi.auth.authorize({
client_id: CLIENT_ID,
scope: SCOPES,
immediate: false
},
handleAuthResult);
return false;
}
/**
* Load Gmail API client library. List labels once client library
* is loaded.
*/
function loadGmailApi() {
gapi.client.load('gmail', 'v1', listLabels);
gapi.client.load('plus', 'v1', loadProfile);
}
/**
* Print all Labels in the authorized user's inbox. If no labels
* are found an appropriate message is printed.
*/
function listLabels() {
var request = gapi.client.gmail.users.messages.list({
'userId': 'me',
'q': 'is:unread'
});
request.execute(function(resp) {
var messages = resp.messages;
if (messages && messages.length > 0) {
appendPre('Total mensagens:' + messages.length);
appendPre('Snippets:');
for (i = 0; i < messages.length; i++) {
var message = messages[i];
var request = gapi.client.gmail.users.messages.get({
'userId': 'me',
'id': message.id
});
request.execute(function(resp) {
appendPre(resp.result.snippet)
});
}
} else {
appendPre('No mensagens found.');
}
});
}
function loadProfile() {
var request = gapi.client.plus.people.get({
'userId': 'me'
});
request.execute(function(resp) {
appendPre("Dados do perfil do usuário: ")
appendPre("Nome: " + resp.displayName);
appendPre("Sexo: " + resp.gender);
appendPre("Perfil do Google Plus: " + resp.url);
appendImg(resp.image.url);
});
}
/**
* Append a pre element to the body containing the given message
* as its text node.
*
* @param {string} message Text to be placed in pre element.
*/
function appendPre(message) {
var pre = document.getElementById('output');
var textContent = document.createTextNode(message + '\n');
pre.appendChild(textContent);
}
function appendImg(url) {
var pre = document.getElementById('output');
var oImg=document.createElement("img");
oImg.setAttribute('src', url);
oImg.setAttribute('height', '100px');
oImg.setAttribute('width', '100px');
pre.appendChild(oImg);
}
</script>
<script src="https://apis.google.com/js/client.js?onload=checkAuth">
</script>
</head>
<body>
<div id="authorize-div" style="display: none">
<span>Authorize access to Gmail API</span>
<!--Button for the user to click to initiate auth sequence -->
<button id="authorize-button" onclick="handleAuthClick(event)">
Authorize
</button>
</div>
<pre id="output"></pre>
</body>
</html>