身份验证后未显示Google Picker

时间:2019-01-27 13:03:35

标签: javascript google-api google-drive-api google-picker google-drive-picker

我正在尝试使用Google Drive Picker API浏览Google云端硬盘中的文件。我正在关注Google页面上的“ Hello World”教程。由于某些原因,当我单击“身份验证”并选择我的帐户后,Google选择器没有弹出。

代码

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

    <script type="text/javascript">

      // The Browser API key obtained from the Google API Console.
      var developerKey = 'AIzaSyAPQ6_NT8CT7NA1yXN0vF979tuoqN4pQ64';

      // The Client ID obtained from the Google API Console. Replace with your own Client ID.
      var clientId = '475080196216-04bp4cln61011q4vsocb69gve0tvt6ek.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('auth2', onAuthApiLoad);
        gapi.load('picker', onPickerApiLoad);
      }

      function onAuthApiLoad() {
        var authBtn = document.getElementById('auth');
        authBtn.disabled = false;
        authBtn.addEventListener('click', function() {
          gapi.auth2.authorize({
            client_id: clientId,
            scope: scope
          }, 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>
    <button type="button" id="auth" disabled>Authenticate</button>

    <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>

托管链接https://shihankhan.com/research/picker.html
教程链接https://developers.google.com/picker/docs/#hiworld

不知道我在做什么错。我什至在控制台中都没有收到任何错误。如果有人可以向我展示如何使其工作,那将非常有帮助。谢谢!

1 个答案:

答案 0 :(得分:0)

Nvm,现在来对了。不知道那时发生了什么,但我认为您必须在首次设置后等待一段时间。