如何将 Google Analytics API 与 2-legged OAuth(Google Apps for Business)结合使用?

发布于 2024-12-06 00:34:47 字数 1366 浏览 0 评论 0原文

我想为我工作的企业开发一个应用程序。我们正在使用 Google Apps,并希望从 Google Analytics 获取数据并将其显示在我们的网络应用程序之一中。我不希望客户端看到任何授权应用程序的请求。我想使用 2-legged OAuth,例如 http ://www.google.com/support/a/bin/answer.py?hl=en&answer=162105,但 Google Analytics(分析)不在列表中。我还能使用它吗? .NET Google Data API 库Google API .NET 客户端 ?

编辑 1

使用 Google API .NET 客户端,我想出了一些应该符合我的感觉的东西:

var auth = new Google.Apis.Authentication.OAuth2LeggedAuthenticator(DOMAIN_CONSUMER_KEY, DOMAIN_CONSUMER_SECRET, USER_TO_IMPERSONATE, DOMAIN);

var service = new Google.Apis.Analytics.v3.AnalyticsService(auth);

service.Key = DEV_KEY_FROM_API_CONSOLE;

var _request = service.Management.Accounts.List();

foreach (var item in _request.Fetch().Items)
{
    Console.WriteLine(item.Name);
}

...但我收到此错误:

Google.Apis.Requests.RequestError 
InvalidCredentials [401]
Errors [
    Message[Invalid Credentials] 
    Location[Authorization - header] 
    Reason[authError] 
    Domain[global]
    ]

谢谢

I want to develop an application for the business I work. We are using Google Apps and want to get data from Google Analytics and show it in one of our web apps. I do not want the client to see any request to authorize the app. I want to use 2-legged OAuth like such http://www.google.com/support/a/bin/answer.py?hl=en&answer=162105 but Google Analytics is not in the list. Will I still be able to use it ? Is it supported in the .NET Google Data API library or the Google API .NET Client ?

EDIT 1 :

Using the Google API .NET Client, I came up with something that should work to my sense :

var auth = new Google.Apis.Authentication.OAuth2LeggedAuthenticator(DOMAIN_CONSUMER_KEY, DOMAIN_CONSUMER_SECRET, USER_TO_IMPERSONATE, DOMAIN);

var service = new Google.Apis.Analytics.v3.AnalyticsService(auth);

service.Key = DEV_KEY_FROM_API_CONSOLE;

var _request = service.Management.Accounts.List();

foreach (var item in _request.Fetch().Items)
{
    Console.WriteLine(item.Name);
}

... but I get this error :

Google.Apis.Requests.RequestError 
InvalidCredentials [401]
Errors [
    Message[Invalid Credentials] 
    Location[Authorization - header] 
    Reason[authError] 
    Domain[global]
    ]

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤千羽 2024-12-13 00:34:47

这篇博文逐步解释了如何使用 Google API .Net 客户端实现双腿身份验证。

http://bittwiddlers.org/?p=212#awp::?p =212

然而,作者以这样的评论结束了他的帖子:

上述特定于 google-api-dotnet-client 项目的信息是相关的,但是该库会像筛子一样泄漏内存,并且如果您尝试执行任何异步工作或使用 2LO(代表多个用户),将会降低您的性能)。

祝你好运,如果你找到了更好的解决方案,请告诉我,我有点厌倦了这个问题......

This blog post explains step by step how to implement a 2 legged authentication with the Google API .Net client.

http://bittwiddlers.org/?p=212#awp::?p=212

However, the author ends his post by this comment:

The above information specific to the google-api-dotnet-client project is relevant, but that library leaks memory like a sieve and will kill your performance if you try to do any asynchronous work or use 2LO (on behalf of multiple users).

Good luck and let me know if you figured out a better solution, I'm a bit sick of the issue...

万人眼中万个我 2024-12-13 00:34:47

我在此处回答了类似的问题。这回答了如何修复一些会导致 401 Invalid Credentials 的问题,并可能帮助您让 v3 版本的 .Net API 正常工作。我只是在此处添加此内容,因为您的解决方案使用已弃用的 v2 API 来规避您在身份验证方面遇到的问题。

I have answered a similar question here. That answers how to fix a couple of problems that will cause 401 Invalid Credentials and may help you get the v3 version of the .Net APIs working. I'm just adding this here as your solution uses the deprecated v2 API to circumvent the problem you were having with authentication.

原来是傀儡 2024-12-13 00:34:47

我找到了一种使用Google Data API 的.NET 库的方法。我找到了此链接,感谢<一个href="https://groups.google.com/d/msg/google-analytics-data-export-api/B5NvNVFn2pI/nqgVFNyVGq4J" rel="nofollow">这个人。以下是列出所有 Google Analytics(分析)帐户的工作代码,考虑到 GOOGLE_APPS_DOMAIN_SECRET 是使用 此方法,DOMAIN 是 Google Apps 的域,用户名是来自 Google Apps 的电子邮件的前缀(例如:“firstname.lastname”)我们想要模拟的 Google Apps 用户(例如管理员,以访问所有内容):

        var requestFactory = new GOAuthRequestFactory("an", "Any app name");
        requestFactory.ConsumerKey = DOMAIN;
        requestFactory.ConsumerSecret = GOOGLE_APPS_DOMAIN_SECRET;

        var service = new AnalyticsService("Any app name");
        service.RequestFactory = requestFactory;

        var query = new AccountQuery();
        query.OAuthRequestorId = username + "@" + DOMAIN;

        var serviceQuery = service.Query(query);

        foreach (AccountEntry Entry in serviceQuery.Entries)
        {
            WriteLine(Entry.Title.Text);
        }

唯一的问题是我必须模拟用户,而不能简单地访问所有内容。

编辑:方法已弃用

I found a method using .NET library for the Google Data API. I found this link thanks to this guy. Here is a working code that lists all Analytics accounts, considering that GOOGLE_APPS_DOMAIN_SECRET is the key generate using this method, the DOMAIN is the domain of the Google Apps, and username is the prefix (ex. : "firstname.lastname") of the email from the Google Apps user we want to impersonate (the admin for example, to have access to everything) :

        var requestFactory = new GOAuthRequestFactory("an", "Any app name");
        requestFactory.ConsumerKey = DOMAIN;
        requestFactory.ConsumerSecret = GOOGLE_APPS_DOMAIN_SECRET;

        var service = new AnalyticsService("Any app name");
        service.RequestFactory = requestFactory;

        var query = new AccountQuery();
        query.OAuthRequestorId = username + "@" + DOMAIN;

        var serviceQuery = service.Query(query);

        foreach (AccountEntry Entry in serviceQuery.Entries)
        {
            WriteLine(Entry.Title.Text);
        }

The only problem, is that I have to impersonate a user, and cannot simply have access to everything.

EDIT: Method deprecated

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文