【问题标题】:How to get list of files and folders from Google Drive on my cordova phonegap android app?如何在我的 cordova phonegap android 应用程序上从 Google Drive 获取文件和文件夹列表?
【发布时间】:2016-11-29 17:17:48
【问题描述】:

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

    "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 禁止。

【问题讨论】:

  • 你可以在类似的 SO question 上查看我的答案

标签: javascript android cordova


【解决方案1】:

作为错误信息:

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

说,您的未经身份验证使用的每日限制已超出。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多