dreamwind1985 2016-01-09 15:23
浏览 131

错误400 OAuth2 Google云存储-Go Lang

// 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()
}

When I run go file, It return error follow:

Pass authentication (Pass getCtx() function).

Error in ExampleGet() May be at

ctx := context.Background()
client, err := datastore.NewClient(ctx, "xxxx")
if err != nil {
    log.Fatal(err)
  }

Error: 2016/01/09 22:08:43 Post https://www.googleapis.com/datastore/v1beta2/datasets/xxxx/lookup: oauth2: cannot fetch token: 400 Bad Request Response: { "error" : "invalid_grant" }

How to resolve this error?

  • 写回答

1条回答 默认 最新

  • doufu6130 2016-01-17 18:24
    关注

    This appears to have worked for me.

    If you are on linux try the following:
    1. apt-get update
    2. apt-get install ntp
    3. /etc/init.d/ntp restart

    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看