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

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

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

问题

我构建了一个传递CredentialsFile和Scope进行身份验证的服务,然后我使用正确的名称(locations/{location_id})调用GetDailyMetricsTimeSeries,但返回错误404。

  1. ctx := context.Background()
  2. performanceService, err := businessprofileperformance.NewService(ctx,
  3. option.WithCredentialsFile("client_secret.json"),
  4. option.WithScopes(Scope))
  5. if err != nil {
  6. log.Println(err.Error())
  7. return
  8. }
  9. cm := performanceService.Locations.GetDailyMetricsTimeSeries("locations/12345...")
  10. cm.DailyMetric("WEBSITE_CLICKS")
  11. cm.DailyRangeStartDateYear(2022)
  12. cm.DailyRangeStartDateMonth(6)
  13. cm.DailyRangeStartDateDay(1)
  14. cm.DailyRangeEndDateYear(2022)
  15. cm.DailyRangeEndDateMonth(12)
  16. cm.DailyRangeEndDateDay(30)
  17. response, err := cm.Do()
  18. if err != nil {
  19. log.Println(err.Error())
  20. return
  21. }
  22. if c := response.HTTPStatusCode; c >= 200 || c <= 299 {
  23. j, _ := response.MarshalJSON()
  24. log.Println(j)
  25. }

我的client_secret.json文件如下:

  1. {
  2. "type": "",
  3. "project_id": "",
  4. "private_key_id": "",
  5. "private_key": "",
  6. "client_email": "",
  7. "client_id": "",
  8. "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  9. "token_uri": "https://accounts.google.com/o/oauth2/token",
  10. "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  11. "client_x509_cert_url": ""
  12. }

我认为问题是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.

>

  1. ctx := context.Background()
  2. performanceService, err := businessprofileperformance.NewService(ctx,
  3. option.WithCredentialsFile(&quot;client_secret.json&quot;),
  4. option.WithScopes(Scope))
  5. if err != nil {
  6. log.Println(err.Error())
  7. return
  8. }
  9. cm := performanceService.Locations.GetDailyMetricsTimeSeries(&quot;locations/12345...&quot;)
  10. cm.DailyMetric(&quot;WEBSITE_CLICKS&quot;)
  11. cm.DailyRangeStartDateYear(2022)
  12. cm.DailyRangeStartDateMonth(6)
  13. cm.DailyRangeStartDateDay(1)
  14. cm.DailyRangeEndDateYear(2022)
  15. cm.DailyRangeEndDateMonth(12)
  16. cm.DailyRangeEndDateDay(30)
  17. response, err := cm.Do()
  18. if err != nil {
  19. log.Println(err.Error())
  20. return
  21. }
  22. if c := response.HTTPStatusCode; c &gt;= 200 || c &lt;= 299 {
  23. j, _ := response.MarshalJSON()
  24. log.Println(j)
  25. }

my client_secret.json file is like this
>

  1. {
  2. &quot;type&quot;: &quot;&quot;,
  3. &quot;project_id&quot;: &quot;&quot;,
  4. &quot;private_key_id&quot;: &quot;&quot;,
  5. &quot;private_key&quot;: &quot;&quot;,
  6. &quot;client_email&quot;: &quot;&quot;,
  7. &quot;client_id&quot;: &quot;&quot;,
  8. &quot;auth_uri&quot;: &quot;https://accounts.google.com/o/oauth2/auth&quot;,
  9. &quot;token_uri&quot;: &quot;https://accounts.google.com/o/oauth2/token&quot;,
  10. &quot;auth_provider_x509_cert_url&quot;: &quot;https://www.googleapis.com/oauth2/v1/certs&quot;,
  11. &quot;client_x509_cert_url&quot;: &quot;&quot;
  12. }

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

问题出在身份验证上,缺少了主题,所以我这样处理:

  1. func (a *AppCredential) GetCredentials(ctx context.Context, scope string) (*google.Credentials, error) {
  2. jsonFile, err := os.Open("config/client_secret.json")
  3. if err != nil {
  4. log.Println("error oppening json")
  5. return &google.Credentials{}, err
  6. }
  7. defer jsonFile.Close()
  8. jsonData, _ := ioutil.ReadAll(jsonFile)
  9. creds, err := google.CredentialsFromJSONWithParams(ctx, jsonData, google.CredentialsParams{Scopes: []string{scope}, Subject: "account@email.com"})
  10. if err != nil {
  11. return &google.Credentials{}, err
  12. }
  13. return creds, nil
  14. }
  15. 然后
  16. ctx := context.Background()
  17. creds, err := appCreds.GetCredentials(ctx, "https://www.googleapis.com/auth/business.manage")
  18. if err != nil {
  19. log.Println(err.Error())
  20. return
  21. }
  22. performanceService, err := businessprofileperformance.NewService(ctx, option.WithCredentials(creds))
  23. if err != nil {
  24. log.Println(err.Error())
  25. return
  26. }
  27. cm := performanceService.Locations.GetDailyMetricsTimeSeries("locations/{location_id}")
  28. response, err := cm.Do()
英文:

the problem was in the authentication, the subject was missing, so I managed it this way:

  1. func (a *AppCredential) GetCredentials(ctx context.Context, scope string) (*google.Credentials, error) {
  2. jsonFile, err := os.Open(&quot;config/client_secret.json&quot;)
  3. if err != nil {
  4. log.Println(&quot;error oppening json&quot;)
  5. return &amp;google.Credentials{}, err
  6. }
  7. defer jsonFile.Close()
  8. jsonData, _ := ioutil.ReadAll(jsonFile)
  9. creds, err := google.CredentialsFromJSONWithParams(ctx, jsonData, google.CredentialsParams{Scopes: []string{scope}, Subject: &quot;account@email.com&quot;})
  10. if err != nil {
  11. return &amp;google.Credentials{}, err
  12. }
  13. return creds, nil

}

then

  1. ctx := context.Background()
  2. creds, err := appCreds.GetCredentials(ctx, &quot;https://www.googleapis.com/auth/business.manage&quot;)
  3. if err != nil {
  4. log.Println(err.Error())
  5. return
  6. }
  7. performanceService, err := businessprofileperformance.NewService(ctx, option.WithCredentials(creds))
  8. if err != nil {
  9. log.Println(err.Error())
  10. return
  11. }
  12. cm := performanceService.Locations.GetDailyMetricsTimeSeries(&quot;locations/{location_id}&quot;)
  13. 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:

确定