【问题标题】:GMail API with JS AJAX calls带有 JS AJAX 调用的 GMail API
【发布时间】:2016-02-28 19:18:55
【问题描述】:

我们使用通用的学生信息系统来管理我们的学校。它在客户端很容易定制,并且有几个地方我们希望在某些操作发生时触发电子邮件(表单提交、页面加载等)。无法使用 PHP、ASP 或 Java 等服务器端编程语言。

我们想做的是使用 AJAX 调用来发送电子邮件。我有这个完全在 JavaScript 中工作,但我必须使用我要发送的 GMail 帐户表单登录。我们需要对其进行一次身份验证(或者只要不记名令牌有效),然后在用户操作发生后发送电子邮件。

由于客户端语言似乎能够访问这些端点并发送电子邮件,而发送帐户没有在浏览器会话中“登录”,这对我来说似乎是可能的。是吗?

我也意识到创建一个使用 GMail 作为 SMTP 中继的服务器端脚本是一种选择,但许多使用 Google Apps For Education 的学校根本没有可以托管此类脚本的网站服务器。客户端插件才是我们真正想要构建的。

【问题讨论】:

    标签: javascript jquery ajax email gmail-api


    【解决方案1】:

    你可以试试这个,只需替换 CLIENT_ID 值:

    此范围是只读,但您可以根据需要执行的操作进行更改。

    <html>
      <head>
        <script type="text/javascript">
          // Your Client ID can be retrieved from your project in the Google
          // Developer Console, https://console.developers.google.com
          var CLIENT_ID = '<YOUR_CLIENT_ID>';
    
      var SCOPES = ['https://www.googleapis.com/auth/gmail.readonly'];
    
      /**
       * Check if current user has authorized this application.
       */
      function checkAuth() {
        gapi.auth.authorize(
          {
            'client_id': CLIENT_ID,
            'scope': SCOPES.join(' '),
            'immediate': true
          }, handleAuthResult);
      }
    
      /**
       * 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();
        } else {
          // Show auth UI, allowing the user to initiate authorization by
          // clicking authorize button.
          authorizeDiv.style.display = 'inline';
        }
      }
    
      /**
       * 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);
        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)
            }
          } else {
            appendPre('No Labels found.');
          }
        });
      }
    
      /**
       * Append a pre element to the body containing the given message
       * as its text node.
       *
       * @param {string} message Text to be placed in pre element.
       */
      function appendPre(message) {
        var pre = document.getElementById('output');
        var textContent = document.createTextNode(message + '\n');
        pre.appendChild(textContent);
      }
    
    </script>
    
    
      <script src="https://apis.google.com/js/client.js?onload=checkAuth">
        </script>
      </head>
      <body>
        <div id="authorize-div" style="display: none">
          <span>Authorize access to Gmail API</span>
          <!--Button for the user to click to initiate auth sequence -->
          <button id="authorize-button" onclick="handleAuthClick(event)">
            Authorize
          </button>
        </div>
        <pre id="output"></pre>
      </body>
    </html>
    

    来源:Gmail API - Quickstart

    【讨论】:

    • 我按照那个例子走下去,但问题是,它会提示您输入您的 Google 帐户。这很好,但我们确实需要在没有人登录的情况下静默发送电子邮件。我使用的其他 API 允许您使用 ID 和密码获取不记名令牌。是否有一种我们可以使用 JavaScript 进行调整的客户端方法?我不是新手开发人员,但我发现 Google 文档很难浏览。
    猜你喜欢
    • 2016-12-09
    • 1970-01-01
    • 1970-01-01
    • 2019-05-13
    • 2017-10-15
    • 2014-04-09
    • 2016-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多