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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀