I am trying to connect to the Google Analytics API from a .NET application. Here is the code I've come up with:
Public Function GetData() As String
Dim client As AssertionFlowClient = New AssertionFlowClient(GoogleAuthenticationServer.Description,
New X509Certificate2("C:\Users\xxxxx\privatekey.p12", "notasecret",
X509KeyStorageFlags.Exportable Or X509KeyStorageFlags.MachineKeySet)) With {
.Scope = "analytics.readonly",
.ServiceAccountId = "xxxxx@developer.gserviceaccount.com"}
Dim authenticator As OAuth2Authenticator(Of AssertionFlowClient) = New OAuth2Authenticator(Of AssertionFlowClient)(client, AddressOf AssertionFlowClient.GetState)
Dim service As AnalyticsService = New AnalyticsService(authenticator)
Dim profileId As String = "ga:xxxxx"
Dim startDate As String = "2013-02-15"
Dim endDate As String = "2013-02-20"
Dim metrics As String = "ga:visitors"
Dim request As DataResource.GaResource.GetRequest = service.Data.Ga.Get(profileId, startDate, endDate, metrics)
Dim data As GaData = request.Fetch()
Return data.ToString()
End Function
My problem is that when I hit this method, I get an exception on the request.Fetch()
line that says "Error occurred while sending a direct message or getting the response." The inner exception says, "The remote server returned an error: (400) Bad Request."
This error is obviously pretty vague. According to Google's documentation of the error codes, 400 Bad Request indicates that the input parameters don't make sense somehow. Can anyone diagnose the problem by looking at the parameters in my code? Otherwise, how would I even begin to track down what I'm doing wrong? There's virtually no information presented in the error.
(At least I've gotten past the authentication stage... I think.)