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

Creating a macro - Writing a Script - Using the API

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

Postby dillydal » Thu Dec 05, 2013 10:31 pm

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

Code: Select all   Expand viewCollapse view
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
dillydal
 
Posts: 5
Joined: Thu Dec 05, 2013 10:26 pm

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

Postby Mr.Dandy » Fri Dec 06, 2013 4:21 pm

Hello,

Did you try to pass user and password directly in the URL?
OpenOffice 4.1.3 - Windows 7
User avatar
Mr.Dandy
 
Posts: 285
Joined: Tue Dec 11, 2012 4:22 pm

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

Postby dillydal » Sat Dec 07, 2013 11:57 pm

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
dillydal
 
Posts: 5
Joined: Thu Dec 05, 2013 10:26 pm

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

Postby Villeroy » Sun Dec 08, 2013 12:25 am

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, OpenOffice 4.x & LibreOffice 6.x
User avatar
Villeroy
Volunteer
 
Posts: 26264
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

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

Postby dillydal » Mon Dec 09, 2013 7:49 am

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
dillydal
 
Posts: 5
Joined: Thu Dec 05, 2013 10:26 pm

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

Postby hanya » Mon Dec 09, 2013 11:12 am

You need custom interaction handler for the authentication.
Code: Select all   Expand viewCollapse view
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
hanya
Volunteer
 
Posts: 882
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

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

Postby Mr.Dandy » Mon Dec 09, 2013 12:34 pm

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   Expand viewCollapse view
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.3 - Windows 7
User avatar
Mr.Dandy
 
Posts: 285
Joined: Tue Dec 11, 2012 4:22 pm

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

Postby dillydal » Wed Dec 11, 2013 6:36 am

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   Expand viewCollapse view
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   Expand viewCollapse view
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

Postby dillydal » Wed Dec 11, 2013 6:56 am

hanya wrote:You need custom interaction handler for the authentication.
Code: Select all   Expand viewCollapse view
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
dillydal
 
Posts: 5
Joined: Thu Dec 05, 2013 10:26 pm


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 8 guests