Skip to content

Google APIs

Tim Hall edited this page Oct 10, 2015 · 4 revisions

An example of using Google APIs with VBA-Web is included in the examples/ folder of the project. Some details:

Authentication

There are many approaches to using Google's OAuth 2.0 authentication, but for use with VBA-Web, none that require a callback url could be used. That limits the authentication approach to Installed Application / Device.

  1. Go to Google's API Console to create a new application with Google. In the Client ID Settings section, select Installed application and Other. Store the generated Client Id and Secret.
  2. Determine the desired scope for your application. For example, in order to access Google Analytics data, this needs to be defined in the scope as https://www.googleapis.com/auth/analytics.readonly. In general, each scope is prefixed by https://www.googleapis.com/auth/ with the specific scope then added. For a list of available scopes, visit the Google OAuth 2.0 Playground
  3. Add the GoogleAuthenticator class to your project and set it up as follows:
Dim Auth As New GoogleAuthenticator
Auth.Setup _
  ClientId:="ClientId", _
  ClientSecret:="ClientSecret"
Auth.Scope = Array("https://www.googleapis.com/auth/analytics.readonly")
Auth.Login
Set Client.Authenticator = Auth

Example Request

With authentication setup for your client, you can use the normal request configuration. Here's an example of the desired url for retreiving Google Analytics data and how to setup the request:

' Desired request: GET https://www.googleapis.com/analytics/v3/data/ga
'                        ?ids=ga:12345
'                        &start-date=2008-10-01
'                        &end-date=2008-10-31
'                        &metrics=ga:visits,ga:bounces
' Client.BaseUrl = "https://www.googleapis.com/analytics/v3/"

Public Function AnalyticsRequest(ProfileId As String, StartDate As Date, EndDate As Date) As RestRequest
    Set AnalyticsRequest = New WebRequest
    AnalyticsRequest.Resource = "data/ga"
    AnalyticsRequest.Method = WebMethod.HttpGet

    AnalyticsRequest.AddQuerystringParam "ids", "ga:" & ProfileId
    AnalyticsRequest.AddQuerystringParam "start-date", Format(StartDate, "yyyy-mm-dd")
    AnalyticsRequest.AddQuerystringParam "end-date", Format(EndDate, "yyyy-mm-dd")
    AnalyticsRequest.AddQuerystringParam "metrics", "ga:visits,ga:bounces"    
End Function

Links:

Clone this wiki locally