Announcement

Collapse
No announcement yet.

Help! Can anyone help me convert python script to VB.net?

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

  • Help! Can anyone help me convert python script to VB.net?

    I have a python script that (successfully) returns data which includes an authorization token from BMW (that I will subsequently use to query my car's data)
    I would like to convert it to vb.net so that I can write the data into Homeseer HS3. I've tried the best I can but I'm getting: (401) Unauthorized

    Can anyone help me get this running?

    The python (with login details removed) is:

    import requests
    import urllib.parse

    AUTH_API = 'https://customer.bmwgroup.com/gcdm/oauth/authenticate'

    class ConnectedDrive(object):

    def __init__(self):

    headers = {
    "Content-Type" : "application/x-www-form-urlencoded"
    }

    values = {
    'username' : 'myemailaddress',
    'password' : 'mypassword',
    'client_id' : 'theclientid',
    'redirect_uri' : 'https://www.bmw-connecteddrive.com/app/static/external-dispatch.html',
    'response_type' : 'token'
    }

    data = urllib.parse.urlencode(values)
    r = requests.post(AUTH_API, data=data, headers=headers,allow_redirects=False)

    print(r.headers['Location'])


    def main():
    print("Running bmw-token.py")
    c = ConnectedDrive()

    if __name__ == '__main__':
    main()
    Attached Files
    Last edited by Jez; September 23rd, 2019, 09:07 AM. Reason: This is the python script which would need to be renamed to .py

  • #2
    Originally posted by Jez View Post
    I have a python script that (successfully) returns data which includes an authorization token from BMW (that I will subsequently use to query my car's data)
    I would like to convert it to vb.net so that I can write the data into Homeseer HS3. I've tried the best I can but I'm getting: (401) Unauthorized

    Can anyone help me get this running?

    The python (with login details removed) is:

    import requests
    import urllib.parse

    AUTH_API = 'https://customer.bmwgroup.com/gcdm/oauth/authenticate'

    class ConnectedDrive(object):

    def __init__(self):

    headers = {
    "Content-Type" : "application/x-www-form-urlencoded"
    }

    values = {
    'username' : 'myemailaddress',
    'password' : 'mypassword',
    'client_id' : 'theclientid',
    'redirect_uri' : 'https://www.bmw-connecteddrive.com/app/static/external-dispatch.html',
    'response_type' : 'token'
    }

    data = urllib.parse.urlencode(values)
    r = requests.post(AUTH_API, data=data, headers=headers,allow_redirects=False)

    print(r.headers['Location'])


    def main():
    print("Running bmw-token.py")
    c = ConnectedDrive()

    if __name__ == '__main__':
    main()
    My VB.net attempt is:

    Imports System.Diagnostics
    Imports Newtonsoft.Json
    Imports Newtonsoft.Json.Linq
    Imports System.Data.OleDb
    Imports System.IO
    Imports System.Net

    Sub Main(parm as object)


    Dim json As String = "{"
    json = json & chr(34) & "username" & chr(34) & ":" & chr(34) & "myemailaddress" & chr(34) & ","
    json = json & chr(34) & "password" & chr(34) & ":" & chr(34) & "mypassword" & chr(34) & ","
    json = json & chr(34) & "client_id" & chr(34) & ":" & chr(34) & "theclientid" & chr(34) & ","
    json = json & chr(34) & "redirect_uri" & chr(34) & ":" & chr(34) & "https://www.bmw-connecteddrive.com/app/static/external-dispatch.html" & chr(34) & ","
    json = json & chr(34) & "response_type" & chr(34) & ":" & chr(34) & "token" & chr(34)
    json = json & "}"


    hs.writelog("BMW",json)


    Dim strURL as String = "https://customer.bmwgroup.com/gcdm/oauth/authenticate"
    Dim myWebReq as HttpWebRequest
    Dim myWebResp as HttpWebResponse
    Dim encoding as New System.Text.UTF8Encoding
    Dim sr as StreamReader

    System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12


    Try
    Dim data As Byte() = encoding.GetBytes(json)
    myWebReq = DirectCast(WebRequest.Create(strURL), HttpWebRequest)
    myWebReq.ContentType = "application/x-www-form-urlencoded"
    myWebReq.ContentLength = data.Length
    myWebReq.Method = "POST"
    Dim myStream As Stream = myWebReq.GetRequestStream()

    myStream.Write(data, 0, data.Length)
    myStream.Close()

    myWebResp = DirectCast(myWebReq.GetResponse(), HttpWebResponse)
    sr = New StreamReader(myWebResp.GetResponseStream())
    Dim jsonText As String = sr.ReadToEnd()

    hs.WriteLog("BMW", "Response: " & jsonText)

    Catch ex As Exception : hs.writelog("BMW", "Error: " & ex.Message.ToString)
    End Try

    End Sub

    Comment


    • #3
      And what happens with your vb.net script? What errors or output do you get in your HS log? You have quite a few Imports at the top, and most are not being used, so could start with removing the unnecessary ones.

      PS The Advanced forum post editor allows you to wrap CODE tags around your code which typically makes it easier to read, but more importantly, it ensures the text stays as it without the forum inserting extra spaces, etc.
      HS 3.0.0.548: 1990 Devices 1172 Events
      Z-Wave 3.0.1.262: 126 Nodes on one Z-Net

      Comment


      • #4
        Originally posted by sparkman View Post
        And what happens with your vb.net script? What errors or output do you get in your HS log? You have quite a few Imports at the top, and most are not being used, so could start with removing the unnecessary ones.

        PS The Advanced forum post editor allows you to wrap CODE tags around your code which typically makes it easier to read, but more importantly, it ensures the text stays as it without the forum inserting extra spaces, etc.
        Thanks for the prompt reply. I've removed the unnecessary imports - same problem.
        The error I get in the log is:
        Error: The remote server returned an error: (401) Unauthorized.

        Comment


        • #5
          Originally posted by Jez View Post

          Thanks for the prompt reply. I will try removing the imports.
          The error I get in the log is:
          Error: The remote server returned an error: (401) Unauthorized.
          That's a good sign that the script is good from a syntax perspective. Now you just need to figure out the authorization part. Although I have zero python skills, it looks like the python script is using some kind of token response mechanism, but the vb.net syntax you are using is not set up for that. I haven't done any web site authorization like that, but google "WebRequest.Create", "token" and "vb.net" and you should find some examples. Here's one that popped up: https://stackoverflow.com/questions/...b-net/25965072
          HS 3.0.0.548: 1990 Devices 1172 Events
          Z-Wave 3.0.1.262: 126 Nodes on one Z-Net

          Comment


          • #6
            Originally posted by sparkman View Post

            That's a good sign that the script is good from a syntax perspective. Now you just need to figure out the authorization part. Although I have zero python skills, it looks like the python script is using some kind of token response mechanism, but the vb.net syntax you are using is not set up for that. I haven't done any web site authorization like that, but google "WebRequest.Create", "token" and "vb.net" and you should find some examples. Here's one that popped up: https://stackoverflow.com/questions/...b-net/25965072
            I'm sure you're right. I suspect the syntax for sending the username, password etc isn't quite right. I'm googling but can't find anything helpful yet.....

            Comment

            Working...
            X