如何使用Gmail API获取新消息

时间:2019-04-08 09:12:16

标签: javascript google-chrome-extension gmail-api

我正在编写Google Chrome浏览器扩展程序。它使用gmail-api,我想接收进入登录用户的电子邮件地址的传入消息。如何在后台脚本中根据代码实现此功能。我知道gmail api使用诸如watсh之类的发布请求,因此我不知道如何实现代码,以便我可以实时看到消息并在此事件期间更新后台脚本。

我的应用程序使用拉警报的方法,因此文档说明我应该使用诸如 https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.subscriptions/pull https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.subscriptions/acknowledge

如何在代码中使用所有这些内容,以便在收到新消息时脚本会不断更新,甚至可以在javascript中使用?

function POST_request(method,url,_async,params)
{
    let xhr= new XMLHttpRequest();
    xhr.open(method,url,_async);
    xhr.setRequestHeader('Authorization', 'Bearer ' + current_token);
    xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
    xhr.send(JSON.stringify(params));
    return(xhr.responseText);
}

let tmp={
            "topicName":"projects/myprofect/topics/mytopic",
            "labelFilterAction":"include",
            "labelIds": ["INBOX"]
        };
        console.log(POST_request("POST","https://www.googleapis.com/gmail/v1/users/me/watch",false,tmp));

此查询通过返回视图主体

{
 "historyId": "678576",
 "expiration": "1555318376357"
}

0 个答案:

没有答案
相关问题