如何将 Google Analytics API 与 2-legged OAuth(Google Apps for Business)结合使用?
我想为我工作的企业开发一个应用程序。我们正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这篇博文逐步解释了如何使用 Google API .Net 客户端实现双腿身份验证。
http://bittwiddlers.org/?p=212#awp::?p =212
然而,作者以这样的评论结束了他的帖子:
祝你好运,如果你找到了更好的解决方案,请告诉我,我有点厌倦了这个问题......
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:
Good luck and let me know if you figured out a better solution, I'm a bit sick of the issue...
我在此处回答了类似的问题。这回答了如何修复一些会导致
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.我找到了一种使用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 用户(例如管理员,以访问所有内容):
唯一的问题是我必须模拟用户,而不能简单地访问所有内容。
编辑:方法已弃用
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) :
The only problem, is that I have to impersonate a user, and cannot simply have access to everything.
EDIT: Method deprecated