I need sample C#.net project to basic login
@HEMANT_DAT_13838195 We’re working on this, but it might take some time given the ongoing developments. We’ll let you know once we publish the C# samples.
@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
HttpListener
to listen on a specific URL (e.g.,http://localhost:11400/
). - This URL acts as the
redirect_uri
where 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
HttpListener
waits for the UpStox API to redirect the user to theredirect_uri
after login. - Once redirected, the authorization code is included in the query string of the URL.
- The
-
Extract Authorization Code:
- The application extracts the
code
parameter 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
code
parameter is missing or invalid, an error is displayed, and the process is halted.
- If the
Hope it helps.
Thanks
Hi,
Thank you for the code snippet. Its working for my app token generation.
But the same is not working for sandbox app client id token generation. Please let me know the difference if any.
Regards,
Hemant
Hello,
Please note that the API URL for the sandbox is different from the production. Please change the URL to sandbox API. Refer to Sandbox | Upstox Developer API
Thanks,
Sanjay Jain
https://api-sandbox.upstox.com/v2/login/authorization/dialog
is it correct for the sandbox. I tried by replacing in your sample. but not working.
{“status”:“error”,“errors”:[{“errorCode”:“”,“message”:“Required String parameter ‘ucc’ is not present”,“propertyPath”:“ucc”,“invalidValue”:null,“error_code”:“”,“property_path”:“ucc”,“invalid_value”:null}]}