golang gmbapi service businessprofileperformance on GetDailyMetricsTimeSeries returning Error 404: Requested entity was not found

huangapple go评论91阅读模式
英文:

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(&quot;client_secret.json&quot;),
		option.WithScopes(Scope))
	if err != nil {
		log.Println(err.Error())
		return
	}
	cm := performanceService.Locations.GetDailyMetricsTimeSeries(&quot;locations/12345...&quot;)
	cm.DailyMetric(&quot;WEBSITE_CLICKS&quot;)
	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 &gt;= 200 || c &lt;= 299 {
		j, _ := response.MarshalJSON()
		log.Println(j)
	}

my client_secret.json file is like this
>

{
	&quot;type&quot;: &quot;&quot;,
	&quot;project_id&quot;: &quot;&quot;,
	&quot;private_key_id&quot;: &quot;&quot;,
	&quot;private_key&quot;: &quot;&quot;,
	&quot;client_email&quot;: &quot;&quot;,
	&quot;client_id&quot;: &quot;&quot;,
	&quot;auth_uri&quot;: &quot;https://accounts.google.com/o/oauth2/auth&quot;,
	&quot;token_uri&quot;: &quot;https://accounts.google.com/o/oauth2/token&quot;,
	&quot;auth_provider_x509_cert_url&quot;: &quot;https://www.googleapis.com/oauth2/v1/certs&quot;,
	&quot;client_x509_cert_url&quot;: &quot;&quot;
}

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(&quot;config/client_secret.json&quot;)
if err != nil {
	log.Println(&quot;error oppening json&quot;)
	return &amp;google.Credentials{}, err
}
defer jsonFile.Close()
jsonData, _ := ioutil.ReadAll(jsonFile)
creds, err := google.CredentialsFromJSONWithParams(ctx, jsonData, google.CredentialsParams{Scopes: []string{scope}, Subject: &quot;account@email.com&quot;})
if err != nil {
	return &amp;google.Credentials{}, err
}
return creds, nil

}

then

ctx := context.Background()
creds, err := appCreds.GetCredentials(ctx, &quot;https://www.googleapis.com/auth/business.manage&quot;)
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(&quot;locations/{location_id}&quot;)
response, err := cm.Do()

huangapple
  • 本文由 发表于 2022年12月17日 09:06:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/74831110.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定