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

标签 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 中的哪个主机,我在编译和运行后都会收到以下错误:

could not get auth cookies: Get https://projectnumber-compute@developer.gserviceaccount.com/_ah/login?continue=/&auth=someencodedtexthere: dial tcp: GetAddrInfoW: No such host is known.

对使用 go 应用程序连接到云数据存储有任何帮助吗?

最佳答案

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

关于go - 无法使用远程 api 从 go 连接到谷歌云数据存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27452241/

相关文章:

security - 在 App Engine 上加密用户数据

java - 使用 Google Endpoint 保存 blob

用于构造问题的 Json 动态字段

go - 如何将键数组传递给 go-redis 包中的 MGET func?

go - 将变量传递给回调函数

golang 编译时(静态代码分析)检测格式化字符串和参数之间的不匹配

concurrency - (websocket) Golang 同步数据锁定失败 - Broken Pipe

java - 如果我知道 ID,如何通过其唯一 ID 获取数据存储实体?

python - 以编程方式将数据存储 key 字符串转换为新的应用程序 ID? (主/从-> HRD迁移)

google-cloud-datastore - 谷歌云数据存储运行查询返回 412 "no matching index found"