使用oauth进行设备状态更新时的Golang和post请求语法



我正在尝试更改chromeos设备的状态。

我"get"请求与从序列号获取设备ID一起工作。

然而,我无法找出golang google sdk/api中的how to pass the payload,因为它是一个"post"。请求。

本例中的有效负载是Action。(deprovision, disable, reenable, pre_provisioned_disable, pre_provisioned_reenable)和deprovisionReason如果动作是deprovision

https://developers.google.com/admin-sdk/directory/reference/rest/v1/chromeosdevices/action ChromeOsDeviceAction

package main
import (
"context"
"encoding/csv"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
admin "google.golang.org/api/admin/directory/v1"
"google.golang.org/api/option"
)
func readCsvFile(filePath string) [][]string {
f, err := os.Open(filePath)
if err != nil {
log.Fatal("Unable to read input file "+filePath, err)
}
defer f.Close()
csvReader := csv.NewReader(f)
records, err := csvReader.ReadAll()
if err != nil {
log.Fatal("Unable to parse file as CSV for "+filePath, err)
}
return records
}
// Retrieve a token, saves the token, then returns the generated client.
func getClient(config *oauth2.Config) *http.Client {
// The file token.json stores the user's access and refresh tokens, and is
// created automatically when the authorization flow completes for the first
// time.
tokFile := "token.json"
tok, err := tokenFromFile(tokFile)
if err != nil {
tok = getTokenFromWeb(config)
saveToken(tokFile, tok)
}
return config.Client(context.Background(), tok)
}
// Request a token from the web, then returns the retrieved token.
func getTokenFromWeb(config *oauth2.Config) *oauth2.Token {
authURL := config.AuthCodeURL("state-token", oauth2.AccessTypeOffline)
fmt.Printf("Go to the following link in your browser then type the "+
"authorization code: n%vn", authURL)
var authCode string
if _, err := fmt.Scan(&authCode); err != nil {
log.Fatalf("Unable to read authorization code: %v", err)
}
tok, err := config.Exchange(context.TODO(), authCode)
if err != nil {
log.Fatalf("Unable to retrieve token from web: %v", err)
}
return tok
}
// Retrieves a token from a local file.
func tokenFromFile(file string) (*oauth2.Token, error) {
f, err := os.Open(file)
if err != nil {
return nil, err
}
defer f.Close()
tok := &oauth2.Token{}
err = json.NewDecoder(f).Decode(tok)
return tok, err
}
// Saves a token to a file path.
func saveToken(path string, token *oauth2.Token) {
fmt.Printf("Saving credential file to: %sn", path)
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
log.Fatalf("Unable to cache oauth token: %v", err)
}
defer f.Close()
json.NewEncoder(f).Encode(token)
}
func findDeviceId(srv *admin.Service, deviceSerial string) (deviceId string) {
deviceId = ""
r, err := srv.Chromeosdevices.List("aaa").Query(deviceSerial).Do()
if err != nil {
log.Printf("Unable to retrieve device with serial %s in domain: %v", deviceSerial, err)
} else {
for _, u := range r.Chromeosdevices {
deviceId = u.DeviceId
fmt.Printf("%s %s (%s)n", u.DeviceId, u.SerialNumber, u.Status)
}
}
return deviceId
}
func main() {
ctx := context.Background()
b, err := os.ReadFile("credentials.json")
if err != nil {
log.Fatalf("Unable to read client secret file: %v", err)
}
config, err := google.ConfigFromJSON(b, admin.AdminDirectoryDeviceChromeosScope)
if err != nil {
log.Fatalf("Unable to parse client secret file to config: %v", err)
}
client := getClient(config)
srv, err := admin.NewService(ctx, option.WithHTTPClient(client))
if err != nil {
log.Fatalf("Unable to retrieve directory Client %v", err)
}
deviceId := findDeviceId(srv, "xxx")
deviceAction := make(map[string]string)
deviceAction["action"] = "disable"
deviceAction["deprovisionReason"] = "retiring_device"
r, err := srv.Chromeosdevices.Action("aaa", deviceId, &deviceAction).Do()
fmt.Println(r)
fmt.Println(err)
}

得到误差

cannot use deviceAction (variable of type map[string]string) as *admin.ChromeOsDeviceAction value in argument to srv.Chromeosdevices.ActioncompilerIncompatibleAssign

方法ChromeosdevicesService.ActionChromeOsDeviceAction:

chromeosdeviceaction := &admin.ChromeOsDeviceAction{
Action: "disable",
DeprovisionReason: "retiring_device",
}
使用应用程序默认凭据,您的代码将更简单,更易于移植。该方法在库的文档中有展示和引用:

最新更新