Announcement

Collapse
No announcement yet.

Not able to do Oauth2 authorization (Netatmo) in vb.net

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    #16
    Originally posted by russr999 View Post
    The application/x-www-form-urlencoded standard implies UTF-8 and percent-encoding
    What happens if you drop ;charset=UTF-8 from your content-type?

    Try this:

    Code:
    Dim myUri As Uri = New Uri("https://api.netatmo.com/oauth2/token")
    Dim request As WebRequest = WebRequest.Create(myUri)
    request.Method = "POST"
    request.ContentType = "application/x-www-form-urlencoded"
    Dim data As String = "grant_type=password&username=" & userName & "&password=" & passWord & "&client_secret=" & clientSecret
    Dim dataStream As Byte() = Encoding.UTF8.GetBytes(data)
    request.ContentLength = dataStream.Length
    Dim newStream As Stream = request.GetRequestStream()
    newStream.Write(dataStream, 0, dataStream.Length)
    newStream.Close()
    Try
    Using Response As WebResponse = request.GetResponse()
    Dim reader As New StreamReader(Response.GetResponseStream())
    Dim readerresponse As String = reader.ReadToEnd()
    hs.writelog("Test Call",readerresponse)
    End Using
    Catch wrerror as exception
    hs.writelog("Test Error:", wrerror.message)
    End Try
    I think you need to sepatate the parameters by crlf not by the "&" character. I think you mix the syntax for the re-direct URL (ie a URL) with the post request (body of data composed of lines).

    https://dev.netatmo.com/apidocumentation/oauth

    Comment


      #17
      Originally posted by dcorsus View Post

      I think you need to sepatate the parameters by crlf not by the "&" character. I think you mix the syntax for the re-direct URL (ie a URL) with the post request (body of data composed of lines).

      https://dev.netatmo.com/apidocumentation/oauth
      Hello, thanks for the input, so now I have the following code:

      Code:
          Dim myUri As Uri = New Uri("https://api.netatmo.com/oauth2/token")
          Dim request As WebRequest = WebRequest.Create(myUri)
          request.Method = "POST"
          request.ContentType = "application/x-www-form-urlencoded"
      
          Dim data As String = "grant_type=password"& vbCrLf & "username=" & userName & vbCrLf _
              & "password=" & passWord & vbCrLf & "client_secret=" & clientSecret _
              & vbCrLf & "client_ID=" & clientID
      '    hs.writelog ("Test", data)
          Dim dataStream As Byte() = Encoding.UTF8.GetBytes(data)
          request.ContentLength = dataStream.Length
          Dim newStream As Stream = request.GetRequestStream()
          newStream.Write(dataStream, 0, dataStream.Length)
          newStream.Close()
          Try
          Using Response As WebResponse = request.GetResponse()
              Dim reader As New StreamReader(Response.GetResponseStream())
              Dim readerresponse As String = reader.ReadToEnd()
              hs.writelog("Test Call",readerresponse)
          End Using
          Catch wrerror as exception
              hs.writelog("Test Error:", wrerror.message)
          End Try
      But unfortunately still the error 400 in return...

      My python code:
      Code:
          payload = {'grant_type': 'password',
                     'username': userName,
                     'password': passWord,
                     'client_id':clientID,
                     'client_secret': clientSecret}
      
          try:
              response = requests.post("https://api.netatmo.com/oauth2/token", data=payload)
      Works as a charm, I'm not an experienced programmer, and I do not understand what I'm doing wrong here

      Comment


        #18
        Finally, this one was the correct one:

        Code:
            Dim data As String = "grant_type=password&" & "username=" & userName & "&password=" & passWord & _ 
                "&client_secret=" & clientSecret & "&client_id=" & clientID
        Thank you all for your help, highly appreciated!!

        Comment


          #19
          Do you have a working code that works with Netatmo, that you could share?

          Comment


            #20
            Originally posted by ZoRaC View Post
            Do you have a working code that works with Netatmo, that you could share?
            https://forums.homeseer.com/forum/hs...53#post1624953

            Comment


              #21
              I’m still on HomeSeer 3…

              Comment


                #22
                Originally posted by ZoRaC View Post

                I’m still on HomeSeer 3…

                Comment

                Working...
                X