在命令行构建,但无法构建为gae app

时间:2016-05-25 00:01:07

标签: google-app-engine go

在命令行中没有问题:

  

Darians-MacBook-Pro:gdriveweb darianhickman $ go build helloworld / hello.go

     

Darians-MacBook-Pro:gdriveweb darianhickman $

locahost错误:8080 /

  

无法构建Go应用程序。

     

(执行命令:/ Users / darianhickman / go_appengine / goroot / bin / go-app-builder -app_base /Users/darianhickman/gowork/src/bitbucket.org/darian_hickman/gdriveweb/helloworld -arch 6 -dynamic -goroot / Users / darianhickman / go_appengine / goroot -nobuild_files ^^ $ -unsafe -gopath / Users / darianhickman / gowork -binary_name _go_app -extra_imports appengine_internal / init -work_dir / var / folders / fk / wknp5jzn53gbgbml0yn695_m0000gn / T / tmpsHFP6tappengine-go-bin - gcflags -I,/ Users / darianhickman / go_appengine / goroot / pkg / darwin_amd64_appengine -ldflags -L,/ Users / darianhickman / go_appengine / goroot / pkg / darwin_amd64_appengine hello.go)   /Users/darianhickman/gowork/src/golang.org/x/net/context/ctxhttp/ctxhttp.go:35:req.Cancel undefined(type * http.Request没有字段或方法取消)

     

2016/05/24 19:39:17 go-app-builder:构建时间:6×6g(总共469ms),0×6l(总共0)   2016/05/24 19:39:17 go-app-builder:运行失败6g:退出状态1

当我研究错误时

  

* http.Request没有字段或方法取消

它导致了一些关于更新到> Go1.5的不适用的帖子。

来源:

 package hello

import (
    "encoding/json"
    "fmt"
    "golang.org/x/net/context"
    "golang.org/x/oauth2"
    "golang.org/x/oauth2/google"
    "google.golang.org/api/drive/v3"
    _ "google.golang.org/appengine/urlfetch"
    "io/ioutil"
    "log"
    "net/http"
    "net/url"
    "os"
    "os/user"
    "path/filepath"
)

const (
    assetfolder = "0B-zdryEj60U_MXVkajFweXBQWHM"
)

var (
    dir *drive.FileList
)

func init() {
    http.HandleFunc("/", handler)

    ctx := context.Background()

    b, err := ioutil.ReadFile("client_secret.json")
    if err != nil {
        log.Fatalf("Unable to read client secret file: %v", err)
    }

    // If modifying these scopes, delete your previously saved credentials
    // at ~/.credentials/drive-go-quickstart.json
    config, err := google.ConfigFromJSON(b, drive.DriveMetadataReadonlyScope)
    if err != nil {
        log.Fatalf("Unable to parse client secret file to config: %v", err)
    }
    client := getClient(ctx, config)

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

    dir, err = srv.Files.List().PageSize(10).
        Fields("nextPageToken, files(id, name)").Do()
    if err != nil {
        log.Fatalf("Unable to retrieve files.", err)
    }

}

func handler(w http.ResponseWriter, r *http.Request) {

    //fmt.Fprint(w, r.RequestURI)

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

// getClient uses a Context and Config to retrieve a Token
// then generate a Client. It returns the generated Client.
func getClient(ctx context.Context, config *oauth2.Config) *http.Client {
    cacheFile, err := tokenCacheFile()
    if err != nil {
        log.Fatalf("Unable to get path to cached credential file. %v", err)
    }
    tok, err := tokenFromFile(cacheFile)
    if err != nil {
        tok = getTokenFromWeb(config)
        saveToken(cacheFile, tok)
    }
    return config.Client(ctx, tok)
}

// getTokenFromWeb uses Config to request a Token.
// It returns the retrieved Token.
func getTokenFromWeb(config *oauth2.Config) *oauth2.Token {
    authURL := config.AuthCodeURL("state-token", oauth2.AccessTypeOffline)
    fmt.Printf("Go to the following link in your browser then type the "+
        "authorization code: \n%v\n", authURL)

    var code string
    if _, err := fmt.Scan(&code); err != nil {
        log.Fatalf("Unable to read authorization code %v", err)
    }

    tok, err := config.Exchange(oauth2.NoContext, code)
    if err != nil {
        log.Fatalf("Unable to retrieve token from web %v", err)
    }
    return tok
}

// tokenCacheFile generates credential file path/filename.
// It returns the generated credential path/filename.
func tokenCacheFile() (string, error) {
    usr, err := user.Current()
    if err != nil {
        return "", err
    }
    tokenCacheDir := filepath.Join(usr.HomeDir, ".credentials")
    os.MkdirAll(tokenCacheDir, 0700)
    return filepath.Join(tokenCacheDir,
        url.QueryEscape("drive-go-quickstart.json")), err
}

// tokenFromFile retrieves a Token from a given file path.
// It returns the retrieved Token and any read error encountered.
func tokenFromFile(file string) (*oauth2.Token, error) {
    f, err := os.Open(file)
    if err != nil {
        return nil, err
    }
    t := &oauth2.Token{}
    err = json.NewDecoder(f).Decode(t)
    defer f.Close()
    return t, err
}

// saveToken uses a file path to create a file and store the
// token in it.
func saveToken(file string, token *oauth2.Token) {
    fmt.Printf("Saving credential file to: %s\n", file)
    f, err := os.Create(file)
    if err != nil {
        log.Fatalf("Unable to cache oauth token: %v", err)
    }
    defer f.Close()
    json.NewEncoder(f).Encode(token)
}

func main() {
    ctx := context.Background()

    b, err := ioutil.ReadFile("client_secret.json")
    if err != nil {
        log.Fatalf("Unable to read client secret file: %v", err)
    }

    // If modifying these scopes, delete your previously saved credentials
    // at ~/.credentials/drive-go-quickstart.json
    config, err := google.ConfigFromJSON(b, drive.DriveMetadataReadonlyScope)
    if err != nil {
        log.Fatalf("Unable to parse client secret file to config: %v", err)
    }
    client := getClient(ctx, config)

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

    r, err := srv.Files.List().PageSize(10).
        Fields("nextPageToken, files(id, name)").Do()
    if err != nil {
        log.Fatalf("Unable to retrieve files.", err)
    }

    fmt.Println("Files:")
    if len(r.Files) > 0 {
        for _, i := range r.Files {
            fmt.Printf("%s (%s)\n", i.Name, i.Id)
        }
    } else {
        fmt.Print("No files found.")
    }

}

1 个答案:

答案 0 :(得分:0)

我通过重新下载并重新安装Go App Engine SDK来解决这个问题。我最好的猜测为什么这样做是因为旧版本的go以某种方式被包含在内。