@HEMANT_DAT_13838195
Hello,
Here is a sample code snippet for logging into UpStox using a C# application (utilizing the UpStox SDK). An explanation of the code is provided below.
string url = "http://localhost:11400/";
var listener = new HttpListener();
listener.Prefixes.Add(url);
listener.Start();
btnContinue.Enabled = false;
UpStoxClient.Api.LoginApi api = new UpStoxClient.Api.LoginApi();
// Open login page in default browser
ProcessStartInfo psi = new ProcessStartInfo
{
FileName = $"https://api.upstox.com/v2/login/authorization/dialog?client_id={UrlEncoder.Default.Encode(Program.UpStoxAPIKey)}&redirect_uri=" + UrlEncoder.Default.Encode(url),
UseShellExecute = true
};
Process.Start(psi);
var context = await listener.GetContextAsync();
string response = "UpStox Login successful!";
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(response);
context.Response.OutputStream.Write(buffer, 0, buffer.Length);
context.Response.OutputStream.Close();
string query = context.Request.Url.Query;
listener.Stop();
// Parse the query string
var queryParams = HttpUtility.ParseQueryString(query);
if (queryParams.HasKeys())
{
// Get the value of "apisession"
string code = queryParams["code"];
if (!string.IsNullOrEmpty(code))
{
TokenResponse tokenResp = api.Token(code, Program.UpStoxAPIKey, Program.UpStoxAPISecret, url, "authorization_code");
Program.UpStoxToken = tokenResp.AccessToken;
Configuration.Default.AccessToken = tokenResp.AccessToken;
this.Focus();
}
else
{
btnContinue.Enabled = true;
MessageBoxAdv.Show("UpStox login failed.", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
-
Setup Local HTTP Listener:
- A local HTTP server is started using
HttpListenerto listen on a specific URL (e.g.,http://localhost:11400/). - This URL acts as the
redirect_uriwhere UpStox will send the callback after successful login.
- A local HTTP server is started using
-
Redirect URL in UpStox API:
- Yes, you need to register the same
redirect_uri(e.g.,http://localhost:11400/) in your UpStox API configuration to ensure UpStox redirects to this URL after user authentication.
- Yes, you need to register the same
-
Open Login Page:
- The application constructs the UpStox login URL with necessary parameters like:
client_id(API key from UpStox).redirect_uri(the registered URL where UpStox will send the callback).
- Opens this URL in the default browser, prompting the user to log in.
- The application constructs the UpStox login URL with necessary parameters like:
-
Wait for Callback:
- The
HttpListenerwaits for the UpStox API to redirect the user to theredirect_uriafter login. - Once redirected, the authorization code is included in the query string of the URL.
- The
-
Extract Authorization Code:
- The application extracts the
codeparameter from the callback URL query string, which serves as the authorization code.
- The application extracts the
-
Exchange Code for Access Token:
- The authorization code is sent to UpStox’s API along with:
client_id(API key).client_secret. (API Secret)redirect_uri. (The registered redirect URL)
- The API responds with an access token if the code is valid.
- The authorization code is sent to UpStox’s API along with:
-
Store the Access Token:
- The access token is saved for use in subsequent API requests to UpStox, enabling authenticated operations.
-
Error Handling:
- If the
codeparameter is missing or invalid, an error is displayed, and the process is halted.
- If the
Hope it helps.
Thanks