Here is an example how to do authentication in google analytics. This example was written not by me (author is Mikael Thuneberg), I just would like to share it here as well, because this example helped me a lot.
There are many example in web with VBA example to get data from Google Analytics (I work on that topic as well).
Public Function getGAauthenticationToken(email As String, password As String)
'Fetches GA authentication token, which can then be used to fetch data with the getGAdata function
'Created by Mikael Thuneberg
Dim objhttp As Variant
Dim authToken As Variant
Dim URL As String
Dim tempAns As String
Dim authResponse As String
Dim ch As String
Dim CurChr As Integer
Dim authTokenStart As Integer
If email = "" Then
getGAauthenticationToken = ""
Exit Function
End If
If password = "" Then
getGAauthenticationToken = "Input password"
Exit Function
End If
'hex password
CurChr = 1
Do Until ((CurChr - 1) = Len(password))
ch = Mid(password, CurChr, 1)
Select Case Asc(ch)
Case 48 To 57, 65 To 90, 97 To 122
tempAns = tempAns & Mid(password, CurChr, 1)
Case 32
tempAns = tempAns & "%" & Hex(32)
Case Else
tempAns = tempAns & "%" & Format(Hex(Asc(Mid(password, CurChr, 1))), "00")
End Select
CurChr = CurChr + 1
Loop
password = tempAns
'/
On Error GoTo errhandler
' authentication
Set objhttp = CreateObject("MSXML2.ServerXMLHTTP")
URL = "https://www.google.com/accounts/ClientLogin"
objhttp.Open "POST", URL, False
objhttp.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
objhttp.send ("accountType=GOOGLE&Email=" & email & "&Passwd=" & password & "&service=analytics&Source=tool-1.0")
authResponse = objhttp.responseText
If InStr(1, authResponse, "BadAuthentication") = 0 Then
authTokenStart = InStr(1, authResponse, "Auth=") + 4
authToken = Right(authResponse, Len(authResponse) - authTokenStart)
getGAauthenticationToken = authToken
Else
getGAauthenticationToken = "Authentication failed"
End If
basta:
Exit Function
errhandler:
getGAauthenticationToken = "Authentication failed"
Resume basta
End Function
Here is another example, it gets real data from Google Analytics.
4 comments :
Hiya
I do a lot of work with Google Analytics but I cannot see what this code is set out to solve/do/etc.
Can you provide a business/non-code explanation of what this sets out to do? :-)
Thanks!
Mark
this example does not have any business value, I just looked couple examples "How to do authentication" and how to get data from "google analytics" account.
Later I will put demo what I'm trying to do
Great blog, I enjoyed reading it
Great read thhankyou
Post a Comment