HTTP.DLL - How to set Authorization Header?

richt

Member
Licensed User
Hi chaps,

Messing around with Google Calendar and the HTTP.DLL.

I have managed to write code to get an authentication token from Google, now I just need to add my token to an Authentication header in my next request to their server.

How would I go about doing this? The headers function appears to be read-only and putting the token in the ContentType field fails as well.

Here's the code so far:

B4X:
Sub Globals
   'Declare the global variables here.

End Sub

Sub App_Start
   Form1.Show
End Sub


Sub Button1_Click
   request.New1("https://www.google.com/accounts/ClientLogin")
   request.Method = "POST"
   request.ContentType = "application/x-www-form-urlencoded"
   stream.New1(request.GetStream,True)
   name="Email=" & googemail.Text & "&Passwd=" & googpassword.Text & "&source=obstudios-studiomanger-1&service=cl"
   stream.WriteBytes(stream.StringToBytes(name))
   response.New1
   request.GetAsyncResponse
End Sub

Sub request_Response
   If request.ResponseCode = 200 Then
      response.Value = request.AsyncResponse
      response.GetAsyncString
   Else
      Msgbox(request.ErrorMessage)
   End If
End Sub

Sub response_Stream
   servrep.Text = response.AsyncString
   
   'Get the Authorisation code
   locauth = StrIndexOf(response.AsyncString,"Auth=",0)
   authcode.Text = SubString(response.AsyncString,locauth+5,StrLength(response.AsyncString) - locauth+5)
   
   response.Close
   
   'Now connect up to Google Calendar
   request2.New1("http://www.google.com/calendar/feeds/default/private/full")
   request2.Method = "POST"
   'request2.Headers = "Authorization: GoogleLogin auth=" & authcode.Text <-- In an ideal world this would work
   request2.ContentType = "application/atom+xml;"
   
   'Construct the XML
   EventCode = "<entry xmlns:gd='http://schemas.google.com/g/2005'>" & CRLF & _
                 "<category scheme='http://schemas.google.com/g/2005#kind'" & CRLF & _
               "term='http://schemas.google.com/g/2005#event'/>" & CRLF & _
               "<published>2005-01-18T21:00:00Z</published>" & CRLF & _
                 "<updated>2006-01-01T00:00:00Z</updated>" & CRLF & _                 
                 "<title>Work Schedule</title>" & CRLF & _
                 "<content>No description</content>" & CRLF & _
                 "<author>" & CRLF & _
                   "<name>" & googname.Text & "</name>" & CRLF & _
                   "<email>" & googemail.Text & "</email>" & CRLF & _
                 "</author>" & CRLF & _
                 "<gd:when startTime='" & googdate.Text & "T" & googstart.Text & ":00Z'" & CRLF & _
                "endTime='" & googdate.Text & "T" & googend.Text & ":00Z'></gd:when>" & CRLF & _
                 "<gd:where valueString='Studio'/>" & CRLF & _  
               "<gd:eventStatus value='http://schemas.google.com/g/2005#event.confirmed'/>" & CRLF & _
               "<gd:visibility value='http://schemas.google.com/g/2005#event.public'/>" & CRLF & _
               "<gd:transparency value='http://schemas.google.com/g/2005'event.transparent'/>" & CRLF & _
               "</entry>"

   stream2.New1(request2.GetStream,True)
   stream2.WriteBytes(stream2.StringToBytes(eventcode))
   
   response2.New1
   request2.GetAsyncResponse
   
End Sub

Sub request2_Response
   
   If request2.ResponseCode = 200 Then
      response2.Value = request2.AsyncResponse
      response2.GetAsyncString
   Else
      Msgbox(request2.ErrorMessage)
   End If
End Sub

Sub response2_Stream
   Msgbox(response.AsyncString)
End Sub

Thanks in advance,

Rich
 

richt

Member
Licensed User
Thanks for the response Erel but I get an error from the Door library when trying to set the Authorization header.

obj.SetProperty("Authorization",authtoken) brings up 'Object reference not set to an instance of an object' but the referer header can be set fine (as per the example).

What am I missing?

Kind regards,

Rich
 

agraham

Expert
Licensed User
Longtime User
There is no Authorization property for a .NET HttpWebRequest. It would have to go in the Headers collection but unfortunately that is only exposed read-only in the HTTP library. You should be able to add it with the Door library.

Try this - I'm afraid that I haven't tested it though so might not work.

obj.FromLibrary("Main.WebRequest1","req",B4PObject(2))
obj.SetProperty("Referer","http://xxx.xxx.xx") ' as before

obj.Value = obj.GetProperty("Headers") ' gets the Header collection
obj.RunMethod2("Add", authheader, "System.String") ' adds the header string
 

richt

Member
Licensed User
There is no Authorization property for a .NET HttpWebRequest. It would have to go in the Headers collection but unfortunately that is only exposed read-only in the HTTP library. You should be able to add it with the Door library.

Try this - I'm afraid that I haven't tested it though so might not work.

obj.FromLibrary("Main.WebRequest1","req",B4PObject(2))
obj.SetProperty("Referer","http://xxx.xxx.xx") ' as before

obj.Value = obj.GetProperty("Headers") ' gets the Header collection
obj.RunMethod2("Add", authheader, "System.String") ' adds the header string

That did it! Thanks once again Agraham, you truly are a guru at this stuff. :)

Cheers,

Rich
 

richt

Member
Licensed User
Ah spoke too soon, no code errors any more but still receive an 'Authorization required' error from the server.

I'll keep plugging away with the Door library...
 
Top