Free Support Forum -

Get Authorization code (using c# code) for accessing gmail messages


Can you share a sample application code (in C# class library) that gets the authorization code similar to the one that is got using using url below :

i have build a code that gets the auth code but that comes to be different than when the above url is hit.
sharing my code as below:

private string GetAuthCode()
string code = “”;

        // Creates a redirect URI using an available port on the loopback address.
        string redirectURI = string.Format("http://{0}:{1}/", IPAddress.Loopback, GetRandomUnusedPort());

        // Creates an HttpListener to listen for requests on that redirect URI.
        var http = new HttpListener();

        // Opens request in the browser.
        System.Diagnostics.Process.Start("" + HttpUtility.UrlEncode("") + "&response_type=code&");

        while (string.IsNullOrEmpty(code))
            var context = http.GetContextAsync().Result;

            var response = context.Response;
            var responseOutput = response.OutputStream;
            code = context.Request.QueryString.Get("code");
        return code;

Thanks & Regards,


I suggest you to please visit this documentation link for your kind reference. If there is still an issue then please share feedback with us.