谷歌驱动公共

时间:2015-01-29 07:05:59

标签: javascript html url google-drive-api

我正在使用Google云端硬盘选择器,一旦从Google云端硬盘中选择了一个项目,就会生成一个网址。问题是该URL只能由所有者访问,因此不公开。我希望URL可以公开访问。

因此,我查看了以下指南: https://developers.google.com/picker/docs/reference#Response.Documents

并认为Document.AUDIENCE类最适用,但我不知道如何将该条件添加到下面的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 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>

2 个答案:

答案 0 :(得分:4)

向所选文件添加公共权限。您需要使用驱动器API来添加文件权限

请参阅https://developers.google.com/drive/v2/reference/permissions/insert

您需要在文件中插入权限,并将角色设置为&#39; reader&#39;和任何人的类型#39;

您可以参考示例中的javascript实现

/**
 * Insert a new permission.
 *
 * @param {String} fileId ID of the file to insert permission for.
 * @param {String} value User or group e-mail address, domain name or
 *                       {@code null} "default" type.
 * @param {String} type The value "user", "group", "domain" or "default".
 * @param {String} role The value "owner", "writer" or "reader".
 */
function insertPermission(fileId, value, type, role) {
  var body = {
    'value': value,
    'type': type,
    'role': role
  };
  var request = gapi.client.drive.permissions.insert({
    'fileId': fileId,
    'resource': body
  });
  request.execute(function(resp) { });
}

答案 1 :(得分:0)

详细说明@ Sam的答案,以下是不使用gapi.client.drive Javascript API的方法。

假设some_id是您的文档ID:

request = gapi.client.request({
  path: '/drive/v2/files/some_id/permissions',
  method: 'POST',
  body: {
    role: 'reader'
    type: 'anyone'
  }
});
request.execute(function (res)  {
  console.log(res);
})

通过此正文向https://content.googleapis.com/drive/v2/files/some_id/permissions?alt=json生成请求:{role: "reader", type: "anyone"}

以下是您将获得的结果:

{
 "kind": "drive#permission",
 "etag": "some etage",
 "id": "anyone",
 "selfLink": "https://www.googleapis.com/drive/v2/files/some_id/permissions/anyone",
 "role": "reader",
 "type": "anyone"
}