RE; Bad Request?

Hi,

Been having a nightmare with this authentication in a desktop application.

Finally decided to implement just the 2nd part of the authentication using
the token only, I thought this would be more straight forward, but
apparently not.

Dim request As HttpWebRequest =
WebRequest.Create(“https://api.sandbox.freeagent.com/v2/company”)
request.Headers.Add(“Authorization”, "OAuth ")
request.Method = “GET”
request.ContentType = “application/json”

    ' Send Request & Get Response
    Dim response = request.GetResponse()

    ' Get the Response Stream
    Dim reader As StreamReader = New 

StreamReader(response.GetResponseStream())
Dim json As String = reader.ReadToEnd()

    ' Retrieve the Access Token
    Dim ser As JavaScriptSerializer = New JavaScriptSerializer()
    Dim x As Dictionary(Of String, Object) = ser.DeserializeObject(json)
    Dim accessToken As String = x("access_token").ToString()

Should I not just get a json response instead of a bad request or am I
missing something here?

This does work on google playground fine and seems to have the same method
and token etc

Thanks,

Rob

Hi Rob,

hopefully you have solved this now. In case you haven’t make sure that you
send a User Agent header - the value can be anything you like but you need
to send one.

cheers
DaveOn Wednesday, November 26, 2014 10:24:05 AM UTC, Robert Tunna wrote:

Hi,

Been having a nightmare with this authentication in a desktop application.

Finally decided to implement just the 2nd part of the authentication using
the token only, I thought this would be more straight forward, but
apparently not.

Dim request As HttpWebRequest = WebRequest.Create("
https://api.sandbox.freeagent.com/v2/company")
request.Headers.Add(“Authorization”, "OAuth ")
request.Method = “GET”
request.ContentType = “application/json”

    ' Send Request & Get Response
    Dim response = request.GetResponse()

    ' Get the Response Stream
    Dim reader As StreamReader = New 

StreamReader(response.GetResponseStream())
Dim json As String = reader.ReadToEnd()

    ' Retrieve the Access Token
    Dim ser As JavaScriptSerializer = New JavaScriptSerializer()
    Dim x As Dictionary(Of String, Object) = 

ser.DeserializeObject(json)
Dim accessToken As String = x(“access_token”).ToString()

Should I not just get a json response instead of a bad request or am I
missing something here?

This does work on google playground fine and seems to have the same method
and token etc

Thanks,

Rob

Hi Rob,

Although I’m not a Visual Basic expert, the code you’ve posted looks
reasonably sane.

You don’t explicitly set an “Accept” header there. Unless your WebRequest
component adds it automatically, you could try setting it to
“application/json”.

The comment “Retrieve the Access Token” is worrying - to call the company
resource you need to have already exchanged the authorization token for an
access token (see
https://dev.freeagent.com/docs/oauth#the-access-token-request). Have you
already done this, or is your something other than an access token
(in which case you would expect a 401 unauthorized response).

If that doesn’t help, could you let me have the content of the response
returned by the server as that might throw some light on things. Also the
specific HTTP status code would be useful (you mentioned a "bad request -
is that an actual 400, or just a description of the problem?).

Also, you could change the endpoint to http://httpbin.org/get to see
exactly what your component is sending.

Thanks,
Paul.On Wednesday, 26 November 2014 10:24:05 UTC, Robert Tunna wrote:

Hi,

Been having a nightmare with this authentication in a desktop application.

Finally decided to implement just the 2nd part of the authentication using
the token only, I thought this would be more straight forward, but
apparently not.

Dim request As HttpWebRequest = WebRequest.Create("
https://api.sandbox.freeagent.com/v2/company")
request.Headers.Add(“Authorization”, "OAuth ")
request.Method = “GET”
request.ContentType = “application/json”

    ' Send Request & Get Response
    Dim response = request.GetResponse()

    ' Get the Response Stream
    Dim reader As StreamReader = New 

StreamReader(response.GetResponseStream())
Dim json As String = reader.ReadToEnd()

    ' Retrieve the Access Token
    Dim ser As JavaScriptSerializer = New JavaScriptSerializer()
    Dim x As Dictionary(Of String, Object) = 

ser.DeserializeObject(json)
Dim accessToken As String = x(“access_token”).ToString()

Should I not just get a json response instead of a bad request or am I
missing something here?

This does work on google playground fine and seems to have the same method
and token etc

Thanks,

Rob