AnalyticsReporting v4范围错误

时间:2018-08-26 13:56:10

标签: go google-api

我正在尝试使用golang获取Google Analytics(分析)API报告。我成功地遵循了工作表的快速入门示例,但无法使其与analyticsreporting / v4一起使用

https://developers.google.com/sheets/api/quickstart/go

我也正在尝试将此示例用于分析报告。

https://github.com/sudo-suhas/example-analyticsreporting/blob/master/hello_analytics.go

我收到以下错误。对于ruby脚本,我具有相同的certificate.json文件,该文件可以正常工作,如果删除该脚本,token.json将被脚本替换。

  

googleapi:错误403:请求的身份验证范围不足。   禁止

以下是一些代码:

func main() {
b, err := ioutil.ReadFile("credentials.json")
if err != nil {
    log.Fatalf("Unable to read client secret file: %v", err)
}

// If modifying these scopes, delete your previously saved token.json.
config, err := google.ConfigFromJSON(b, "https://www.googleapis.com/auth/analytics.readonly")
if err != nil {
            fmt.Println(err)
    log.Fatalf("Unable to parse client secret file to config: %v", err)
}
client := getClient(config)

service, err := ga.New(client)

if err != nil {
    log.Fatalf("Unable to retrieve Analytics client: %v", err)
}

res, err := getReport(service)
 if err != nil {
            fmt.Println("error getting analytics result")
    fmt.Println(err)
}

fmt.Println(res)

}

func getReport(service *ga.Service) (*ga.GetReportsResponse, error) {
    // A GetReportsRequest instance is a batch request
    // which can have a maximum of 5 requests
    req := &ga.GetReportsRequest{
        ReportRequests: []*ga.ReportRequest{
            // Create the ReportRequest object.
            {
                ViewId: "12345", 
                DateRanges: []*ga.DateRange{
                    // Create the DateRange object.
                    {StartDate: "7daysAgo", EndDate: "today"},
                },
                Metrics: []*ga.Metric{
                    // Create the Metrics object.
                    {Expression: "ga:sessions"},
                },
                Dimensions: []*ga.Dimension{
                    {Name: "ga:country"},
                },
            },
        },
    }

    fmt.Println("Doing GET request from analytics reporting")
    // Call the BatchGet method and return the response.
    return service.Reports.BatchGet(req).Do()
}

0 个答案:

没有答案