unsupportedOutputFormat go drive api sdk v3无法下载gdrive上的文件内容

时间:2016-05-28 06:07:57

标签: go google-drive-api

文档不清楚如何正确设置输出格式。 我在google驱动器上为单个文件硬编码了一条路线: http://localhost:8080/0B-zdryEj60U_OXZBVklsdG13Wlk 它的响应如下: 运行时进程给出了错误的HTTP响应:

2016/05/28 06:00:26 handleOneDoc
2016/05/28 06:00:26 
2016/05/28 06:00:26 &{0xc82007a500 0xc8201121c0 0x84de40 false false 0xc82016a180 {0xc82016c820 map[] false false} map[] false 0 -1 0 false false [] 0 [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0] <nil>}
2016/05/28 06:00:29 Unable to get download link %vgoogleapi: got HTTP response code 400 with body: {"error":{"errors":[{"domain":"global","reason":"unsupportedOutputFormat","message":"Unsupported Output Format","locationType":"parameter","location":"alt"}],"code":400,"message":"Unsupported Output Format"}}

以下是来源:

package gwebdrive

import (
    "fmt"
    "golang.org/x/net/context"
    _ "golang.org/x/oauth2"
    "golang.org/x/oauth2/google"
    "golang.org/x/oauth2/jwt"
    "google.golang.org/api/drive/v3"
    "google.golang.org/appengine"
    _ "html/template"
    _ "io/ioutil"
    "log"
    "net/http"
    "net/url"

    "strings"
)

const (
    assetFolder = "0B-zdryEj60U_N0NtSFJuQUdaWTA"
)

var (
    //tpl  *template
    dir  *drive.FileList
    conf *jwt.Config
)

func init() {
    ... key config content here. 
    http.Handle("/list", appengineHandler(handleList))
    http.Handle("/0B-zdryEj60U_OXZBVklsdG13Wlk", appengineHandler(handleOneDoc))

    //http.Handle("/.+", appengineHandler(handleContent))
}

// appengineHandler wraps http.Handler to pass it a new `appengine.Context` and handle errors.
type appengineHandler func(c context.Context, w http.ResponseWriter, r *http.Request) error

func (h appengineHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {

    c := appengine.NewContext(r)
    if err := h(c, w, r); err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
    }
}

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "Hello Darian Go Serve some Assets")

}

func handleOneDoc(c context.Context, w http.ResponseWriter, r *http.Request) error {
    log.Print("handleOneDoc")
    u, err := url.Parse(r.RequestURI)
    if err != nil {
        log.Fatalf("Unable to parse Request URL %v", err)
    }
    log.Printf(u.RawPath)
    s := strings.Split(u.RawPath, "/")
    // looking for regex for Google File ID's.

    // I don't actually know the point of this call but it's needed.
    client := conf.Client(appengine.NewContext(r))
    // use url to decide what asset to return as data.
    // for now assume url contains id.
    srv, err := drive.New(client)
    fileid := s[len(s)-1]
    log.Print(w, fileid)
    if err != nil {
        log.Fatalf("Unable to retrieve drive Client %v", err)
    }

    dwn, err := srv.Files.Get(fileid).Download()
    fmt.Fprint(w, "mimeType:image/png")

    if err != nil {
        log.Fatal("Unable to get download link %v", err)
    }

    fmt.Fprint(w, dwn.Body)

    return nil

}

func handleContent(c context.Context, w http.ResponseWriter, r *http.Request) error {
    log.Print("handleContent")
    u, err := url.Parse(r.RequestURI)
    if err != nil {
        log.Fatalf("Unable to parse Request URL %v", err)
    }
    fmt.Fprintf(w, u.RawPath)
    s := strings.Split(u.RawPath, "/")
    // looking for regex for Google File ID's.

    // I don't actually know the point of this call but it's needed.
    client := conf.Client(appengine.NewContext(r))
    // use url to decide what asset to return as data.
    // for now assume url contains id.
    srv, err := drive.New(client)
    fileid := s[len(s)-1]
    if err != nil {
        log.Fatalf("Unable to retrieve drive Client %v", err)
    }

    dwn, err := srv.Files.Get(fileid).Download()
    //service.files().get(fileId).executeMediaAndDownloadTo(w)
    fmt.Fprint(w, dwn)
    if err != nil {
        log.Fatal("Unable to get download link %v", err)
    }

    //fmt.Fprint(w, dwn)

    return nil

}

func handleList(c context.Context, w http.ResponseWriter, r *http.Request) error {
    log.Print("just handle")

    // Initiate an http.Client, the following GET request will be
    // authorized and authenticated on the behalf of user@example.com.
    client := conf.Client(appengine.NewContext(r))

    srv, err := drive.New(client)
    if err != nil {
        log.Fatalf("Unable to retrieve drive Client %v", err)
    }

    dir, err = srv.Files.List().PageSize(10).Q("'0B-zdryEj60U_MXVkajFweXBQWHM' in parents").
        Fields("files(id, kind, name, size, webViewLink)").Do()
    //resp, err := srv.Files.Export(dir.Files[1].Id, dir.Files[1].MimeType).Download()
    //fmt.Fprint(w, resp.Body)

    if err != nil {
        log.Fatalf("Unable to retrieve files.", err)
    }

    fmt.Fprint(w, "Files:")
    if len(dir.Files) > 0 {
        for _, i := range dir.Files {
            fmt.Fprint(w, "%s (%s)\n", i.Name, i.WebViewLink)
        }
    } else {
        fmt.Fprint(w, "No files found.")
    }
    return err
}

1 个答案:

答案 0 :(得分:0)

this解决方案对我有用。找到的更改是直接提供文件URL。这会导致inSufficientPermission出现另一个错误。通过this

解决了这个问题