[Solved] Open a file with http authentication with a macro

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
dillydal
Posts: 5
Joined: Thu Dec 05, 2013 10:26 pm

[Solved] Open a file with http authentication with a macro

Post by dillydal »

I have written a macro to open an xml file which is protected by http authentication in calc -

Code: Select all

dim sURL as string 
dim oArgs(0) As New com.sun.star.beans.PropertyValue
dim oDocStatus as object
dim oihandler 
oihandler = createUnoService("com.sun.star.task.InteractionHandler")
sURL = "http://127.0.0.1:8080/requests/status.xml"
oArgs(0).Name = "InteractionHandler"
oArgs(0).Value = oihandler
oDocStatus = StarDesktop.loadComponentFromURL(sURL, "_default", 0, oArgs)
This works fine except that each time I run it -

I have to key in my user name / password in an Authentication Required window
I have to click OK on the subsequent "Text Import" window.
How could I improve this macro to avoid repetitively doing these actions? (removing http authentication from the xml page is not possible)
Last edited by Hagar Delest on Wed Dec 11, 2013 3:04 pm, edited 1 time in total.
Reason: tagged [Solved].
LibreOffice 4.0.6.2 / Windows 7
User avatar
Mr.Dandy
Posts: 427
Joined: Tue Dec 11, 2012 4:22 pm

Re: How to open a file with http authentication with a macro

Post by Mr.Dandy »

Hello,

Did you try to pass user and password directly in the URL?
OpenOffice 4.1.12 - Windows 10
dillydal
Posts: 5
Joined: Thu Dec 05, 2013 10:26 pm

Re: How to open a file with http authentication with a macro

Post by dillydal »

Do you mean http://username:password@127.0.0.1:8080 ... status.xml ?

No, it doesn't work. In fact it causes libreoffice to crash. However, the above URL does automatically log in when I paste it in Google Chrome.
LibreOffice 4.0.6.2 / Windows 7
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: How to open a file with http authentication with a macro

Post by Villeroy »

StarBasic is a simple API caller for this office suite. Downloading data via http behind a log-in would be easy to do without any office suite at all, using any of the well established, fully featured programming languages.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
dillydal
Posts: 5
Joined: Thu Dec 05, 2013 10:26 pm

Re: How to open a file with http authentication with a macro

Post by dillydal »

Do you mean UNO doesn't not provide an "non-interactive http authentication" service?
What about the "Text Import" window? Is there a way specify the default import settings when opening a file?
LibreOffice 4.0.6.2 / Windows 7
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: How to open a file with http authentication with a macro

Post by hanya »

You need custom interaction handler for the authentication.

Code: Select all

Sub AuthenticationTest
  dim sURL as string
  dim oArgs(0) As New com.sun.star.beans.PropertyValue
  dim oDocStatus as object
  dim oihandler
  oihandler = CreateUnoListener("InteractionHandler_", "com.sun.star.task.XInteractionHandler")
  sURL = "http://127.0.0.1:8080/requests/status.xml"
  oArgs(0).Name = "InteractionHandler"
  oArgs(0).Value = oihandler
  oDocStatus = StarDesktop.loadComponentFromURL(sURL, "_default", 0, oArgs)
End Sub


Sub InteractionHandler_handle(req)
  r = req.getRequest()
  If CheckExceptionType(r, "com.sun.star.ucb.URLAuthenticationRequest") Then
    conts = req.getContinuations()
    for i = 0 to ubound(conts) step 1
      cont = conts(i)
      If HasUnoInterfaces(cont, "com.sun.star.ucb.XInteractionSupplyAuthentication2") Then
        cont.setUserName("foo")
        cont.setPassword("bar")
        cont.select()
        Exit For
      End If
    next
  End If
End Sub


Function CheckExceptionType(e, sType As String) As Boolean
  ret = False
  idlclass = CreateUnoService("com.sun.star.reflection.CoreReflection").getType(e)
  If not IsNull(idlclass) Then
    ret = (idlclass.getTypeClass() = com.sun.star.uno.TypeClass.EXCEPTION and _
           idlclass.getName() = sType)
  End If
  CheckExceptionType = ret
End Function
And pass FilterName to loadComponentFromURL method to choose the filter to open the file, you can find some examples for this way.
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
User avatar
Mr.Dandy
Posts: 427
Joined: Tue Dec 11, 2012 4:22 pm

Re: How to open a file with http authentication with a macro

Post by Mr.Dandy »

dillydal wrote:No, it doesn't work. In fact it causes libreoffice to crash.
I try this code and no problem with AOO:

Code: Select all

Function GetFileViaHttp
	Dim url$
	Dim oHttp As Object
	
	url = "http://username:password@127.0.0.1:8080/requests/status.xml"
	Set oHttp = CreateObject("MSXML2.XMLHTTP")
	
	oHttp.Open "GET", url, False
	oHttp.Send
	GetFileViaHttp = oHttp.responseText
End Function
OpenOffice 4.1.12 - Windows 10
dillydal
Posts: 5
Joined: Thu Dec 05, 2013 10:26 pm

Re: How to open a file with http authentication with a macro

Post by dillydal »

Mr.Dandy wrote:
dillydal wrote:No, it doesn't work. In fact it causes libreoffice to crash.
I try this code and no problem with AOO:

Code: Select all

Function GetFileViaHttp
	Dim url$
	Dim oHttp As Object
	
	url = "http://username:password@127.0.0.1:8080/requests/status.xml"
	Set oHttp = CreateObject("MSXML2.XMLHTTP")
	
	oHttp.Open "GET", url, False
	oHttp.Send
	GetFileViaHttp = oHttp.responseText
End Function
When tested your code, the debugger stopped on the line

Code: Select all

oHttp.Open "GET", url, False
and displayed an error messsage -

BASIC runtime error.
An exception occurred
Type: com.sun.star.uno.RuntimeException
Message: [automation bridge] unexpected exception in IUnknownWrapper_Impl::hasMethod !.
LibreOffice 4.0.6.2 / Windows 7
dillydal
Posts: 5
Joined: Thu Dec 05, 2013 10:26 pm

Re: How to open a file with http authentication with a macro

Post by dillydal »

hanya wrote:You need custom interaction handler for the authentication.

Code: Select all

Sub AuthenticationTest
  dim sURL as string
  dim oArgs(0) As New com.sun.star.beans.PropertyValue
  dim oDocStatus as object
  dim oihandler
  oihandler = CreateUnoListener("InteractionHandler_", "com.sun.star.task.XInteractionHandler")
  sURL = "http://127.0.0.1:8080/requests/status.xml"
  oArgs(0).Name = "InteractionHandler"
  oArgs(0).Value = oihandler
  oDocStatus = StarDesktop.loadComponentFromURL(sURL, "_default", 0, oArgs)
End Sub


Sub InteractionHandler_handle(req)
  r = req.getRequest()
  If CheckExceptionType(r, "com.sun.star.ucb.URLAuthenticationRequest") Then
    conts = req.getContinuations()
    for i = 0 to ubound(conts) step 1
      cont = conts(i)
      If HasUnoInterfaces(cont, "com.sun.star.ucb.XInteractionSupplyAuthentication2") Then
        cont.setUserName("foo")
        cont.setPassword("bar")
        cont.select()
        Exit For
      End If
    next
  End If
End Sub


Function CheckExceptionType(e, sType As String) As Boolean
  ret = False
  idlclass = CreateUnoService("com.sun.star.reflection.CoreReflection").getType(e)
  If not IsNull(idlclass) Then
    ret = (idlclass.getTypeClass() = com.sun.star.uno.TypeClass.EXCEPTION and _
           idlclass.getName() = sType)
  End If
  CheckExceptionType = ret
End Function
And pass FilterName to loadComponentFromURL method to choose the filter to open the file, you can find some examples for this way.

I don't yet understand your code but it works perfectly! Thanks
LibreOffice 4.0.6.2 / Windows 7
Post Reply