Exemplo n.º 1
0
        private async void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            Text = e.Url.AbsoluteUri;
            var path = e.Url.AbsoluteUri;

            if (path.StartsWith(ConfigAPI.App_redirect))
            {
                if (string.IsNullOrEmpty(ConfigAPI.client_secret))
                {
                    return;
                }

                const string code_str = "?code=";
                var          i        = path.IndexOf(code_str);
                if (i < 0)
                {
                    return;
                }

                string code = path.Substring(i + code_str.Length, path.IndexOf('&', i) - i - code_str.Length);
                await GetAuthorizationCode(code, ct_soruce.Token);

                if (key != null && key.access_token != "")
                {
                    webBrowser1.Navigate(ConfigAPI.LoginSuccess);
                    timer1.Enabled = true;
                }
            }
            if (path.StartsWith(ConfigAPI.App_GetToken))
            {
                try
                {
                    var body = webBrowser1.DocumentText;
                    var i    = body.IndexOf("{");
                    var j    = body.IndexOf("}");
                    if (i < 0 || j < 0)
                    {
                        return;
                    }

                    key = AmazonDrive.ParseAuthResponse(body.Substring(i, j - i));
                    // Save refresh_token
                    server.Refresh_Token = key.refresh_token;
                }
                catch (Exception ex)
                {
                    error_str = ex.ToString();
                    System.Diagnostics.Debug.WriteLine(error_str);
                }
                if (key != null && key.access_token != "")
                {
                    webBrowser1.Navigate(ConfigAPI.LoginSuccess);
                    timer1.Enabled = true;
                }
            }
        }
Exemplo n.º 2
0
        private async Task GetAuthorizationCode(string access_code, CancellationToken ct = default(CancellationToken))
        {
            using (var client = new HttpClient())
            {
                try
                {
                    var response = await client.PostAsync(
                        ConfigAPI.AmazonAPI_token,
                        new FormUrlEncodedContent(new Dictionary <string, string> {
                        { "grant_type", "authorization_code" },
                        { "code", access_code },
                        { "client_id", ConfigAPI.client_id },
                        { "client_secret", ConfigAPI.client_secret },
                        { "redirect_uri", Uri.EscapeUriString(ConfigAPI.App_redirect) },
                    }),
                        ct
                        );

                    response.EnsureSuccessStatusCode();
                    string responseBody = await response.Content.ReadAsStringAsync();

                    // Above three lines can be replaced with new helper method in following line
                    // string body = await client.GetStringAsync(uri);
                    key = AmazonDrive.ParseAuthResponse(responseBody);

                    // Save refresh_token
                    server.Refresh_Token = key.refresh_token;
                }
                catch (HttpRequestException ex)
                {
                    error_str = ex.Message;
                    System.Diagnostics.Debug.WriteLine(error_str);
                }
                catch (Exception ex)
                {
                    error_str = ex.ToString();
                    System.Diagnostics.Debug.WriteLine(error_str);
                }
            }
        }