Exemplo n.º 1
0
        public async Task <ActionResult> RegisterDropboxAsync(string dbxCode)
        {
            int accountId = JWTUtility.GetIdFromRequestHeaders(Request.Headers);

            if (accountId == -1)
            {
                return(BadRequest());
            }

            using var client = new HttpClient();

            string dbxKeys = _authService.GetDropboxKeys();

            var dict = new Dictionary <string, string>();

            dict.Add("grant_type", "authorization_code");
            dict.Add("code", dbxCode);
            dict.Add("redirect_uri", _config.GetValue <string>("DropboxRedirectURL"));

            client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", dbxKeys);

            var req = new HttpRequestMessage(HttpMethod.Post, "https://api.dropbox.com/1/oauth2/token")
            {
                Content = new FormUrlEncodedContent(dict)
            };

            var response = await client.SendAsync(req);

            if (!response.IsSuccessStatusCode)
            {
                return(BadRequest());
            }

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

            var db = JsonConvert.DeserializeObject <DbxOAuthResponse>(resBody);

            var json = new DropboxJson()
            {
                Cursor    = string.Empty,
                DropboxID = db.Account_id,
                JwtToken  = db.Access_token
            };

            await _authService.RegisterDropboxAsync(accountId, json);

            await _migService.DropboxMigrationAsync(accountId);

            return(Ok());
        }