Here are two small macros which allow you to use the Google and Microsoft Graph APIs via HTTP requests.
You obviously need to have an account with Google or Microsoft to be able to use their APIs.
The OAuth2 protocol necessary for the use of such an API is fully integrated, automatically and transparently.
I think that LibreOffice is the first software integrating such functionalities in such a simple language (BASIC).
Macro querying your Google Contacts:
Code: Select all
Sub Main
Rem Ask the user for their Google account
sUser = InputBox("Please enter your Google account", _
"Google API example")
Rem User clicked Cancel
if sUser = "" then
exit sub
endif
Rem Ask the user for their Query string
sQuery = InputBox("Please enter the query search string", _
"Google API example")
Rem User clicked Cancel
if sQuery = "" then
exit sub
endif
Rem To have access to Google Contact data we need to use the: https://www.googleapis.com/auth/contacts Google scope.
Rem We use the Url: people.googleapis.com who is already registered in the LibreOffice configuration for this scope.
sUrl = "people.googleapis.com"
Rem First we need to create the UNO OAuth2Service with the OAuth2 protocol support
Rem (ie: with an Url and a User email address)
oRequest = CreateUnoServiceWithArguments("io.github.prrvchr.OAuth2OOo.OAuth2Service", Array(sUrl, sUser))
Rem User canceled OAuth2 Wizard.
if isNull(oRequest) then
exit sub
endif
Rem To execute an HTTP request we first need a HTTP Request parameter
oParameter = oRequest.getRequestParameter("Google API")
Rem Default Requests method is already GET
Rem We use the method: people.searchContacts
oParameter.Url = "https://people.googleapis.com/v1/people:searchContacts"
Rem Before searching, we should send a warmup request with an empty query to update the cache.
oParameter.setQuery("query", "")
oResponse = oRequest.execute(oParameter)
Rem Now we can set the GET parameters:
oParameter.setQuery("query", sQuery)
oParameter.setQuery("readMask", "names,phoneNumbers")
Rem To obtain the HTTP response we use the execute() method
Rem of the OAuth2Service service with the HTTP parameter as argument
oResponse = oRequest.execute(oParameter)
if oResponse.Ok then
sText = getFirstResult(oResponse, sQuery)
else
sText = "ERROR: " & oResponse.Text
endif
Rem When it is finished we have to close the HTTP response
oResponse.close()
Msgbox sText
End Sub
Function getFirstResult(oResponse as Variant, sQuery as String) as String
sResult = sQuery & ": Not found..."
oResults = oResponse.getJson().getStructure("results")
if oResults.Count > 0 then
oPerson = oResults.getStructure(0).getStructure("person")
oNames = oPerson.getStructure("names")
if oNames.Count > 0 then
sResult = "Name: " + oNames.getStructure(0).getString("displayName")
endif
oPhones = oPerson.getStructure("phoneNumbers")
if oPhones.Count > 0 then
sResult = sResult + chr(13) + "Phone: " + oPhones.getStructure(0).getString("value")
endif
endif
getFirstResult = sResult
End Function
Code: Select all
Sub Main
Rem Ask the user for their Microsoft account
sUser = InputBox("Please enter your Microsoft account", _
"Microsoft API example")
Rem User clicked Cancel
if sUser = "" then
exit sub
endif
Rem Ask the user for their Query string
sQuery = InputBox("Please enter the query search string", _
"Microsoft API example")
Rem User clicked Cancel
if sQuery = "" then
exit sub
endif
Rem To have access to Microsoft Contact data we need to use the: MailboxSettings.Read Contacts.Read Microsoft scopes.
Rem We use the Url: graph.microsoft.com who is already registered in the LibreOffice configuration for there scopes.
sUrl = "graph.microsoft.com"
Rem First we need to create the UNO OAuth2Service with the OAuth2 protocol support
Rem (ie: with an Url and a User email address)
oRequest = CreateUnoServiceWithArguments("io.github.prrvchr.OAuth2OOo.OAuth2Service", Array(sUrl, sUser))
Rem User canceled OAuth2 Wizard.
if isNull(oRequest) then
exit sub
endif
Rem To execute an HTTP request we first need a HTTP Request parameter
oParameter = oRequest.getRequestParameter("Google API")
Rem Default Requests method is GET but can be changed (ie: oParameter.Method = "PUT")
Rem We use the Microsoft Search API
oParameter.Url = "https://graph.microsoft.com/v1.0/me/people/"
Rem To obtain the HTTP response we use the execute() method
Rem of the OAuth2Service service with the HTTP parameter as argument
oResponse = oRequest.execute(oParameter)
if oResponse.Ok then
sText = getFirstResult(oResponse, sQuery)
else
sText = "ERROR: " & oResponse.Text
endif
Rem When it is finished we have to close the HTTP response
oResponse.close()
Msgbox sText
End Sub
Function getFirstResult(oResponse as Variant, sQuery as String) as String
sResult = sQuery & ": Not found..."
oValues = oResponse.getJson().getStructure("value")
if oValues.Count > 0 then
oValue = oValues.getStructure(0)
sResult = "Name: " + oValue.getString("displayName")
oAddresses = oValue.getStructure("scoredEmailAddresses")
if oAddresses.Count > 0 then
oAddress = oAddresses.getStructure(0)
sResult = sResult + chr(13) + "Email: " + oAddress.getString("address")
endif
endif
getFirstResult = sResult
End Function
And if you go to: Tools -> Macros -> Edit Macros... -> OAuth2OOo you can have fun with these macros.
Ah yes for the lazy people who don't take the trouble to read the documentation:
- OAuth2OOo no longer works under OpenOffice and under Windows you need at least version 7 of LibreOffice.
- You also need a minimum JRE Java 11 regardless of the installation.
- These macros does not work if no document is open in LibreOffice (I don't know why...)
Voilà...