获取存储容量的Google Drive API方法会产生错误

时间:2017-03-02 11:42:14

标签: google-drive-realtime-api

参考文献: https://developers.google.com/drive/v2/reference/about/get

当我使用about方法时,得到以下错误:

  

发生错误:{“error”:{“errors”:[{“domain”:“global”,   “reason”:“required”,“message”:“'fields'参数是必需的   对于此方法。“,”locationType“:”参数“,”位置“:”字段“}   ],“code”:400,“message”:“'fields'参数是必需的   这种方法。“}}

这是我的代码:

function printAbout() {
    try {
        $about = $this->service->about->get(array('fields' => 'name'));
        print "Current user name: " . $about->getName();
        print "Root folder ID: " . $about->getRootFolderId();
        print "Total quota (bytes): " . $about->getQuotaBytesTotal();
        print "Used quota (bytes): " . $about->getQuotaBytesUsed();
    } catch (Exception $e) {
        print "An error occurred: " . $e->getMessage();
    }
}

2 个答案:

答案 0 :(得分:1)

如果你还没弄明白,我已经得到你的回答了。

您正在链接到文档的V2版本,但您在代码中使用V3。在V2中,您不需要指定字段param,因此它应该通过API测试程序完全运行。您可能在帖子中链接了V2文档,但是自己使用V3文档进行测试。

/ drive / about / get API从V2更改为V3。具体而言,返回信息的布局没有不同。因此,当您将“name”指定为字段时,它不存在,并且您继续得到该(相当无用的)错误。

解决问题

在'fields'参数中指定'user'而不是'name',它将在result.user.displayName中返回您想要的数据。

<强>文档

V2 About/Get Result Documentation

V3 About/Get Result Documentation

我希望这些解释很有帮助,您可以正确获取数据!

答案 1 :(得分:0)

对于Node.js,这可行:

var drive = google.drive({ version: 'v3', auth: authClient }); 
drive.about.get({
   auth: authClient,
   fields: ["storageQuota"]
}, function (err, resp) {
   if(err) console.log(err);
   console.log(resp)
});