2015-10-13 09:47
浏览 46

使用App Engine开发服务器向Google API声明身份

I have a Golang app running on App Engine and I would like to call the Analytics API to process some metrics. Both on App Engine and on the development server.

This page describes the overall procedure using a service account for server-to-server communication and getting an access token using OAuth2. I would like to avoid doing this myself so I found Google APIs for Go and the following that describes how to do it for App Engine.

I have added the App Engine app's service account email to Google Analytics for read access.

I have played around with queries using the Query Explorer and that works great. I have even used the API Query URI from here that includes the proper access token and that works fine. That works fine with cURL or calling it through urlfetch on App Engine.

What I need is to get hold of an access token automatically on App Engine and also while using the development server.

In here is an example for URL shortener which a have modified to use the Analytics API instead.

    client := http.Client{
            Transport: &oauth2.Transport{
                    Source: google.AppEngineTokenSource(c, analytics.AnalyticsScope),
                    Base: &urlfetch.Transport{Context: c},
    svc, err := analytics.New(&client)
    if err != nil {
            return nil, err

    data, err := svc.Data.Ga.Get("...")

The response in the development server is 401 Invalid Credentials, authError.

Is this supposed to work like this or am I missing something?


So, the problem is half solved. It works fine now using the development server when I pass in the service account's email and .pem file. However, that very same service account that is in App Engine does not work with the above code!

googleapi: Error 403: User does not have any Google Analytics account., insufficientPermissions

User should be the service account that I already successfully use on the development server, so I don't really understand this problem. It's the exact same code on App Engine and on the development server as well.

Google Analytics is set up to have read access and I use the read-only API. And it already works on the development server so that should be fine.

What might be different that is the issue?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongxiegao3071
    dongxiegao3071 2015-10-13 10:58

    It is possible to provide the development server with the necessary information at start like here.

    $ dev_appserver.py --appidentity_email_address email --appidentity_private_key_path file.pem

    And for App Engine you need to not use this Service Account but the one with the app name:

    点赞 评论