首页 > 后端开发 > 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学习者快速成长!