[英]golang gmbapi service businessprofileperformance on GetDailyMetricsTimeSeries returning Error 404: Requested entity was not found
我構建傳遞 CredentialsFile 和身份驗證范圍的服務,然后我使用正確的名稱 (locations/{location_id}) 調用 GetDailyMetricsTimeSeries,但返回錯誤 404。
ctx := context.Background()
performanceService, err := businessprofileperformance.NewService(ctx,
option.WithCredentialsFile("client_secret.json"),
option.WithScopes(Scope))
if err != nil {
log.Println(err.Error())
return
}
cm := performanceService.Locations.GetDailyMetricsTimeSeries("locations/12345...")
cm.DailyMetric("WEBSITE_CLICKS")
cm.DailyRangeStartDateYear(2022)
cm.DailyRangeStartDateMonth(6)
cm.DailyRangeStartDateDay(1)
cm.DailyRangeEndDateYear(2022)
cm.DailyRangeEndDateMonth(12)
cm.DailyRangeEndDateDay(30)
response, err := cm.Do()
if err != nil {
log.Println(err.Error())
return
}
if c := response.HTTPStatusCode; c >= 200 || c <= 299 {
j, _ := response.MarshalJSON()
log.Println(j)
}
我的 client_secret.json 文件是這樣的
{
"type": "",
"project_id": "",
"private_key_id": "",
"private_key": "",
"client_email": "",
"client_id": "",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": ""
}
我認為問題是缺少 location_id 引用的主題參數,但我沒有找到可以傳遞它的地方我隱藏了 json 文件的個人信息
您能否通過獲取位置調用驗證 GBP 位置仍然存在並且您可以訪問它?
獲取https://mybusinessbusinessinformation.googleapis.com/v1/{parent=accounts/*}/locations
404 可能表示 GBP 位置已被刪除。
問題出在身份驗證上,主題丟失了,所以我是這樣處理的:
func (a *AppCredential) GetCredentials(ctx context.Context, scope string) (*google.Credentials, error) {
jsonFile, err := os.Open("config/client_secret.json")
if err != nil {
log.Println("error oppening json")
return &google.Credentials{}, err
}
defer jsonFile.Close()
jsonData, _ := ioutil.ReadAll(jsonFile)
creds, err := google.CredentialsFromJSONWithParams(ctx, jsonData, google.CredentialsParams{Scopes: []string{scope}, Subject: "account@email.com"})
if err != nil {
return &google.Credentials{}, err
}
return creds, nil
}
然后
ctx := context.Background()
creds, err := appCreds.GetCredentials(ctx, "https://www.googleapis.com/auth/business.manage")
if err != nil {
log.Println(err.Error())
return
}
performanceService, err := businessprofileperformance.NewService(ctx, option.WithCredentials(creds))
if err != nil {
log.Println(err.Error())
return
}
cm := performanceService.Locations.GetDailyMetricsTimeSeries("locations/{location_id}")
response, err := cm.Do()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.