英文:
golang gmbapi service businessprofileperformance on GetDailyMetricsTimeSeries returning Error 404: Requested entity was not found
问题
我构建了一个传递CredentialsFile和Scope进行身份验证的服务,然后我使用正确的名称(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引用缺少subject参数,但我找不到可以传递它的地方。我已经隐藏了JSON文件中的个人信息。
英文:
I construct the service passing a CredentialsFile and the Scope for auth, then I call the GetDailyMetricsTimeSeries with the right name (locations/{location_id}) but returns error 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)
}
my client_secret.json file is like this
>
{
"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": ""
}
I think the problem is missing the subject param for the location_id reference, but I didn't found where I can pass it
I've hide the personal information of json file
答案1
得分: 0
请通过“get locations”调用验证一下 GBP 位置是否仍然存在,并且你是否有访问权限?
GET https://mybusinessbusinessinformation.googleapis.com/v1/{parent=accounts/*}/locations
404 可能表示 GBP 位置已被移除。
英文:
Can you please verify through a get locations call that the GBP location still exists and that you have access to it?
GET https://mybusinessbusinessinformation.googleapis.com/v1/{parent=accounts/*}/locations
404 could indicate that the GBP location was removed.
答案2
得分: 0
问题出在身份验证上,缺少了主题,所以我这样处理:
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()
英文:
the problem was in the authentication, the subject was missing, so I managed it this way:
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
}
then
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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论