C# Trading example
Hello everyone, we're building a trading platform in C#. We had it all working before the websockets. Now we're rebuilding the entire code for using the websockets. I have a question regarding the login procedure. I already have the authorization part using the websocket in C#. This works okay. But then I'm a bit confused how to get the authentication managed of the user. What data do I have to send to the websocket for the authentication part (trades should be made later on). Does anyone has a C# example? or can point me into the right direction?
/// <summary> /// Autorize this app /// </summary> /// <returns></returns> private bool Autorize() { //Send the autorization string and wait for the correct awnser var dataToSend = string.Format("{{\"authorize\":\"{0}\"}}", _isDemoAccount ? ConfigurationManager.AppSettings[@"BinaryAppIdDemo"] : ConfigurationManager.AppSettings[@"BinaryAppId"]); //Send request SendRequest(dataToSend).Wait(); var response = string.Empty; try { //Validate response response = Listen().Result; var resultObject = JsonConvert.DeserializeObject<AuthorizationResponseRoot>(response); if (resultObject == null) return false; if(resultObject.authorize.scopes.Contains(@"trade")) return true; return false; } catch (Exception ex) { Log.Exception(string.Format(@"Failed to authorize. Message: {0}", response),ex); } return false; }
I use this for authorizing the app. Now I need the code to authorize the user so he/she can do a trade
Also I would like to know how the process of making a trade works. Do I first need to do a request for a Proposal? etc...
Answers
Hello. I really don't understand your full question But I'll try to answer part of it.
"Authorize" is needed to authorize (At the risk of sounding redundant) your current websocket session. You can use a per-app token or token obtained via Oauth. Check Guide
Making a trade can be done in two ways.
resquest for a Proposal and then resquest buy based in the contract id.
Or
resquest buy sending parameters.
Check buy Help.
Regards
Hello tarja,
This is the idea. We're creating an application in C# that on which a user can make trades at Binary.com. We had it working before the whole websocket change. So the application will trade on their account when the login. Can this be done by the websocket or do I need to use Oauth? What I can find on the internet is when I do it with the websocket I only have a per-app authentication, so trades will be done on the account who has registered this app at Binary.com (this is not what we want). Am I correct sofar?
Regards
Hello Alex267.
Binary API always will work through websocket not matter if you use per-app token(This is a token generated in account settings) or a token returned by Oauth2 (this also is per-app token but it's dynamic. It's generated by Oauth2 protocol).
Let's say I have a web binary trader application (It would be a third-party application)
Fisrt I register my app to get my app_id to use in my websocket end-point url and Oauth2
Then your websocket end-point url should be something like this
wss://ws.binaryws.com/websockets/v3?app_id=app_id
Oauth2 Will requiere your app_id too.
https://oauth.binary.com/oauth2/authorize?app_id=app_id
You still can use old websocket end-point url (wss://ws.binaryws.com/websockets/v3) But I'm not sure if it will be deprecated. Maybe some Developer can answer this.
if a user want to use my app I can use Oauth2 to request a token or just use his token generate in settings(If user doesn't have token generated in settings I must use Oauth2 to request a valid token).
About "trades will be done on the account who has registered this app at Binary.com"
No. App registration is just for third-party application. not every user that will use the application.
(All I wrote above is based in my understanding. I cannot make sure that is strictly correct.)
Regards
Hello tarja,
Sorry I'm a bit late but I was on a holiday. I've managed to authorize my app and also the login of the user so this is all covered. Now I've one question left. When a user makes a trade, do I need to send a price proposal first or can I make the trade directly?
Regards
Hello. There is two way to buy sending proposal or just buy sending buy parameters. Check API playground details.
Regards
Hello tarja,
Thank you for your help.
Regards
Hello Alex267
I'm a bit confused how to get the authentication managed of the user too. Actually my application (Java and WebSocket) use the client authentication by app-specific API token. That works well.
Now I would like to change the authentication managed of my application to authentication by OAuth. The password and username is stored in a database and the application should use this information to login as user. Is this possible by using only WebSocket? How have you solved your problem?
Or maybe I'm on the wrong way? Can anyone point me into the right direction?
Regards
Client authentication by OAuth -
To use OAuth, get your app_id (you can register your app here https://developers.binary.com/applications/), here you need to provide
Redirect URL:
i.e url where your client will be redirected after successful login.To authenticate your users, navigate them to Binary.com OAuth URL:
https://oauth.binary.com/oauth2/authorize?app_id=[..insert app_id..]
Our system will authenticate the user, and redirect him to the redirect URI specified during app registration, with a valid token returned in the URL in the token parameter.
With a token obtained via OAuth, you may call
authorize
.You can read how oauth works in general