golang gmbapi服务业务配置文件GetDailyMetricsTimeSeries上的性能返回错误404:未找到请求的实体

busg9geu  于 2022-12-20  发布在  Go
关注(0)|答案(2)|浏览(127)

我构造了一个传递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引用缺少主题参数,但我没有找到可以传递它的位置,我隐藏了json文件的个人信息

wn9m85ua

wn9m85ua1#

您能否通过获取位置调用来验证GBP位置是否仍然存在,以及您是否可以访问它?
获取https://mybusinessbusinessinformation.googleapis.com/v1/{parent=accounts/*}/locations
404可以指示GBP位置被移除。

iqjalb3h

iqjalb3h2#

问题出在认证上,主题丢失了,所以我这样处理:

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()

相关问题