我正在嘗試更改 chromeos 設備的狀態。
我的“獲取”請求用于從序列號獲取設備 ID。
但是,我無法how to pass the payload在 golang google sdk/api 中弄清楚,因為它是一個“發布”請求。
在這種情況下,有效負載是 Action。(取消配置、禁用、重新啟用、pre_provisioned_disable、pre_provisioned_reenable)和 deprovisionReason(如果操作是取消配置)。
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%v\n", 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: %s\n", 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
uj5u.com熱心網友回復:
該方法ChromeosdevicesService.Action需要ChromeOsDeviceAction:
chromeosdeviceaction := &admin.ChromeOsDeviceAction{
Action: "disable",
DeprovisionReason: "retiring_device",
}
使用應用程式默認憑據,您的代碼會更簡單、更便攜。庫的檔案中顯示并參考了這種方法:Creating a client
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/535736.html
標籤:去谷歌APIgoogle-oauthgoogle-api-go-客户端
