如何触发点击“保存到Google云端硬盘”按钮

时间:2016-06-02 01:08:22

标签: google-drive-api

如何在“保存到Google云端硬盘”按钮上触发“点击”,我的意思是,我有自己的“保存到Google云端硬盘”按钮设计。 是否有任何编程触发器在Javascript中打开“保存到谷歌驱动器”弹出窗口?

由于

修改

我想修改这个api

https://developers.google.com/drive/v3/web/savetodrive#getting_started

1 个答案:

答案 0 :(得分:0)

您可以尝试:

HTML:

<html>

<head>
  <title>Save to Drive</title>
</head>

<body>
  <input type="button" id="doitButton" value="Save Chat History in Drive">
  <input type="button" id="authorizeButton" value="Authorize" onClick="checkAuth()">
  <script type="text/javascript" src="https://apis.google.com/js/client.js?onload=handleClientLoad"></script>
</body>

</html>

JS:

var CLIENT_ID = 'CLIENT_ID';
var SCOPES = 'https://www.googleapis.com/auth/drive';

function handleClientLoad() {
  window.setTimeout(checkAuth, 1);
}

function checkAuth() {
  gapi.auth.authorize({
    'client_id': CLIENT_ID,
    'scope': SCOPES,
    'immediate': true
  }, handleAuthResult);
}

function handleAuthResult(authResult) {
  var authButton = document.getElementById('authorizeButton');
  var doitButton = document.getElementById('doitButton');
  authButton.style.display = 'none';
  doitButton.style.display = 'none';
  if (authResult && !authResult.error) {
    // Access token has been successfully retrieved, requests can be sent to
    // the API.
    doitButton.style.display = 'block';
    doitButton.onclick = uploadFile;
  } else {
    // No access token could be retrieved, show the button to start the
    // authorization flow.
    authButton.style.display = 'block';
    authButton.onclick = function() {
      gapi.auth.authorize({
        'client_id': CLIENT_ID,
        'scope': SCOPES,
        'immediate': false
      }, handleAuthResult);
    };
  }
}

function uploadFile(evt) {
  gapi.client.load('drive', 'v2', function() {
    insertFile();
  });
}

function insertFile() {
  //YOUR INSERT CODE
}

正如您所看到的,handleAuthResult()获取OAuth结果并有条件地检查授权,如果成功将添加onclick =&#34; uploadFile()&#34;。