예제 #1
0
        public BitportAppToken GetToken()
        {
            BitportAppToken token = new BitportAppToken();

            string code = "";

            while (code.IsNullOrEmpty())
            {
                PlatformService.OpenBrowser(string.Format("https://api.bitport.io/v2/oauth2/authorize?response_type=code&client_id={0}", Settings.AppId));

                var request = PlatformService.ListenOnLocalPort(Settings.LocalhostCallbackPort);

                code = request["code"];
            }

            while (token.IsInvalid())
            {
                using (var client = new HttpClient())
                {
                    UriBuilder builder = new UriBuilder();
                    builder.Scheme = "https";
                    builder.Host   = "api.bitport.io";
                    builder.Path   = "/v2/oauth2/access-token";
                    var body = new Dictionary <string, string>();
                    body["client_id"]     = Settings.AppId;
                    body["grant_type"]    = "authorization_code";
                    body["client_secret"] = Settings.SecretKey;
                    body["code"]          = code;
                    string url = builder.ToString();

                    var resultText = client.PostAsync(url, new FormUrlEncodedContent(body)).Result?.Content?.ReadAsStringAsync().Result;
                    token = JsonConvert.DeserializeObject <BitportAppToken>(resultText);
                }
            }
            return(token);
        }
 public static bool IsValid(this BitportAppToken tokenResponse)
 {
     return((tokenResponse?.access_token).IsNotNullOrEmpty());
 }
 public static bool IsInvalid(this BitportAppToken tokenResponse)
 {
     return(!tokenResponse.IsValid());
 }