Last active
July 4, 2024 05:12
-
-
Save brucemcpherson/6937174 to your computer and use it in GitHub Desktop.
oauth2 examples for VBA- http://ramblings.mcpher.com/Home/excelquirks/googleoauth2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'gistThat@mcpher.com :do not modify this line - see ramblings.mcpher.com for details: updated on 15/10/2013 10:52:07 : from manifest:5055578 gist https://gist.github.com/brucemcpherson/6937174/raw/oauthExamples.vba | |
Option Explicit | |
' oauth examples | |
' v1.1 | |
' convienience function for auth.. | |
Public Function getGoogled(scope As String, _ | |
Optional replacementpackage As cJobject = Nothing, _ | |
Optional clientID As String = vbNullString, _ | |
Optional clientSecret As String = vbNullString, _ | |
Optional complain As Boolean = True, _ | |
Optional cloneFromeScope As String = vbNullString) As cOauth2 | |
Dim o2 As cOauth2 | |
Set o2 = New cOauth2 | |
With o2.googleAuth(scope, replacementpackage, clientID, clientSecret, complain, cloneFromeScope) | |
If Not .hasToken And complain Then | |
MsgBox ("Failed to authorize to google for scope " & scope & ":denied code " & o2.denied) | |
End If | |
End With | |
Set getGoogled = o2 | |
End Function | |
Private Sub testOauth2() | |
Dim myConsole As cJobject | |
' if you are calling for the first time ever you can either provide your | |
' clientid/clientsecret - or pass the the jsonparse retrieved from the google app console | |
' normally all this stuff comes from encrpted registry store | |
' first ever | |
'Set myConsole = makeMyGoogleConsole | |
'With getGoogled("analytics", myConsole) | |
' Debug.Print .authHeader | |
' .tearDown | |
' End With | |
'or you can do first ever like this | |
'With getGoogled("viz", , "xxxxx.apps.googleusercontent.com", "xxxxx") | |
' Debug.Print .authHeader | |
' .tearDown | |
'End With | |
' all other times this is what is needed | |
With getGoogled("drive") | |
Debug.Print .authHeader | |
.tearDown | |
End With | |
' lets auth and have a look at the contents | |
'Debug.Print objectStringify(getGoogled("drive")) | |
' all other times this is what is needed | |
With getGoogled("analytics") | |
Debug.Print .authHeader | |
.tearDown | |
End With | |
' here's an example of cloning credentials from a different scope for 1st time in | |
With getGoogled("urlshortener", , , , , "drive") | |
Debug.Print .authHeader | |
.tearDown | |
End With | |
With getGoogled("urlshortener") | |
Debug.Print .authHeader | |
.tearDown | |
End With | |
' if you made one, clean it up | |
If Not myConsole Is Nothing Then | |
myConsole.tearDown | |
End If | |
End Sub | |
Private Function makeMyGoogleConsole() As cJobject | |
Dim consoleJSON As String | |
consoleJSON = _ | |
"{'installed':{'auth_uri':'https://accounts.google.com/o/oauth2/auth'," & _ | |
"'client_secret':'xxxxxxxx'," & _ | |
"'token_uri':'https://accounts.google.com/o/oauth2/token'," & _ | |
"'client_email':'','redirect_uris':['urn:ietf:wg:oauth:2.0:oob','oob']," & _ | |
"'client_x509_cert_url':'','client_id':'xxxxxxx.apps.googleusercontent.com'," & _ | |
"'auth_provider_x509_cert_url':'https://www.googleapis.com/oauth2/v1/certs'}}" | |
Set makeMyGoogleConsole = JSONParse(consoleJSON) | |
End Function | |
Private Sub showLinkedinConsole() | |
Dim url As String, cb As cBrowser | |
Set cb = New cBrowser | |
' see http://excelramblings.blogspot.co.uk/2012/10/somewhere-to-keep-those-api-keys-google.html | |
' for how to store credentials in a google lockbox | |
url = "https://script.google.com/a/macros/mcpher.com/s/" & _ | |
"AKfycbza96-Mpa47jlqXoPosk64bUfR8T7zO5POZMYyN45InrvX8gm28/exec" & _ | |
"?action=show&entry=linkedinauth" | |
With getGoogled("drive") | |
If .hasToken Then | |
Debug.Print cb.httpGET(url, , , , , .authHeader) | |
Else | |
MsgBox ("failed to authenticate: " & .denied) | |
End If | |
.tearDown | |
End With | |
cb.tearDown | |
End Sub | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment