繁体   English   中英

如何在我的cordova phonegap android应用上从Google云端硬盘获取文件和文件夹列表?

[英]How to get list of files and folders from Google Drive on my cordova phonegap android app?

我的cordova应用程序身份验证完成后,我也获得了访问令牌。 当我调用获取文件列表的方法时。 我显示以下错误消息403 forbbidan

    "domain": "usageLimits",
    "reason": "dailyLimitExceededUnreg",
    "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.",
    "extendedHelp": "https://code.google.com/apis/console"
     "code": 403,
  "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup."

如下所示的代码

var googleapi = {
    authorize : function(options) {

        var deferred = $.Deferred();
        var authUrl = 'https://accounts.google.com/o/oauth2/auth?' + $.param({
            client_id : options.client_id,
            redirect_uri : options.redirect_uri,

            response_type : 'code',
            scope : options.scope
        });

        var authWindow = window.open(authUrl, '_blank',
                'location=no,toolbar=no');
        $(authWindow).on('loadstart', function(e) {

            var url = e.originalEvent.url;
            var code = /\?code=(.+)$/.exec(url);
            var error = /\?error=(.+)$/.exec(url);

            if (code || error) {
                authWindow.close();
            }
            if (code) {
                $.post('https://accounts.google.com/o/oauth2/token', {
                    code : code[1],
                    client_id : options.client_id,
                    client_secret : options.client_secret,
                    redirect_uri : options.redirect_uri,
                    grant_type : 'authorization_code'
                }).done(function(data) {
                    deferred.resolve(data);
                }).fail(function(response) {
                    alert("fail");
                    deferred.reject(response.responseJSON);
                });
            } else if (error) {
                alert("reject");
                deferred.reject({
                    error : error[1]
                });
            }
        });

        return deferred.promise();
    }
};

$(document)
        .on(
                'deviceready',
                function() {
                    var deferred = $.Deferred();
                    var $loginButton = $('#login a');
                    var $loginStatus = $('#login p');

                    $loginButton
                            .on(
                                    'click',
                                    function() {
                                    googleapi
                                                .authorize(
                                                        {
                                                        //  client_id : '10901099134-tenshqgdrulrb4r483lcuaami7e7dldt.apps.googleusercontent.com',
                                                        //  client_secret : 'CJ7icaUBFWEwJCV9j_NPqRG8', 
                                                            client_id : '966198539347-h7kjthlnl9t3glsoe0uvgh4km2iogbll.apps.googleusercontent.com',
                                                            client_secret : 'M9CWYuz09StWMZpLin3hb6dt',
                                                            redirect_uri : 'http://localhost',
                                                            scope : 'https://www.googleapis.com/auth/drive.file'
                                                        })
                                                .done(
                                                        function(data) {


                                                            $loginStatus
                                                                    .html('Access security Token: '
                                                                            + data.access_token);
                                                            var Access_Token = data.access_token;
                                                            loadDriveApi();
                                            })
                                                .fail(
                                                        function(data) {
                                                            $loginStatus
                                                                    .html(data.error);
                                                        });

                                    });
                });


function loadDriveApi() {
    gapi.client.load('drive', 'v3',listFiles);
  }
function listFiles() {
  var request = gapi.client.drive.files.list({
      'pageSize': 10,
      'fields': "nextPageToken, files(id, name)"
    });

    request.execute(function(resp) {
      appendPre('Files:');
  var files = resp.files;
      if (files && files.length > 0) {
        for (var i = 0; i < files.length; i++) {
          var file = files[i];
          appendPre(file.name + ' (' + file.id + ')');
        }
      } else {
        appendPre('No files found.');
      }
    });
}

function appendPre(message) {
  var pre = document.getElementById('output');
  var textContent = document.createTextNode(message + '\n');
  pre.appendChild(textContent);
} 

在我的cordova android应用程序中执行此代码后,未返回任何文件。 调试器显示403 forbiddan。

作为错误消息:

"message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup."

说,您的未经授权使用的每日限制已超出。

我看到您提到您正在使用身份验证api。 在访问驱动器api时,请检查您是否正确连接了access_token ,以确保您的API调用使用了经过身份验证的信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM