Exemplo n.º 1
0
        private async Task <TokenValidResponse> Poll(DeviceCodeResponse deviceCodeResponse)
        {
            var dict = new Dictionary <string, string>();

            // TODO: Don't hardcode client_id
            dict.Add("client_id", "72d0443b-ff34-4568-8eb9-1d81849c5462");
            dict.Add("grant_type", "urn:ietf:params:oauth:grant-type:device_code");
            dict.Add("device_code", deviceCodeResponse.DeviceCode);

            while (true)
            {
                // TODO: Don't hardcode URI
                var req = new HttpRequestMessage(HttpMethod.Post,
                                                 "https://login.microsoftonline.com/73a99466-ad05-4221-9f90-e7142aa2f6c1/oauth2/v2.0/token")
                {
                    Content = new FormUrlEncodedContent(dict)
                };
                req.Headers.Add("Origin", "localhost");

                var resp = await _httpClient.SendAsync(req);

                var respPayload = await resp.Content.ReadAsStringAsync();

                if (resp.IsSuccessStatusCode)
                {
                    return(JsonSerializer.Deserialize <TokenValidResponse>(respPayload));
                }

                var error = JsonSerializer.Deserialize <TokenErrorResponse>(respPayload);
                if (error.Error.Equals("authorization_pending"))
                {
                    //Console.WriteLine($"Sleeping {deviceCodeResponse.Interval}");
                }
                else
                {
                    throw new Exception(error.ErrorDescription);
                }

                Thread.Sleep(deviceCodeResponse.Interval * 1000);                 // Wait the requested interval before polling again.
            }
        }