我正在使用Google Analytics API向我的CMS用户显示他们的访问者数据。所有配置文件都在我自己的帐户下,所以使用客户端登录和一些谷歌.net库,我能够检索所有帐户的网络id来查询数据。现在,由于API已被弃用,所有帐户请求都将返回404。
我尝试了所有的方法来更新到2.4,但是没有任何成功。我的路该怎么走?因为我只需要使用自己的帐户登录一次,而不是重定向用户来接受我的应用程序。
使用服务帐户?我也有一个服务器应用程序接口密钥,但我不知道如何实现新的应用程序接口,可惜现在还没有.net库。欢迎任何建议!
发布于 2012-08-29 04:23:33
你遇到的主要问题是他们删除了2.3版API中包含的帐户Feed。这意味着任何关于账户、web属性、个人资料和目标的信息都不能使用旧的客户端库。它的其余部分,也就是数据查询,应该仍然可以工作,您应该根据migration documents对它们进行升级。
要解决你获取配置文件的问题,你必须切换到他们的management api。最简单的修复方法是去掉获取配置文件的旧方法,代之以简单的restful管理api实现。您的库的其余部分(例如获得访问者/访问者)应该仍然可以正常工作。
下面是一个小示例:
1)使用ClientLogin获取身份验证令牌。
2)通过向https://www.googleapis.com/analytics/v2.4/management/accounts/~all/webproperties/~all/profiles发送GET请求来获取配置文件xml。确保在请求request.Headers.Add("Authorization", String.Format("GoogleLogin auth={0}", clientLoginAuthToken));中包含authorization标头
3)使用XDocument解析结果!
发布于 2012-08-31 19:21:36
如果任何人需要使用webrequest和clientlogin从新的分析api管理提要获取他们的表If。以下是我的(快速)代码(感谢Bengel):
string queryString = String.Format("https://www.google.com/accounts/ClientLogin?accountType=GOOGLE&Email={0}&Passwd={1}&service=analytics&source={2}", __username, __pass, "yourlog");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(queryString);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string responseContent = new StreamReader(response.GetResponseStream()).ReadToEnd();
string authCode = responseContent.Substring(responseContent.LastIndexOf("Auth=") + 5);
queryString = "https://www.googleapis.com/analytics/v2.4/management/accounts/~all/webproperties/~all/profiles";
request = (HttpWebRequest)WebRequest.Create(queryString);
request.Headers.Add("Authorization", String.Format("GoogleLogin auth={0}", authCode));
response = (HttpWebResponse)request.GetResponse();
XDocument doc = XDocument.Load(new StreamReader(response.GetResponseStream()));
var entries = (from item in doc.Root.Elements("{http://www.w3.org/2005/Atom}entry")
select new
{
tableid = item.Elements("{http://schemas.google.com/analytics/2009}property").ElementAt(4).Attribute("value").Value,
profileid = item.Elements("{http://schemas.google.com/analytics/2009}property").ElementAt(1).Attribute("value").Value
});https://stackoverflow.com/questions/12141877
复制相似问题