Stopped at System.Diagnostics.Process.Start - OAuth 2.0 to Google for Windows apps

Viewed 22

I am following below libraries and samples to help me implement the OAuth 2.0 flow described form this Gooolge’s developers recommend codes(https://github.com/googlesamples/oauth-apps-for-windows/blob/master/OAuthDesktopApp/OAuthDesktopApp/MainWindow.xaml.cs), It can runs successfully.

But when I created a new WinForms project to reproduce code, my code was same as the sample : (https://github.com/googlesamples/oauth-apps-for-windows/blob/master/OAuthDesktopApp/OAuthDesktopApp/MainWindow.xaml.cs).

created a new WinForms app

it showed “InnerException: System.Exception {System.ComponentModel.Win32Exception}” and stopped at “Application.Run(new Form1());” when I build this project and clicked the ‘button1’.

[C#]
private async void button1_Click(object sender, EventArgs e)
{

    string state = randomDataBase64url(32);
    string code_verifier = randomDataBase64url(32);
    string code_challenge = base64urlencodeNoPadding(sha256(code_verifier));
    const string code_challenge_method = "S256";

    string redirectURI = string.Format("http://{0}:{1}/", IPAddress.Loopback, GetRandomUnusedPort());
    output("redirect URI: " + redirectURI);

    var http = new HttpListener();
    http.Prefixes.Add(redirectURI);
    output("Listening..");
    http.Start();

    string authorizationRequest = string.Format("{0}?response_type=code&scope=openid%20profile&redirect_uri={1}&client_id={2}&state={3}&code_challenge={4}&code_challenge_method={5}",
        authorizationEndpoint,
        System.Uri.EscapeDataString(redirectURI),
        clientID,
        state,
        code_challenge,
        code_challenge_method);

    System.Diagnostics.Process.Start(authorizationRequest); // <=====   key! stops here

    var context = await http.GetContextAsync();

    this.Activate();

    var response = context.Response;
    string responseString = string.Format("<html><head><meta http-equiv='refresh' content='10;url=https://google.com'></head><body>Please return to the app.</body></html>");
    var buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
    response.ContentLength64 = buffer.Length;
    var responseOutput = response.OutputStream;
    Task responseTask = responseOutput.WriteAsync(buffer, 0, buffer.Length).ContinueWith((task) =>
    {
        responseOutput.Close();
        http.Stop();
        Console.WriteLine("HTTP server stopped.");
    });

    if (context.Request.QueryString.Get("error") != null)
    {
        output(String.Format("OAuth authorization error: {0}.", context.Request.QueryString.Get("error")));
        return;
    }
    if (context.Request.QueryString.Get("code") == null
        || context.Request.QueryString.Get("state") == null)
    {
        output("Malformed authorization response. " + context.Request.QueryString);
        return;
    }

    var code = context.Request.QueryString.Get("code");
    var incoming_state = context.Request.QueryString.Get("state");

    if (incoming_state != state)
    {
        output(String.Format("Received request with invalid state ({0})", incoming_state));
        return;
    }
    output("Authorization code: " + code);

    performCodeExchange(code, code_verifier, redirectURI);
}

InnerException

The error message:

System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation.'
Win32Exception: 系統找不到指定的檔案。

此例外狀況原先在此呼叫堆疊擲回: 
System.Diagnostics.Process.StartWithCreateProcess(System.Diagnostics.ProcessStartInfo)
System.Diagnostics.Process.Start()
System.Diagnostics.Process.Start(System.Diagnostics.ProcessStartInfo)
System.Diagnostics.Process.Start(string)
OAuthDesktopApp_改成winForm_20220921.Form1.button1_Click(object, System.EventArgs) 位於 Form1.cs
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Threading.Tasks.Task.ThrowAsync.AnonymousMethod__139_0(object)

I find comment out this line can pass the program, so it stop here.

...
[C#]
**System.Diagnostics.Process.Start(authorizationRequest);** // <=====   key! stops here

var context = await http.GetContextAsync();
this.Activate();
...

Any suggestion is welcome.

0 Answers
Related