繁体   English   中英

我如何使用oauth 2.0通过JS代码从gmail抓取一封电子邮件

[英]how can I grab email one by one come from gmail using oauth 2.0 via JS code

我已经使用JS代码检索了Gmail联系人。

<script type="text/javascript">
      var clientId = '';
      var apiKey = '';
      var scopes = 'https://www.googleapis.com/auth/contacts.readonly';
      $(document).on("click",".googleContactsButton", function(){
        gapi.client.setApiKey(apiKey);
        window.setTimeout(authorize);
      });
      function authorize() {
        gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: false}, handleAuthorization);
      }
      function handleAuthorization(authorizationResult) {
        if (authorizationResult && !authorizationResult.error) {
          $.get("https://www.google.com/m8/feeds/contacts/default/thin?alt=json&access_token=" + authorizationResult.access_token + "&max-results=500&v=3.0",
            function(response){
              //process the response here
              //console.log(response);

              var response = (JSON.stringify(response.feed.entry[1].gd$email, null, 4));
console.log(response);
            });
        }
      }
    </script>

现在我的问题是,我通过否通过任何一封电子邮件。

(JSON.stringify(response.feed.entry[1].gd$email, null, 4));

entry[0 or 1 or etc]entry[0 or 1 or etc]

且以上代码的结果为[ { "rel": "http://schemas.google.com/g/2005#other", "address": "ashish.ascent@gmail.com", "primary": "true" } ]

我想一个个地检索所有电子邮件,我该如何实现?

现在不考虑JSON.stringify() ,您可以执行以下操作。

Array .map方法返回一个新数组。

var arrayOfEmails = response.feed.entry.map(function (feedEntry) {
  return feedEntry.address;
})

我找到了解决方案

function(response){
   //console.log(response.feed.entry.length);
    for(var i = 0;i<response.feed.entry.length; i++){
    if(response.feed.entry[i].gd$email)
    {
        console.log("email is " + response.feed.entry[i].gd$email[0].address);
    }
}
});

收到响应后,使用for循环逐个打印电子邮件,直到响应中包含电子邮件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM