Chilkat Online Tools

Fetch GMail SMTP OAuth2 Access Token

This tool demonstrates how to get a GMail SMTP OAuth2 access token using three-legged OAuth2 in an ASP.NET application. This is also known as the "authorization code grant flow". This is when your ASP.NET app acts on the behalf of a third-party user, your app must obtain the user's permission before it can make requests that access and update that third-party's confidential resources. A User access token carries a third-party's authorization to access specific resources, and this type of token is obtained through the authorization code grant flow.

Begin OAuth2
Note: Google will display an Unverified App warning. This is because the app is unverified, but it is an app that does nothing. After the warning, click on "Advanced", then click on "go to chilkat.io".

The C# Source Code for this Page

             
@{

    Layout = "~/_LayoutPage1.cshtml";
}


@{
    string access_token = "";
    string state = "";


    // Check to see if this is our redirect containing the access token.
    if (!string.IsNullOrEmpty(Request.Params["state"]))
    {
        state = Request["state"];

        // Make sure this is the redirect for our session.
        if (!state.Equals(Session["oauth2_state"]))
        {
            access_token = "invalid_state";
        }
        else if (!string.IsNullOrEmpty(Request.Params["code"]))
        {
            access_token = Request["code"];
        }
    }
}

@{

    Chilkat.HttpRequest req = new Chilkat.HttpRequest();
    req.AddParam("client_id", "933037264567-bvaackde6evjoov68r4o3sebg8hq82rv.apps.googleusercontent.com");
    req.AddParam("redirect_uri", "https://tools.chilkat.io/gmail_smtp_oauth2.cshtml");
    req.AddParam("include_granted_scopes", "true");
    req.AddParam("access_type", "offline");
    req.AddParam("response_type", "code");
    // SPACE separated list of scopes.
    req.AddParam("scope", "https://mail.google.com/");
    string stateData = "12345678";  // Replace this with random data..
    req.AddParam("state", stateData);
    Session["oauth2_state"] = stateData;

    string auth_url = "https://accounts.google.com/o/oauth2/v2/auth?" + req.GetUrlEncodedParams();

}

<div class="container">
    <h2>Fetch GMail SMTP OAuth2 Access Token</h2>
    <p>
        This tool demonstrates how to get a GMail SMTP OAuth2 access token using
        three-legged OAuth2 in an ASP.NET application.  This is also known as the "authorization code grant flow".
        This is when your ASP.NET app acts on the behalf of a third-party user, your app must obtain the user's permission
        before it can make requests that access and update that third-party's confidential resources. A User access token carries
        a third-party's authorization to access specific resources, and this type of token is obtained through the authorization code grant flow.
    </p>
    <div class="panel panel-default">
        <div class="panel-body">
            <a href="@auth_url" class="btn btn-primary" role="button">Begin OAuth2</a>
            <div><small>Note: Google will display an Unverified App warning.  This is because the app is unverified, but it is an app that does nothing. After the warning, click on "Advanced", then click on "go to chilkat.io".</small></div>

        </div>
    </div>
    <div class="panel panel-default">
        @{
            if (!string.IsNullOrEmpty(access_token))
            {
                @:<p><b></b>GMail SMTP access token:</b> @access_token</p>
            }
            if (!string.IsNullOrEmpty(state))
            {
                @:<p>state: @state</p>
            }
        }
    </div>

    <div class="panel panel-default">
        <h2>The C# Source Code for this Page</h2>
        <pre>
        @{ 
            Chilkat.FileAccess fac = new Chilkat.FileAccess();
            string path = Server.MapPath("~/gmail_smtp_oauth2.cshtml");
            string src = fac.ReadEntireTextFile(path,"utf-8");
            @: @src
        }
        </pre>
    </div>