2015-07-03 103 views
1

我嘗試使用服務帳戶連接到驅動器。服務帳戶,應用引擎,Go,Google API

其實我有

 c := appengine.NewContext(r) 
     key, err := ioutil.ReadFile("key/key.pem") 
     if err != nil { 
      w.WriteHeader(http.StatusInternalServerError) 
      c.Errorf("Pem file not found") 
      return 
     } 
     config := &jwt.Config{ 
      Email: "[email protected]", 
      PrivateKey: key, 
      Scopes: []string{ 
       "https://www.googleapis.com/auth/drive", 
      }, 
      TokenURL: google.JWTTokenURL, 
     } 

     client := config.Client(oauth2.NoContext) 
     service, err := drive.New(client) 
     if (err != nil) { 
      w.WriteHeader(http.StatusInternalServerError) 
      c.Errorf("Service connection not works") 
      return 
     } 
     about, err := service.About.Get().Do() 
     if (err != nil) { 
      w.WriteHeader(http.StatusInternalServerError) 
      c.Errorf(err.Error()) 
      return 
     } 
     c.Infof(about.Name) 

,我發現在這裏:https://github.com/golang/oauth2/blob/master/google/example_test.go

當然它不工作,我必須使用網址抓取,但我不知道怎麼... 的我得到的錯誤是"ERROR: Get https://www.googleapis.com/drive/v2/about?alt=json: oauth2: cannot fetch token: Post https://accounts.google.com/o/oauth2/token: not an App Engine context"

我該怎麼辦?

謝謝。

回答

0

Google App Engine有兩種Go軟件包:appenginegoogle.golang.org/appengine

第一個使用的appengine.Context與oauth2軟件包使用的context.Context不兼容。您需要將第二個導入google.golang.org/appengine

此外,將client := config.Client(oauth2.NoContext)更改爲client := config.Client(c)