无法使用远程api连接到谷歌云数据存储区

时间:2014-12-12 21:16:56

标签: go google-cloud-datastore

我使用以下go代码(大部分来自go_appengine/demos/remote_api/datastore_info.go):

package main

import (
    "net/http"
    "net/http/cookiejar"
    "net/url"
    "regexp"
    "io/ioutil"
    "log"
    "errors"
    //"appengine"
    "appengine/remote_api"
    "appengine/datastore"
    "fmt"
)

type CustomType struct {
    FirstName string
    LastName string
}

func clientLoginClient(host, email, password string) *http.Client {
    jar, err := cookiejar.New(nil)
    if err != nil {
        log.Fatalf("failed to make cookie jar: %v", err)
    }
    client := &http.Client{
        Jar: jar,
    }

    v := url.Values{}
    v.Set("Email", email)
    v.Set("Passwd", password)
    v.Set("service", "ah")
    v.Set("source", "Misc-remote_api-0.1")
    v.Set("accountType", "HOSTED_OR_GOOGLE")

    resp, err := client.PostForm("https://www.google.com/accounts/ClientLogin", v)
    if err != nil {
        log.Fatalf("could not post login: %v", err)
    }
    defer resp.Body.Close()

    body, err := ioutil.ReadAll(resp.Body)
    if resp.StatusCode != http.StatusOK {
        log.Fatalf("unsuccessful request: status %d; body %q", resp.StatusCode, body)
    }
    if err != nil {
        log.Fatalf("unable to read response: %v", err)
    }

    m := regexp.MustCompile(`Auth=(\S+)`).FindSubmatch(body)
    if m == nil {
        log.Fatalf("no auth code in response %q", body)
    }
    auth := string(m[1])

    u := &url.URL{
        Scheme:   "https",
        Host:     host,
        Path:     "/_ah/login",
        RawQuery: "continue=/&auth=" + url.QueryEscape(auth),
    }

    // Disallow redirects.
    redirectErr := errors.New("stopping redirect")
    client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
        return redirectErr
    }

    resp, err = client.Get(u.String())
    if urlErr, ok := err.(*url.Error); !ok || urlErr.Err != redirectErr {
        log.Fatalf("could not get auth cookies: %v", err)
    }
    defer resp.Body.Close()

    body, err = ioutil.ReadAll(resp.Body)
    if resp.StatusCode != http.StatusFound {
        log.Fatalf("unsuccessful request: status %d; body %q", resp.StatusCode, body)
    }

    client.CheckRedirect = nil
    return client
}

func Test() {
        host := "projectnumber-compute@developer.gserviceaccount.com" //or alternatively "projectname@appspot.gserviceaccount.com"
        email := "myemail@gmail.com"
        password := "mypassword"

        client := clientLoginClient(host, email, password)

        c, err := remote_api.NewRemoteContext(host, client)
        if err != nil {
                fmt.Println("Error: ", err)
                return
        }

        e1 := CustomType{
            FirstName:     "Joe Citizen",
            LastName:     "Manager",
        }

        key, err := datastore.Put(c, datastore.NewIncompleteKey(c, "CustomType", nil), &e1)
        if err != nil {
            fmt.Println("Can not Put in datastore")
        }

        var e2 CustomType
        if err = datastore.Get(c, key, &e2); err != nil {
            fmt.Println("Can not Get from datastore")
        }

        fmt.Println("Stored and retrieved the CustomType: %q %q", e2.FirstName, e2.LastName)
}

func main () {
    Test()
}

无论我使用的给定URL中的哪个主机,编译运行后都会出现以下错误:

  

无法获取身份验证Cookie:获取https:// 项目编号 -compute@developer.gserviceaccount.com/_ah/login?continue = /& auth = someencodedtexthere :dial tcp:GetAddrInfoW:没有这样的主机。

使用go app连接到云数据存储的任何帮助吗?

1 个答案:

答案 0 :(得分:1)

为什么使用服务帐户电子邮件地址作为主机?主机类似于" localhost",IP地址或FQDN。再次检查SDK演示,并注意它没有使用服务帐户电子邮件地址作为主机。如果您阅读了错误消息,则可以看到GetAddrInfoW: No such host is known.,这可能会提醒您。