I wanted to edit the contents of the spreadsheet from the application on the GAE that I wrote in go, so I wrote the following code:
I get stuck in authentication of the spreadsheet authentication gets stuck.
Could you please let me know if anyone noticed anything?
As a precondition,
- I have created an IAM service account to learn the json private key and keep it local.
- I also shared client_email with the spreadsheet.
- I use 1.9 for go
Spreadsheet APIs and
https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/get?hl=ja
See AppEngine tutorial
https://cloud.google.com/appengine/docs/standard/go/building-app/creating-your-application
I wrote the following code.
In this case, we only want to print "success" to the browser as we wish we could access it.
package main
import(
"fmt"
"io/ioutil"
"log"
"net/http"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/api/sheets/v4"
"google.golang.org/appengine" // Required external App Engine library
)
US>func httpClient(credentialFilePath string) (*http.Client, error)
data,err: =ioutil.ReadFile(credentialFilePath)
if err!=nil{
return nil, err
}
conf,err:=google.JWTConfigFromJSON(data, "https://www.googleapis.com/auth/spreadsheets")
if err!=nil{
return nil, err
}
return conf.Client(oauth2.NoContext), nil
}
funcmain(){
spreadsheetId: = "ID of spreadsheet"
credentialFilePath:="credential.json"
client,err: = httpClient(credentialFilePath)
if err!=nil{
log.Fatal(err)
}
sheetService, err: = sheets.New(client)
if err!=nil{
log.Fatalf("Unable to retrieve Sheets Client %v", err)
}
_,err=sheetService.Spreadsheets.Get(spreadsheetId).Do()
if err!=nil{
log.Fatalf("Unable to get Spreadsheets.%v", err)
}
code_str: = "success" // characters to be displayed in the browser
http.HandleFunc("/", func(whttp.ResponseWriter,r*http.Request){
indexHandler(w,r,code_str)
})
appengine.Main() // Starts the server to receive requests
}
func indexHandler(whttp.ResponseWriter,r*http.Request,code string){
// if statement redirects all invalid URLs to the root homepage.
// Ex: if URL is http:// [YOUR_PROJECT_ID].appspot.com/FOO, it will be
// redirected to http://[YOUR_PROJECT_ID].appspot.com.
ifr.URL.Path!="/"{
http.Redirect(w,r, "/", http.StatusFound)
return
}
// fmt.Fprintln(w, "Hello, Gopher Network!")
fmt.Fprintln(w,code)
}
"When I ran ""run sample.go"" I was able to see ""success"" from my browser."
This means that when you run it locally, you can access the spreadsheet using a private key.
However, if you run dev_appserver.py app.yaml using an emulator,
oauth2:cannot fetch token:Post https://oauth2.googleapis.com/token:not an App Engine context appears as follows:
ERROR 2018-08-20 15:23:41,150 http_runtime.py:414] bad runtime process port [']
INFO 2018-08-20 15:23:41,150 instance.py:294] Instance PID: 16732
August 20, 2018 15:23:41 Unable to get Spreadsheets.Get https://sheets.googleapis.com/v4/spreadsheets/ Spreadsheet ID?alt=json:oauth2:cannot fetch token:Post https://oauth2.googleapis.com/token:not an App Engine context
If you notice anything, could someone please let me know?
Thank you for your cooperation.
[Resolved below]
https://cloud.google.com/appengine/docs/standard/go/building-app/creating-your-application
This is based on the code of the official tutorial, but
Now you can retrieve the spreadsheet data.
@belbo Thank you for letting me know.
Thank you very much
package main
import(
"fmt"
"io/ioutil"
"log"
"net/http"
"golang.org/x/oauth2/google"
"google.golang.org/api/sheets/v4"
"google.golang.org/appengine" // Required external App Engine library
)
funcmain(){
http.HandleFunc("/", indexHandler)
appengine.Main() // Starts the server to receive requests
}
func indexHandler(whttp.ResponseWriter,r*http.Request){
ifr.URL.Path!="/"{
http.Redirect(w,r, "/", http.StatusFound)
return
}
// the following additional parts
credentialFilePath:="Certificate json"
data,err: =ioutil.ReadFile(credentialFilePath)
if err!=nil{
return
}
conf,err:=google.JWTConfigFromJSON(data, "https://www.googleapis.com/auth/spreadsheets")
if err!=nil{
return
}
ctx: =appengine.NewContext(r)
client —=conf.Client(ctx)
sheetService, err: = sheets.New(client)
if err!=nil{
log.Fatalf("Unable to retrieve Sheets Client %v", err)
}
spreadsheetId: = "ID of spreadsheet"
readRange: = "Worksheet Name"
resp,err: =sheetService.Spreadsheets.Values.Get(spreadsheetId,readRange).Do()
if err!=nil{
log.Fatalf("Unable to get Spreadsheets.%v", err)
}
// fmt.Fprintln(w, "Hello, Gopher Network!")
fmt.Fprintln(w,resp.Values) // Display spreadsheet data in browser
}
→dev_appserver.py app.yaml result browser.
I also read Spreadsheets on GAE/Go and experimented with spitting on the web.
(https://lists.kogus.org/).
httpClient() returns conf.Client(oauth2.NoContext), but
You should return context.Context as conf.Client(ctx).
© 2024 OneMinuteCode. All rights reserved.