>本文展示了使用Gmail Rest API和JavaScript构建基本的Gmail收件箱查看器。 我们将利用jQuery和Bootstrap提高效率。完整的代码可在我们的github存储库中可用。
>
密钥功能:
>访问Google的开发人员控制台。
创建或选择一个项目。http://localhost
>我们将使用Google的JavaScript客户端库进行简化的交互。 我们的HTML文件包括jQuery,Bootstrap和Google客户端库:
> 。>
用户身份验证:
<code class="language-html"><!DOCTYPE html> <title>Gmail API demo</title> <meta charset="UTF-8"> <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css"> <style> .hidden { display: none; } </style> <div class="container"> <h1>Gmail API demo</h1> <button id="authorize-button" class="btn btn-primary hidden">Authorize</button> <table class="table table-striped table-inbox hidden"> <thead> <tr> <th>From</th> <th>Subject</th> <th>Date/Time</th> </tr> </thead> <tbody></tbody> </table> </div> </code>
,YOUR_CLIENT_ID
>,YOUR_API_KEY
,
, 函数使用OAuth 2.0处理用户身份验证。 (此处省略了这些功能的代码,但可在GitHub repo中使用)。
>。handleClientLoad()
>提取和显示收件箱消息:checkAuth()
>
handleAuthClick()
handleAuthResult()
函数使用gmail API获取最后十个消息,并调用loadGmailApi()
将它们添加到表中。
>。 显示电子邮件内容:
displayInbox()
>模态窗口使用iframe安全地显示电子邮件内容,从而防止了与应用程序的样式发生的潜在冲突。 (此处省略了此功能的代码,但在GitHub repo中可用)。appendMessageRow()
>
appendMessageRow()
>提供的CSS样式在模态内iframe。 (此处省略了此处的代码,但可以在GitHub repo中使用)。>
进一步的改进:
以上是使用Gmail JavaScript API掌握收件箱的详细内容。更多信息请关注PHP中文网其他相关文章!