首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误400 oauth2谷歌云存储- go lang

错误400 oauth2谷歌云存储- go lang
EN

Stack Overflow用户
提问于 2016-01-09 23:23:15
回答 1查看 331关注 0票数 1
代码语言:javascript
复制
// datastore1
package main

import (
  "fmt"
  "io/ioutil"
  "log"
  "time"

"golang.org/x/net/context"
"golang.org/x/oauth2/google"
"google.golang.org/cloud"
"google.golang.org/cloud/datastore"
)

const (
// ScopeDatastore grants permissions to view and/or manage datastore    entities
copeDatastore = "https://www.googleapis.com/auth/datastore"

// ScopeUserEmail grants permission to view the user's email address.
// It is required to access the datastore.
ScopeUserEmail = "https://www.googleapis.com/auth/userinfo.email"
)

type ehrEntity struct {
email       *datastore.Key
firstname   string
lastname    string
address     string
age         int8
dateofbirth time.Time
sex         bool
}

 func getCtx() *datastore.Client {
// 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
jsonKey, err := ioutil.ReadFile("filename.json")
opts, err := google.JWTConfigFromJSON(
    jsonKey,
    datastore.ScopeDatastore,
    datastore.ScopeUserEmail,
)
if err != nil {
    log.Fatal(err)
}

ctx := context.Background()
client, err := datastore.NewClient(ctx, "xxxx",     cloud.WithTokenSource(opts.TokenSource(ctx)))
if err != nil {
    log.Fatal(err)
}
// Use the context (see other examples)
return client
}

 func ExampleGet() {
ctx := context.Background()
client, err := datastore.NewClient(ctx, "xxxx")
if err != nil {
    log.Fatal(err)
}
key := datastore.NewKey(ctx, "User", "tluu@abc.com", 0, nil)
ehr := ehrEntity{
    nil,
    "tri",
    "luu",
    "addr1",
    20,
    time.Date(2009, time.January, 10, 23, 0, 0, 0, time.UTC),
    false}
if err := client.Get(ctx, key, ehr); err != nil {
    log.Fatal(err)
 }
 }

func main() {
getCtx()
fmt.Println("Pass authentication")
ExampleGet()
}

当我运行go file时,它返回以下错误:

通过身份验证(通过getCtx()函数)。

ExampleGet()中的错误可能位于

代码语言:javascript
复制
ctx := context.Background()
client, err := datastore.NewClient(ctx, "xxxx")
if err != nil {
    log.Fatal(err)
  }

错误: 2016/01/09 22:08:43发布错误: oauth2:无法获取令牌: 400错误请求响应:{“https://www.googleapis.com/datastore/v1beta2/datasets/xxxx/lookup”:"invalid_grant“}

如何解决此错误?

EN

回答 1

Stack Overflow用户

发布于 2016-01-18 02:24:23

这似乎对我很有效。

如果您使用的是linux,请尝试执行以下操作:

  1. apt-get

apt-get install ntp

  1. /etc/init.d/ntp restart
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34695285

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档