2016-12-25 60 views
0

我正在学习JavaScript和API的,我正在练习与Gmail。我使用JavaScript网页验证Gmail。问题与验证用户使用JavaScript和GMail API

我得到它的工作,但我试图控制台登录用户的电子邮件后,他们进行身份验证。我尝试了许多没有成功的东西

当用户单击按钮handleAuthClick(event)打开一个iframe并开始auth过程。

我怎样才能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

我真的不明白你在哪里尝试控制电子邮件地址?在authorize后它去回调函数“handleAuthResult”,那么你加载函数“loadGmailApi” gmail的API,并在此行之后:

gapi.client.load('gmail', 'v1', listLabels); 

你可以问你想要gapi.client方法什么都来自here以及特定于邮箱地址gapi.auth2.BasicProfile