I use the following go
code (mostly borrowed from 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()
}
No matter which host from the given URLs I use, I get the following error after I compile and run:
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.
Any help connecting to cloud datastore using a go app?
Why are you using the service account email address as the host? A host is something like "localhost", an IP address, or an FQDN. Check the SDK demo again and notice that it doesn't use a service account email address as the host. If you read the error message, you could see GetAddrInfoW: No such host is known.
, which might alert you to this.