du131642 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?

UPDATE

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 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:

    app_name@appspot.gserviceaccount.com
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大