如何使用 Google Drive API 列出域中所有共享云端硬盘中的文件?

时间:2024-04-09

浏览:127

作者:佚名

本篇文章向大家介绍《如何使用 Google Drive API 列出域中所有共享云端硬盘中的文件?》,主要包括,具有一定的参考价值,需要的朋友可以参考一下。

问题内容

我正在尝试列出在我的组织内创建的所有共享云端硬盘内的所有文件。虽然,当我尝试获取特定驱动器的文件时,我得到:

error 403: the attempted action requires shared drive membership., teamdrivemembershiprequired

这是有道理的,因为我的用户不属于该共享云端硬盘。我可以冒充作为所有者的用户,并且我认为这会起作用,尽管 drives list(我用来获取共享云端硬盘列表的 api)不提供有关共享云端硬盘所有者的详细信息。

如果我冒充管理员用户或使用服务帐户来获取 driveid,我会收到相同的 403 错误。

package main

imports ... 

func main() {
    d := drive.new(drive.service{
        ctx:     ctx,
        user:    nil,
        query:   "",
        limiter: &drivequota,
    })

    drivelist := d.getdriveslist()
    for _, shareddrive := range drivelist.drives {
        files := d.getshareddrivefiles(shareddrive.id)
        for _, f := range files {
            j, _ := f.marshaljson()
            logrus.println(string(j))
        }
    }
}
package drive

imports ...

func new(options service) service {
    var c *http.client
    if options.user != nil {
        c = client.new(options.ctx, options.user.primaryemail, drive.drivescope)
    } else {
        apiuser := os.getenv("api_user")
        c = client.new(options.ctx, apiuser, drive.drivescope)
    }

    srv, err := drive.newservice(options.ctx, option.withhttpclient(c))
    errorchecker.check(err, "unable to create drive service")

    return service{srv, options.ctx, options.user, options.query, options.limiter}
}

func (d *service) getdriveslist() *drive.drivelist {
    srv := d.service

    drivelist, err := srv.drives.list().usedomainadminaccess(true).do()
    errorchecker.check(err, "unable to get drives list")

    return drivelist
}

func (d *service) getshareddrivefiles(id string) []*drive.file  {
    srv := d.service

    var shareddrivefiles []*drive.file
    listfiles := func(r *drive.filelist) error {
        shareddrivefiles = append(shareddrivefiles, r.files...)
        return nil
    }

    err := srv.files.list().corpora("drive").driveid(id).supportsalldrives(true).includeitemsfromalldrives(true).fields("files(id,ownedbyme,permissions,shared,sharinguser,trashed,properties,appproperties,name)").pagesize(1000).pages(d.ctx, listfiles)
    errorchecker.check(err, "unable to get shared drive files")

    return shareddrivefiles
}
package client

imports ...

func New(ctx context.Context, subject interface{}, scope ...string) *http.Client {
    cert := os.Getenv("CERT")
    b, err := ioutil.ReadFile(cert)
    errorchecker.Check(err, "Unable to read secret file")

    config, err := google.JWTConfigFromJSON(b, scope...)
    errorchecker.Check(err, "Unable to parse utils secret file to config")

    if subject != nil {
        config.Subject = subject.(string)
    }

    return config.Client(ctx)
}


正确答案


看来我的问题与 this answer 有关,而不是与我的身份验证方式有关。即使作为管理员,如果我不属于共享云端硬盘,我也无法访问该共享云端硬盘中的文件。

今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~

本文内容来源于互联网,如有侵权请联系删除。

相关文章

更多

近期热门

更多

站内热门

更多

热门专题

更多
首页