Voila deux petites macros qui permettent d'utiliser via des requêtes HTTP les API de Google et Microsoft Graph.
Il faut évidement avoir un compte chez Google ou Microsoft pour pouvoir utiliser leurs API.
Le protocole OAuth2 nécessaire à l'utilisation de telle API est totalement intégré et cela de façon automatique et transparente.
Je pense que LibreOffice est le premier logiciel intégrant dans un langage aussi simple (BASIC) de telles fonctionnalités.

Macro interrogeant vos Contacts Google:
Code : Tout sélectionner
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 : Tout sélectionner
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
Il faut bien sur installer la dernière version de OAuth2OOo...
Et si vous allez dans: Outils -> Macros -> Éditer les Macros... -> OAuth2OOo vous pourrez vous amuser avec ces macros.
Ah oui pour les fainéant qui ne prend pas la peine de lire les documentations:
- OAuth2OOo ne fonctionne plus sous OpenOffice et sous Windows il faut la version 7 minimum de LibreOffice.
- Il faut aussi un JRE Java 11 minimum quelque soit l'installation.
- Les macros ne fonctionnent pas si aucun document n'est ouvert dans LibreOffice (je ne sais pas pourquoi...)
Voilà...