在Google驱动器选择器中上传后的文件绝对路径 [英] files absolute path after upload in Google drive picker
问题描述
我正在尝试创建一个具有图像上传功能的网络应用程序,该功能会将图像上传到用户的Google驱动器&通过使用Google驱动器选择器返回上传图像的绝对路径
I'm trying to create a web app with an image upload feature which will upload an image to a user's google drive & returns absolute path of the uploaded image by making use of google drive picker
我的生成选择器功能
var picker=new google.picker.PickerBuilder()
.addView(new google.picker.DocsUploadView())
.addView(new google.picker.DocsView())
.setOAuthToken(oauthToken)
.setDeveloperKey("[MY_API]")
.setCallback(imageReceived)
.build();
picker.setVisible(true);
回调函数
function imageReceived(data)
{
if (data.action == google.picker.Action.PICKED) {
console.log(data.docs[0].url);
}
我能够生成选择器,并且文件上传也能正常工作
I'm able to generate the picker and the file upload also works
但是在我的上传回调函数url中,此表单会返回
however in my upload callback function url is returned in this form
https://docs.google.com/file/d/[something]/edit?usp = drive_web
此url不能在img标签中使用,因为它不是上传图像的绝对路径 那么我应该如何获得绝对路径?
this url can't be used in an img tag as it is not the absolute path of the image uploaded So how should I get the absolute path ?
推荐答案
查看文档( https ://developers.google.com/picker/docs/results ),它指出您应该改用"embed_URL".
Looking at the documentation (https://developers.google.com/picker/docs/results), it states that you should use "embed_URL" instead.
您可以轻松调试并检查响应.我将Google Picker集成到了我的一个项目中,因此能够获取图像的完整URL.
You can easily debug and check the response. I integrated google picker in one of my projects, I was able to get full url to the image.
我已经检查了我的代码,实际上我不得不使用google file api来获取链接.那就是对 https://www.googleapis.com/drive/v2/文件/ {id}传递文件ID.您可以在img标签中使用的绝对网址位于"webContentLink"属性中.
I've checked my code, and I actually had to use google file api to get the link. That is to make an ajax call to https://www.googleapis.com/drive/v2/files/{id} passing file id. Absolute url that you can use in img tag is in 'webContentLink' property.
请注意,进行该Ajax调用时,您必须在请求标头中转发访问令牌:
Note that you have to forward access token in request header when making that ajax call:
var token = gapi.auth.getToken();
$.ajax({....,
beforeSend: function(n) {
n.setRequestHeader("Authorization", "Bearer " + token.access_token)
}}),...
});
这篇关于在Google驱动器选择器中上传后的文件绝对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!