如何从Google Picker JavaScript API获取直接下载网址? [英] How to get a direct download URL from Google Picker JavaScript API?

查看:258
本文介绍了如何从Google Picker JavaScript API获取直接下载网址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Google Picker API获取文件的直接下载网址,以便我可以选择文件并将此网址传递给服务器端代码,以便将该文件的副本下载并存储在服务器上。



我可以通过选取器API进行授权,并获取包括文件名和预览网址在内的所选文件的信息(它被简单地称为URL此项目在JSON响应文档中:

解决方案

您可能想要尝试此文档。注意 url 之前被初始化,如果语句:

 函数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;
}

另外,在授权中,设置 AppId 值,然后选择具有该应用当前OAuth 2.0令牌的用户帐户。请注意, AppId 集合和用于授权访问用户文件的客户端ID必须包含在同一个应用程序中。然后,在成功获取 fileId 后,您可以使用 files.get 。默认情况下,响应主体会响应一个文件资源,其中包括 downloadUrl



有关更多见解,请参阅相关的 SO贴子


I'm trying to get a direct download URL for a file using Google's Picker API so that I can choose a file and pass this URL to server side code to download and store a copy of the item on the server.

I'm able to authorize through the picker API and get info of a picked file including the file name and preview URL (which is confusingly referred to as simply "A URL to this item" in the JSON response docs: https://developers.google.com/picker/docs/results)

I noticed that there is a post about using the Drive API to get a direct download URL here: Get google drive file download URL

However when I do this in my picker callback function (based on the docs here: https://developers.google.com/picker/docs/)

I get an error of:

 "Project [number here] is not found and cannot be used for API calls. If it is recently created, enable Drive API by visiting https://console.developers.google.com/apis/api/drive.googleapis.com/overview?project=[project number here] then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry."

I have the API enabled in my developer console and the URL added to the JS allowed origins.

The documentation is very confusing and there seems to be 3 versions of the REST API to use with Drive which is based on an gapi.auth2 object whereas the picker api uses gapi.auth object.

I'm not sure if I need to authenticate again using the Google Drive API before performing the GET request. This all seems very messy and I believe there must be an easier approach for what is a simple request!

My picker callback function:

  pickerCallback: function(data) {
    if (data[google.picker.Response.ACTION] == google.picker.Action.PICKED) {
      var doc = data[google.picker.Response.DOCUMENTS][0];
      var fileName = doc[google.picker.Document.NAME];
      var url = doc[google.picker.Document.URL];
      var docId = doc[google.picker.Document.ID];
      var request = null;

      gapi.client.load('drive', 'v2', function() {
        request = gapi.client.drive.files.get({
          'fileId': docId
        });
        request.execute(function(resp){
          console.log(resp);
        });
      });


     //Write upload details to page
     //Populate hidden field
    }

Developer console screen - The first app is the picker API the second is for the Drive API:

解决方案

You may want to try the simple callback implementation shown in this documentation. Notice that url was initialized before the if statement:

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;
}    

Also, in authorizing, set the AppId value and choose the user account with the app's current OAuth 2.0 token. Note that the AppId set and the client ID used for authorizing access to a user's files must be contained in the same app. Then, after successfully obtaining the fileId, you can then send request using files.get. By default, this responds with a Files resource in the response body which includes downloadUrl.

For additional insights, see this related SO post.

这篇关于如何从Google Picker JavaScript API获取直接下载网址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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