用户拒绝授权时使用Google Drive api并运行脚本

时间:2014-05-06 03:56:13

标签: javascript api authorization

我使用Google Drive Picker api,当用户同意允许访问他们的YouTube帐户时,它正常工作,但是如果用户点击了#34,有没有办法运行该功能;取消"而不是"接受"?我已经搜索了很多,但是找不到关于在哪里捕捉这个动作的信息!

Javascript在下面,感谢您的帮助!

<script type="text/javascript">

  // The API developer key obtained from the Google Developers Console.
  var developerKey = '???';

  // The Client ID obtained from the Google Developers Console.
  var clientId = '???.apps.googleusercontent.com';

  // Scope to use to access user's photos.
  var scopes = ['https://www.googleapis.com/auth/youtube'];
  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': scopes.join(' '),
          '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.VIDEO_SEARCH).
          addView(google.picker.ViewId.YOUTUBE).
          setOAuthToken(oauthToken).
          setDeveloperKey(developerKey).
          setCallback(pickerCallback).
          build();
      picker.setVisible(true);
    }
  }

  // A simple callback implementation.
  function pickerCallback(data) {
    var url = '';
    if (data[google.picker.Response.ACTION] == google.picker.Action.PICKED) {
      var doc = data[google.picker.Response.DOCUMENTS][0];
      url = doc[google.picker.Document.URL];

      parent.document.getElementById("linkstr").value = url;
      window.parent.savelink();
    }
    else
    {
      window.parent.youtubereload();
    }
  }
</script>

0 个答案:

没有答案