谷歌驱动器公开 [英] Google drive Public

查看:249
本文介绍了谷歌驱动器公开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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



和感觉Document.AUDIENCE类是最适用的,但是我不知道如何将该条件添加到下面的Google Drive选择器示例代码中。



任何帮助都可以非常感谢。

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

< script type =text / javascript>

//从Google Developers Console获取的浏览器API密钥。
var developerKey ='xxxxxxxYYYYYYYY-12345678';

//从Google Developers Console获取的客户端ID。用您自己的客户ID替换。
var clientId =1234567890-abcdefghijklmnopqrstuvwxyz.apps.googleusercontent.com

//用于访问用户照片的范围。
var scope = ['https://www.googleapis.com/auth/photos'];

var pickerApiLoaded = false;
var oauthToken;

//使用API​​加载程序脚本加载google.picker和gapi.auth。
函数onApiLoad(){
gapi.load('auth',{'callback':onAuthApiLoad});
gapi.load('picker',{'callback':onPickerApiLoad});


函数onAuthApiLoad(){
window.gapi.auth.authorize(
{
'client_id':clientId,
'范围':范围,
'立即':false
},
handleAuthResult);
}

函数onPickerApiLoad(){
pickerApiLoaded = true;
createPicker();
}

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

//创建并呈现一个拾取器对象来拾取用户照片。
函数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);
}
}

//一个简单的回调实现。
函数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 ='您选择了:'+ url;
document.getElementById('result')。innerHTML = message;
}
< / script>
< / head>
< body>
< div id =result>< / div>

<! - Google API加载器脚本。 - >
< script type =text / javascriptsrc =https://apis.google.com/js/api.js?onload=onApiLoad>< / script>
< / body>


解决方案

将公共权限添加到选定的文件。您需要使用驱动器api添加文件权限




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




你需要为角色设置为'reader'并且类型为'any'的文件插入一个权限




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

  / ** 
*插入新权限。
*
* @param {String} fileId要插入权限的文件的ID。
* @param {String}值用户或组电子邮件地址,域名或
* {null}默认类型。
* @param {字符串}输入值user,group,domain或default。
* @param {字符串}角色值所有者,作者或读者。
* /
函数insertPermission(fileId,value,type,role){
var body = {$ b $'value':value,
'type':type,
'角色':角色
};
var request = gapi.client.drive.permissions.insert({$ b $'fileId':fileId,
'resource':body
});
request.execute(function(resp){});
}


I am working with Google drive picker, where once an item is selected from Google drive a url is produced. The problem is that that url is only accessible by the owner, and hence not public. I want the URL to be publicly accessible.

Hence, i've looked into the following guide: https://developers.google.com/picker/docs/reference#Response.Documents

and feel that the Document.AUDIENCE class would be best applicable, however I do not know how to add that criteria into the below google drive picker example code.

Any help would be greatly appreciated.

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

解决方案

To add public permissions to the selected file. You will need to use the drive api to add file permissions

Please see https://developers.google.com/drive/v2/reference/permissions/insert

You will need to insert a permission to the file with role set to 'reader' and the type to 'anyone'

You can refer to the javascript implementation in the example

/**
 * 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) { });
}

这篇关于谷歌驱动器公开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆