本文介绍了如何使文件共享使用谷歌驱动器API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 限时删除!! 我试图使用谷歌驱动api来获取文件,默认情况下文件不可共享,我想让它共享。 这是我的代码: - < script type =text / javascriptsrc =https://apis.google.com/js/api的.js>< /脚本> < script src =https://apis.google.com/js/client.js>< / script> //从Google Developers Console获取的浏览器API密钥。 //替换为您自己的浏览器API密钥或您自己的密钥。 var developerKey ='XXXXXXXXXX_ff_NX66eb-XXXXXXXXXXX'; //从Google Developers Console获取的客户端ID。用您自己的客户ID替换。 var clientId =XXXXXXXXXX-hs4ujaro5sc3d0g8qndtcq2tl279cfm1.apps.googleusercontent.com //替换为您自己的应用程序ID。 (它是您的客户ID中的第一个数字) var appId =XXXXXXXXXX; //用于访问用户的Drive项目的范围。 var scope = ['https://www.googleapis.com/auth/drive']; var pickerApiLoaded = false; var oauthToken; $ scope.addGoogleDriveFile = function(){ gapi.load('auth',{'callback':onAuthApiLoad}); gapi.load('picker',{'callback':onPickerApiLoad}); gapi.client.setApiKey(developerKey); 函数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 view = new google.picker.View(google.picker.ViewId.DOCS); var picker = new google.picker.PickerBuilder() .enableFeature(google.picker.Feature.NAV_HIDDEN) .setAppId(appId) .setOAuthToken(oauthToken) .addView(view) .addView(new google.picker.DocsUploadView()) .setDeveloperKey(developerKey) .setCallback(pickerCallback) .build(); picker.setVisible(true); } } //一个简单的回调实现。 函数pickerCallback(data){ if(data.action == google.picker.Action.PICKED){ console.log(data); gapi.client.load('drive','v2')。then(function(){ var request = gapi.client.drive.files.get({'fileId' :data.docs [0] .id }); request.execute(function(resp){ console.log(resp); }); }); 我收到以下回应: - {kind:drive#file,id:0B2Tb9btqdPGHeDl2NU5mYXRIaGc , 的etag: \ rCKCAyesbPCaBxGt0eDJcEBQNUI / MTQ1ODgxNDU0MDg2NA\ , selfLink: https://www.googleapis.com/drive/v2/files/0B2Tb9btqdPGHeDl2NU5mYXRIaGc webContentLink:https://docs.google.com/uc?id=0B2Tb9btqdPGHeDl2NU5mYXRIaGc&export=download,alternateLink:https://drive.google.com/file / d / 0B2Tb9btqdPGHeDl2NU5mYXRIaGc / view?usp = drivesdk,iconLink:https://ssl.gstatic.com/docs/doclist/images/icon_12_pdf_list.png,thumbnailLink: https://lh6.googleusercontent.com/iriNEeCbpP3OfCxntpc7sK7Dok03tcBJ86X4Be_sFgLXXG1Ta8b2WOM6pF6sEpS8ytaMvA = s220,title:POTSS-2012.pdf,mimeType:application / pdf, labels:{starred:false,hidden:false,trashed:f restricted:false,看过:true },createdDate:2016-03-24T09:57:00.978Z,modifiedDate:2016-03-24T10:15:40.864Z,modifiedByMeDate:2016-03-24T09:57:00.978Z,lastViewedByMeDate:2016 -03-24T09:57:00.978Z,markedViewedByMeDate:1970-01-01T00:00:00.000Z,版本:130,父母:[ {kind:drive#parentReference,id:0AGTb9btqdPGHUk9PVA,selfLink:https://www.googleapis .COM /驱动器/ V2 /文件/ 0B2Tb9btqdPGHeDl2NU5mYXRIaGc /父母/ 0AGTb9btqdPGHUk9PVA , parentLink: https://www.googleapis.com/drive/v2/files/0AGTb9btqdPGHUk9PVA,isRoot:true } ],downloadUrl:https://doc-0o-3g-docs.googleusercontent.com/docs/securesc/79n10ihd7kdei03rpq502qv5437nv33f $ b ?$ b / b6p7kupn55cuqeqq8l0d9vc1orf4bkiv / 1458813600000/07489391032112366212/07489391032112366212 / 0B2Tb9btqdPGHeDl2NU5mYXRIaGc E =下载和放大器; GD =真 userPe rmission :{ 种: 驱动器#许可, 的etag: \ rCKCAyesbPCaBxGt0eDJcEBQNUI / QKpDIyepJ3z6YHq0L3ksgvrZLSo\, 标识: 我, selfLink:https://www.googleapis.com/drive/v2/files/0B2Tb9btqdPGHeDl2NU5mYXRIaGc/permissions/me,角色:所有者,type:user},originalFilename:POTSS-2012.pdf,fileExtension:pdf,md5Checksum : 5b60cd94d0c0647c8828a60b8ce7efe4, 档案大小: 519097, quotaBytesUsed: 519097, ownerNames: XXXXX IT ],owner:[ {kind:drive#user,displayName:xxxx IT,isAuthenticatedUser :true,permissionId:07489391032112366212,emailAddress:it%xxxx.com @ xxxxx.com} ], lastModifyingUserName:xxxxx IT,lastModifyingUser:{kind:drive#user,displayName:xxxx IT,isAuthenticatedUser :true,permissionId :07489391032112366212,emailAddress:it%[email protected]},可编辑:true,可复制:true, writersCanShare:真实, 共享:虚假, explicitlyTrashed:虚假, appDataContents:虚假, headRevisionId:0B2Tb9btqdPGHT0pGV1FRRis0MmlJVmZsOE5uMU5UK2FNR0g0PQ ,spaces:[drive] } 这里我想更改shared:true ,而不是shared:false 如何更改权限? 任何想法? 谢谢解决方案 @arun您可能想要详细了解权限资源。对于共享为 true ,每个文件权限需要指定角色,键入和电子邮件地址或域。作为文件的所有者(文档,表格等),您需要提供适当的权限才能设置。 下面是一个例子,使用 Permissions.create (我建议使用 Drive API v3 ) POST https://www.googleapis.com/drive/v3/files/{fileId}/permissions? key = {YOUR_API_KEY} {角色:reader,type:user,emailAddress:xxxxxxxx @ xxx .com Drive Files.get : GET https://www.googleapis.com/drive/v3/files/{fileId}?fields=appProperties%2CfileExtension%2Ckind%2CmimeType%2Cshared&key={YOUR_API_KEY} {kind:drive#file,mimeType:application / vnd.google-apps.document,shared:true } 然而,如果您无法切换到云端硬盘v3,则仍然可以使用 Permission.insert 从Drive v2开始工作。希望这个帮助和祝你好运! I am trying to get the file using google drive api, by default the file is not shareable i want to make it shareable.Here is my code:- <script type="text/javascript" src="https://apis.google.com/js/api.js"></script><script src="https://apis.google.com/js/client.js"></script> // The Browser API key obtained from the Google Developers Console. // Replace with your own Browser API key, or your own key. var developerKey = 'XXXXXXXXXX_ff_NX66eb-XXXXXXXXXXX'; // The Client ID obtained from the Google Developers Console. Replace with your own Client ID. var clientId = "XXXXXXXXXX-hs4ujaro5sc3d0g8qndtcq2tl279cfm1.apps.googleusercontent.com" // Replace with your own App ID. (Its the first number in your Client ID) var appId = "XXXXXXXXXX"; // Scope to use to access user's Drive items. var scope = ['https://www.googleapis.com/auth/drive']; var pickerApiLoaded = false; var oauthToken; $scope.addGoogleDriveFile = function() { gapi.load('auth', {'callback': onAuthApiLoad}); gapi.load('picker', {'callback': onPickerApiLoad}); gapi.client.setApiKey(developerKey); } 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 searching images. function createPicker() { if (pickerApiLoaded && oauthToken) { var view = new google.picker.View(google.picker.ViewId.DOCS); var picker = new google.picker.PickerBuilder() .enableFeature(google.picker.Feature.NAV_HIDDEN) .setAppId(appId) .setOAuthToken(oauthToken) .addView(view) .addView(new google.picker.DocsUploadView()) .setDeveloperKey(developerKey) .setCallback(pickerCallback) .build(); picker.setVisible(true); } } // A simple callback implementation. function pickerCallback(data) { if (data.action == google.picker.Action.PICKED) { console.log(data); gapi.client.load('drive', 'v2').then(function() { var request = gapi.client.drive.files.get({ 'fileId': data.docs[0].id }); request.execute(function(resp) { console.log(resp); }); }); } }I am getting the following response:-{ "kind": "drive#file", "id": "0B2Tb9btqdPGHeDl2NU5mYXRIaGc", "etag": "\"rCKCAyesbPCaBxGt0eDJcEBQNUI/MTQ1ODgxNDU0MDg2NA\"", "selfLink": "https://www.googleapis.com/drive/v2/files/0B2Tb9btqdPGHeDl2NU5mYXRIaGc", "webContentLink": "https://docs.google.com/uc?id=0B2Tb9btqdPGHeDl2NU5mYXRIaGc&export=download", "alternateLink": "https://drive.google.com/file/d/0B2Tb9btqdPGHeDl2NU5mYXRIaGc/view?usp=drivesdk", "iconLink": "https://ssl.gstatic.com/docs/doclist/images/icon_12_pdf_list.png", "thumbnailLink": "https://lh6.googleusercontent.com/iriNEeCbpP3OfCxntpc7sK7Dok03tcBJ86X4Be_sFgLXXG1Ta8b2WOM6pF6sEpS8ytaMvA=s220", "title": "POTSS-2012.pdf", "mimeType": "application/pdf", "labels": { "starred": false, "hidden": false, "trashed": false, "restricted": false, "viewed": true }, "createdDate": "2016-03-24T09:57:00.978Z", "modifiedDate": "2016-03-24T10:15:40.864Z", "modifiedByMeDate": "2016-03-24T09:57:00.978Z", "lastViewedByMeDate": "2016-03-24T09:57:00.978Z", "markedViewedByMeDate": "1970-01-01T00:00:00.000Z", "version": "130", "parents": [ { "kind": "drive#parentReference", "id": "0AGTb9btqdPGHUk9PVA", "selfLink": "https://www.googleapis.com/drive/v2/files/0B2Tb9btqdPGHeDl2NU5mYXRIaGc/parents/0AGTb9btqdPGHUk9PVA", "parentLink": "https://www.googleapis.com/drive/v2/files/0AGTb9btqdPGHUk9PVA", "isRoot": true } ], "downloadUrl": "https://doc-0o-3g-docs.googleusercontent.com/docs/securesc/79n10ihd7kdei03rpq502qv5437nv33f/b6p7kupn55cuqeqq8l0d9vc1orf4bkiv/1458813600000/07489391032112366212/07489391032112366212/0B2Tb9btqdPGHeDl2NU5mYXRIaGc?e=download&gd=true", "userPermission": { "kind": "drive#permission", "etag": "\"rCKCAyesbPCaBxGt0eDJcEBQNUI/QKpDIyepJ3z6YHq0L3ksgvrZLSo\"", "id": "me", "selfLink": "https://www.googleapis.com/drive/v2/files/0B2Tb9btqdPGHeDl2NU5mYXRIaGc/permissions/me", "role": "owner", "type": "user" }, "originalFilename": "POTSS-2012.pdf", "fileExtension": "pdf", "md5Checksum": "5b60cd94d0c0647c8828a60b8ce7efe4", "fileSize": "519097", "quotaBytesUsed": "519097", "ownerNames": [ "xxxxx IT" ], "owners": [ { "kind": "drive#user", "displayName": "xxxx IT", "isAuthenticatedUser": true, "permissionId": "07489391032112366212", "emailAddress": "it%xxxx [email protected]" } ], "lastModifyingUserName": "xxxxx IT", "lastModifyingUser": { "kind": "drive#user", "displayName": "xxxx IT", "isAuthenticatedUser": true, "permissionId": "07489391032112366212", "emailAddress": "it%[email protected]" }, "editable": true, "copyable": true, "writersCanShare": true, "shared": false, "explicitlyTrashed": false, "appDataContents": false, "headRevisionId": "0B2Tb9btqdPGHT0pGV1FRRis0MmlJVmZsOE5uMU5UK2FNR0g0PQ", "spaces": [ "drive" ]}Here i want to change "shared": true Instead of "shared": falseHow can i change its permission?Any Idea?Thanks 解决方案 @arun you may want to read more about Permission resource in Drive API. For "shared" to be true, each file permission needs to specifies a role, type, and email address or domain. As an owner of the file (Docs, Sheets, etc.), you will need to provide the appropriate permission to be set to.Here is an example, using the Permissions.create (I recommend using Drive API v3) POST https://www.googleapis.com/drive/v3/files/{fileId}/permissions?key={YOUR_API_KEY}{ "role": "reader", "type": "user", "emailAddress": "[email protected]"}Response from the Drive Files.get: GET https://www.googleapis.com/drive/v3/files/{fileId}?fields=appProperties%2CfileExtension%2Ckind%2CmimeType%2Cshared&key={YOUR_API_KEY}{ "kind": "drive#file", "mimeType": "application/vnd.google-apps.document", "shared": true}However, if you are unable to switch to the Drive v3, you can still use the Permission.insert from Drive v2 to do the job. Hope this helpful and good luck! 这篇关于如何使文件共享使用谷歌驱动器API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的.. 09-07 00:00