Ejemplo n.º 1
0
 private async void Form1_Load(object sender, EventArgs e)
 {
     if (string.IsNullOrEmpty(Settings.Default.RefreshToken))
     {
         try
         {
             var authForm = new LiveAuthForm(ClientId);
             if (authForm.ShowDialog(this) == DialogResult.OK)
             {
                 _liveRestClient = new LiveRestClient(ClientId, ClientSecret);
                 await _liveRestClient.InitAsync(authForm.AuthorizeCode);
             }
         }
         catch (Exception ex)
         {
             MessageBox.Show(ex.Message);
         }
     }
     else
     {
         _liveRestClient = new LiveRestClient(ClientId, ClientSecret, Settings.Default.RefreshToken);
         await _liveRestClient.RefreshTokensAsync();
     }
     if (_liveRestClient != null)
     {
         if (Settings.Default.RefreshToken != _liveRestClient.RefreshToken)
         {
             Settings.Default.RefreshToken = _liveRestClient.RefreshToken;
             Settings.Default.Save();
         }
         _oneClient = new OneDriveClient(_liveRestClient, RootFolderId);
     }
 }
Ejemplo n.º 2
0
        private async Task <ResourceResult> SendMessage(HttpRequestMessage message)
        {
            var httpClient = new HttpClient();
            var response   = await httpClient.SendAsync(message);

            if (response.StatusCode == HttpStatusCode.Unauthorized)
            {
                response.Dispose();
                await _liveRestClient.RefreshTokensAsync();

                message.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _liveRestClient.AccessToken);
                response = await httpClient.SendAsync(message);
            }
            response.EnsureSuccessStatusCode();

            var data = await response.Content.ReadAsStringAsync();

            response.Dispose();

            var result = JsonConvert.DeserializeObject <ResourceResult>(data);

            return(result);
        }