首頁 > 後端開發 > Golang > 主體

GetDailyMetricsTimeSeries 上的 golang gmbapi 服務 BusinessProfilePerformance 回傳錯誤 404:未找到要求的實體

WBOY
發布: 2024-02-06 10:45:07
轉載
1205 人瀏覽過

GetDailyMetricsTimeSeries 上的 golang gmbapi 服务 BusinessProfilePerformance 返回错误 404:未找到请求的实体

問題內容

我建構了傳遞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檔案的個人資訊


正確答案


問題出在身分驗證上,主題遺失了,所以我這樣處理:

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: "<a href="https://www.php.cn/link/89fee0513b6668e555959f5dc23238e9" class="__cf_email__" data-cfemail="f796949498829983b7929a969e9bd994989a">[email&#160;protected]</a>"})
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()
登入後複製

以上是GetDailyMetricsTimeSeries 上的 golang gmbapi 服務 BusinessProfilePerformance 回傳錯誤 404:未找到要求的實體的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
lsp
來源:stackoverflow.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!