我正在学习javascript和API,我正在使用Gmail练习。我在javascript网页中使用Gmail验证Gmail。
我得到了它的工作,但我试图在他们进行身份验证后控制用户的电子邮件。我尝试了许多没有成功的事情
当用户点击按钮handleAuthClick(event)
打开iframe并启动身份验证过程时。
我如何在console.log中输入电子邮件地址?
<script type="text/javascript">
var CLIENT_ID = '123.apps.googleusercontent.com';
var SCOPES = ['https://mail.google.com/'];
/**
* Check if current user has authorized this application.
*/
function checkAuth() {
gapi.auth.authorize(
{
'client_id': CLIENT_ID,
'scope': SCOPES.join(' '),
'immediate': true
}, handleAuthResult);
}
function listMessages(userId, query, callback) {
var getPageOfMessages = function(request, result) {
request.execute(function(resp) {
result = result.concat(resp.messages);
var nextPageToken = resp.nextPageToken;
if (nextPageToken) {
request = gapi.client.gmail.users.messages.list({
'userId': userId,
'pageToken': nextPageToken,
'q': query
});
getPageOfMessages(request, result);
console.log("hey its done" + result)
} else {
callback(result);
}
});
};
var initialRequest = gapi.client.gmail.users.messages.list({
'userId': userId,
'q': query
});
getPageOfMessages(initialRequest, []);
}
/**
* 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();
console.log(resp.email);
} else {
// Show auth UI, allowing the user to initiate authorization by
// clicking authorize button.
//authorizeDiv.style.display = 'inline';
console.log("hey form else")
}
}
/**
* 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);
listMessages('me',"is:unread");// this returns big list of objects yee haw
console.log("hey there from handleAuthClick");
return false;
}
/**
* Load Gmail API client library. List labels once client library
* is loaded.
*/
function loadGmailApi() {
gapi.client.load('gmail', 'v1', listLabels);
}
/**
* 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.labels.list({
'userId': 'me'
});
request.execute(function(resp) {
var labels = resp.labels;
appendPre('Labels:');
if (labels && labels.length > 0) {
for (i = 0; i < labels.length; i++) {
var label = labels[i];
appendPre(label.name)
cosnole.log(label.name)
}
} else {
appendPre('No Labels found.');
}
});
}
function appendPre(message) {
var pre = document.getElementById('output');
var textContent = document.createTextNode(message + '\n');
pre.appendChild(textContent);
}
</script>
答案 0 :(得分:0)
我真的不明白你在哪里试图控制电子邮件地址?在授权之后它转到回调函数'handleAuthResult',然后在函数'loadGmailApi'中加载gmail API并在此行之后:
gapi.client.load('gmail', 'v1', listLabels);
您可以使用here中的gapi.client方法和gapi.auth2.BasicProfile