Connecting to Google Cloud datastore with go

I am trying to connect to a cloud storage from Go. I used the sample code given here - https://github.com/GoogleCloudPlatform/gcloud-golang .

These are the relevant bits of my code:

func getCtx() context.Context {
    // Initialize an authorized transport with Google Developers Console
    // JSON key. Read the google package examples to learn more about
    // different authorization flows you can use.
    // http://godoc.org/golang.org/x/oauth2/google
    opts, err := oauth2.New(
        google.ServiceAccountJSONKey("CassandraTest-key.json"),
        oauth2.Scope(datastore.ScopeDatastore),
    )
    if err != nil {
        log.Fatal(err)
    }

    //titanium-goods-766 is the project id for CassandraTest (under sthilakan@eyeota.com)

    ctx := cloud.NewContext("titanium-goods-766", &http.Client{Transport: opts.NewTransport()})

    // Use the context (see other examples)
    return ctx
}

type contactInfoEntity struct {
    EmailKey  *datastore.Key
    FirstName string
    LastName  string
}

func main() {
    ctx := getCtx()
    fmt.Println("successfully got context", ctx)

    err := putEntity(ctx, "fname1", "lname1", "email1")

    if err != nil {
        fmt.Println("Error:", err)
    } else {
        fmt.Println("success")
    }
}

func putEntity(ctx context.Context, firstName string, lastName string, email string) error {
    key := datastore.NewKey(ctx, "contactInfoEntity", email, 0, nil)

    contactInfoEntity := contactInfoEntity{
        EmailKey:  key,
        FirstName: firstName,
        LastName:  lastName,
    }

    _, err := datastore.Put(ctx, key, &contactInfoEntity)

    return err
}

      

I am getting this error consistently.

Error: error during call, http status code: 403 Unauthorized.

      

I have disconnected and reconnected the datastore api multiple times (as suggested here: All requests return 403 Unauthorized ). I also tried to remove and add the service account.

(I tried to connect my compute engine instance to the datastore using the steps here - https://cloud.google.com/datastore/docs and it works fine).

Has anyone connected to cloud storage?

Regards, Sathya

+3


source to share


1 answer


Cloud storage access requires two areas: datastore.ScopeDatastore

and datastore.ScopeUserEmail

:



opts, err := oauth2.New(
    google.ServiceAccountJSONKey("CassandraTest-key.json"),
    oauth2.Scope(datastore.ScopeDatastore, datastore.ScopeUserEmail),
)

      

+3


source







All Articles