Forum Discussion
Hi All,
I've been working though this for several days and I've built a test app (i've built this using example code from the community) using the WebView2 package and still no luck.
Even when I try using Edge I still get an error message as below:
Other times I get and IP Address error message.
I'm trying this on my Dev and other device with the same results, the best I've had so far is edge in that it gave me back a code.
I have tried my test app and I've only ever got back a response with the code in it and off course it had an error.
Below is the code for the form as I can't add the project files....
Imports Microsoft.Web.WebView2.Core
Imports System.IO
Public Class frmWebView
Private Async Sub frmWebView_Load(sender As Object, e As EventArgs) Handles MyBase.Load
txtURL.Text = "https://www.google.com.au"
LogMsg($"MS Edge Version: {CoreWebView2Environment.GetAvailableBrowserVersionString()}")
' Initialise.
Await wvwMain.EnsureCoreWebView2Async()
'Await InitializeCoreWebView2Async(Path.Combine("C:\Temp", Reflection.Assembly.GetExecutingAssembly().GetName().Name))
If Not String.IsNullOrEmpty(txtURL.Text) Then
' Navigate to URL.
WebsiteNavigate(txtURL.Text)
End If
End Sub
Private Sub frmWebView_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
' Unsubscribe from CoreWebView2 event(s) (remove event handlers).
RemoveHandler wvwMain.CoreWebView2.HistoryChanged, AddressOf wvwMain_HistoryChanged
End Sub
Private Sub txtURL_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtURL.KeyPress
'// Press ENTER
If Asc(e.KeyChar) = 13 Then
'// No beep
e.Handled = True
Try
'wvwMain.CoreWebView2.Navigate(txtURL.Text)
'// Or --> wvwMain.CoreWebView2.Navigate(txtURL.Text)
wvwMain.Source = New Uri(txtURL.Text)
Catch ex As UriFormatException
MessageBox.Show("The URL must be fully styled, such as the prefix. http[s]://")
Catch ex As Exception
MessageBox.Show($"Error: {ex}")
End Try
End If
End Sub
Private Sub wvwMain_CoreWebView2InitializationCompleted(sender As Object, e As CoreWebView2InitializationCompletedEventArgs) Handles wvwMain.CoreWebView2InitializationCompleted
LogMsg("wvwMain_CoreWebView2InitializationCompleted")
LogMsg($"UserDataFolder: {wvwMain.CoreWebView2.Environment.UserDataFolder.ToString()}")
' Subscribe to CoreWebView2 event(s) (add event handlers).
AddHandler wvwMain.CoreWebView2.HistoryChanged, AddressOf wvwMain_HistoryChanged
End Sub
Private Sub wvwMain_NavigationCompleted(sender As Object, e As CoreWebView2NavigationCompletedEventArgs) Handles wvwMain.NavigationCompleted
MessageBox.Show(wvwMain.Source.ToString())
LogMsg("wvwMain_NavigationCompleted")
End Sub
Private Sub wvwMain_WebMessageReceived(sender As Object, e As CoreWebView2WebMessageReceivedEventArgs) Handles wvwMain.WebMessageReceived
LogMsg("wvwMain_WebMessageReceived")
End Sub
Private Async Function InitializeCoreWebView2Async(Optional userDataFolder As String = "") As Task
Dim options As CoreWebView2EnvironmentOptions = Nothing
Dim cwv2Environment As CoreWebView2Environment = Nothing
' It's recommended to create the userDataFolder in the same location
' that your other application data is stored (ie: in a folder in %APPDATA%)
' if not specified, we'll create a folder in %TEMP%
If String.IsNullOrEmpty(userDataFolder) Then
userDataFolder = Path.Combine(Path.GetTempPath(), Reflection.Assembly.GetExecutingAssembly().GetName().Name)
End If
' Create WebView2 Environment using the installed or specified WebView2 Runtime version.
'cwv2Environment = Await CoreWebView2Environment.CreateAsync("C:\Program Files (x86)\Microsoft\Edge Dev\Application\1.0.1054.31", userDataFolder, options)
cwv2Environment = Await CoreWebView2Environment.CreateAsync(Nothing, userDataFolder, options)
' Initialise.
Await wvwMain.EnsureCoreWebView2Async(cwv2Environment)
End Function
Private Sub wvwMain_HistoryChanged(sender As Object, e As Object)
LogMsg("wvwMain_HistoryChanged")
'btnBack.Enabled = WebView21.CoreWebView2.CanGoBack
'btnForward.Enabled = WebView21.CoreWebView2.CanGoForward
' Update the Address Bar.
UpdateAddressBar()
End Sub
Private Sub LogMsg(msg As String, Optional addTimestamp As Boolean = True)
If addTimestamp Then
msg = $"{Now.ToString("yyyy/MM/dd HH:mm:ss.fff")} - {msg}"
End If
Debug.WriteLine(msg)
End Sub
Private Sub UpdateAddressBar()
'' If necessary, update address bar.
'If textBoxAddressBar.Text <> wvwMain.Source.ToString() Then
' textBoxAddressBar.Text = wvwMain.Source.ToString()
' 'move cursor to end of text
' textBoxAddressBar.SelectionStart = textBoxAddressBar.Text.Length
' textBoxAddressBar.SelectionLength = 0
'End If
End Sub
Private Sub WebsiteNavigate(ByVal dest As String)
If wvwMain IsNot Nothing AndAlso Not String.IsNullOrWhiteSpace(dest) Then
' URL must start with one of the specified strings
' if Not, pre-pend with "https://"
If Not dest = "about:blank" AndAlso Not dest.StartsWith("edge://") AndAlso
Not dest.StartsWith("file://") AndAlso Not dest.StartsWith("http://") AndAlso
Not dest.StartsWith("https://") AndAlso
Not System.Text.RegularExpressions.Regex.IsMatch(dest, "^([A-Z]|[a-z]):") Then
' Set Value
dest = "https://" & dest
End If
' Option 1.
'wvwMain.Source = New Uri(dest, UriKind.Absolute)
' Option 2.
wvwMain.CoreWebView2.Navigate(dest)
' Update the Address Bar.
UpdateAddressBar()
End If
End Sub
End Class
Please let me know what I am doing wrong here.
- SteveP13 months agoContributing User
Hi DevRBSystems
You seem to be on the right track, but I don't see where you parse "code=..." from the "https://desktop?code=..." URL. It looks like you are treating it like a "normal" URL and opening it in the browser.
The "desktop" URL is what MYOB call the "Redirect URL", which is used to get back to your application after going through the MYOB login screens. When you get the "desktop" URL, instead of browsing to it (which gives the error you posted), you parse the URL to get the "code" value, which is used in subsequent communication with MYOB.
Your screenshot shows the "code" in the redirect URL, Phil_Jeffrey_KC posted an answer that might help you here.
See also this question Getting OAuth code when Redirect is http://desktop
- 3 months ago
Hi All,
Thanks for all the support and help with this, I've finally got it working.
I've moved the code from my test harness into our live app and our customers are finally syning again.
This hasn't been a pleasant experience with MYOB on this.
Cheers
A.
Looking for something else?
Search the Community Forum for answers or find your topic and get the conversation started!
Find technical support and help for all MYOB products in our online help centre
Dig into MYOB Academy for free courses, learning paths and live events to help build your business with MYOB.