dongxiz5342 2016-05-28 06:07 采纳率: 100%
浏览 83

UnsupportedOutputFormat Go驱动器api sdk v3无法在gdrive上下载文件内容

The docs aren't clear about how to properly set output format. I hardcoded a route for a single file on google drive: http://localhost:8080/0B-zdryEj60U_OXZBVklsdG13Wlk and it responds below: the runtime process gave a bad HTTP response:

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"}}

Below is the source:

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)
", i.Name, i.WebViewLink)
        }
    } else {
        fmt.Fprint(w, "No files found.")
    }
    return err
}
  • 写回答

1条回答 默认 最新

  • douyi3833 2017-12-12 14:25
    关注

    this solution worked for me. the change found is that to give the file URL directly. which lead to another error on inSufficientPermission. Solved that too via this

    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图