【问题标题】:Google Picker API -- Button click eventGoogle Picker API -- 按钮点击事件
【发布时间】:2016-01-09 00:41:00
【问题描述】:

我想知道在用户点击按钮后如何加载 Google Drive Picker 窗口。

我使用 Dropbox 网站上提供的代码轻松实现了 Dropbox 的选择器 API。

现在,Google 在https://developers.google.com/picker/docs/index 提供的以下“Hello World”应用程序代码不起作用。

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>

    <script type="text/javascript">

      // The Browser API key obtained from the Google Developers Console.
      var developerKey = 'xxxxxxxYYYYYYYY-12345678';

      // The Client ID obtained from the Google Developers Console. Replace with your own Client ID.
      var clientId = "1234567890-abcdefghijklmnopqrstuvwxyz.apps.googleusercontent.com"

      // Scope to use to access user's photos.
      var scope = ['https://www.googleapis.com/auth/photos'];

      var pickerApiLoaded = false;
      var oauthToken;

      // Use the API Loader script to load google.picker and gapi.auth.
      function onApiLoad() {
        gapi.load('auth', {'callback': onAuthApiLoad});
        gapi.load('picker', {'callback': onPickerApiLoad});
      }

      function onAuthApiLoad() {
        window.gapi.auth.authorize(
            {
              'client_id': clientId,
              'scope': scope,
              'immediate': false
            },
            handleAuthResult);
      }

      function onPickerApiLoad() {
        pickerApiLoaded = true;
        createPicker();
      }

      function handleAuthResult(authResult) {
        if (authResult && !authResult.error) {
          oauthToken = authResult.access_token;
          createPicker();
        }
      }

      // Create and render a Picker object for picking user Photos.
      function createPicker() {
        if (pickerApiLoaded && oauthToken) {
          var picker = new google.picker.PickerBuilder().
              addView(google.picker.ViewId.PHOTOS).
              setOAuthToken(oauthToken).
              setDeveloperKey(developerKey).
              setCallback(pickerCallback).
              build();
          picker.setVisible(true);
        }
      }

      // A simple callback implementation.
      function pickerCallback(data) {
        var url = 'nothing';
        if (data[google.picker.Response.ACTION] == google.picker.Action.PICKED) {
          var doc = data[google.picker.Response.DOCUMENTS][0];
          url = doc[google.picker.Document.URL];
        }
        var message = 'You picked: ' + url;
        document.getElementById('result').innerHTML = message;
      }
    </script>
  </head>
  <body>
    <div id="result"></div>

    <!-- The Google API Loader script. -->
    <script type="text/javascript" src="https://apis.google.com/js/api.js?onload=onApiLoad"></script>
  </body>
</html>

我添加了我的 API 密钥和客户端 ID,然后,我尝试使用 &lt;button type="button" class="btn btn-danger" id="result"&gt; 在 Bootstrap 按钮上应用代码,而不是底部的 &lt;div id="result"&gt;,但它不起作用。

我的问题是:有没有更好的 Google Picker API 处理按钮点击的例子?

【问题讨论】:

标签: javascript twitter-bootstrap google-picker


【解决方案1】:

您是否尝试过将以下内容用于谷歌驱动器选择器?

<script type="text/javascript" src="https://apis.google.com/js/api.js"></script>
<button  id="googleDrive" onClick="onApiLoad(); return false;" value="G_driver">Google Drive</button>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-28
    • 2011-12-30
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多