我在尝试从 GoogleAnalytics API 获取几天前运行良好的代码时遇到问题。
为此,我引用了以下 DLL:
Google.GData.Analytics.dll
Google.GData.Client.dll
Google.GData.Extensions.dll
我正在使用以下代码:
Dim visits As String = String.Empty
Dim username As String = "myuser@mydomain.com"
Dim pass As String = "mypassword"
Const dataFeedUrl As String = "https://www.google.com/analytics/feeds/data"
Dim query As AccountQuery = New AccountQuery()
Dim service As AnalyticsService = New AnalyticsService("MyWebAnalyticsService")
service.setUserCredentials(username, pass)
Dim accountFeed As AccountFeed = service.Query(query) ''----------> Exception thrown in this line: GDataRequestException Execution of request failed: https://www.google.com/analytics/feeds/accounts/default
我认为这与阻止我正在使用的帐户有关,但事实并非如此,因为我验证了为另一个分析帐户注册该站点并且仍然无法正常工作。
正如我所说,这段代码一直在完美运行,但昨天突然停止了。
你能帮我找出问题所在吗?也许用户凭据的设置方式已经改变,我遗漏了一些东西。
非常感谢您的帮助。
' ----Update---- 我设法使它工作,现在我可以查询所需域的访问。代码如下:
Dim visits As String = String.Empty
Dim username As String = "myuser@mydomain.com"
Dim pass As String = "mypassword"
'按照https://developers.google.com/analytics/resources/articles/gdata-migration-guide上的说明(在 Google API 控制台中创建项目)生成您的密钥 '一旦将其设置为请求我们的 GA 服务的查询字符串
Dim gkey As String = "key=yourkeystring"
'设置新的 URI 以检索提要数据并将其附加到生成的密钥
Dim dataFeedUrl As String = "https://www.google.com/analytics/feeds/data?" & gkey
'在我们的服务实例上创建和验证
Dim service As AnalyticsService = New AnalyticsService("MyAnaliticsService")
service.setUserCredentials(username, pass)
'使用您想要访问的帐户的配置文件 ID,您可以找到它
'登录您的分析帐户,在列表中选择所需的域(蓝色链接)单击管理员按钮,然后在配置文件选项卡上找到profile
'配置子选项卡,在这种情况下,您会在此处找到配置文件 ID,长度为 8 个字符 id 12345678
Dim query1 As DataQuery = New DataQuery(dataFeedUrl)
With query1
.Ids = "ga:12345678"
.Metrics = "ga:visits"
.Sort = "ga:visits"
.GAStartDate = DateTime.Now.AddMonths(-1).AddDays(-2).ToString("yyyy-MM-dd")
.GAEndDate = DateTime.Now.ToString("yyyy-MM-dd")
.StartIndex = 1
End With
'使用基于前一个查询生成的数据馈送来获取访问
Dim dataFeedVisits As DataFeed = service.Query(query1)
For Each entry As DataEntry In dataFeedVisits.Entries
Dim st As String = entry.Title.Text
Dim ss As String = entry.Metrics(0).Value
visits = ss
Next