Ejemplo n.º 1
0
        /// <summary>
        ///     アクセストークンを更新します。
        /// </summary>
        /// <param name="account">対象のアカウント</param>
        public static async Task<bool> RefreshAccessToken(UserAccountEntity account)
        {
            var dic = new Dictionary<String, String>();
            dic["grant_type"] = "refresh_token";
            dic["client_id"] = Constants.CONSUMER_KEY;
            dic["client_secret"] = Constants.CONSUMER_SECRET;
            dic["refresh_token"] = account.GetRefreshToken();

            account.SetAccessToken("updating", null);
            account.SetRefreshTime(1000);
            var theAuthClient = new HttpClient();
            HttpContent header = new FormUrlEncodedContent(dic);
            HttpResponseMessage response;
            try
            {
                response = await theAuthClient.PostAsync(EndPoints.OAUTH_TOKEN, header);
            }
            catch (WebException)
            {
                return false;
            }
            if (response.StatusCode != HttpStatusCode.OK) return false;
            string responseContent = await response.Content.ReadAsStringAsync();
            JObject o = JObject.Parse(responseContent);
            account.SetAccessToken((String) o["access_token"], (String) o["refresh_token"]);
            account.SetRefreshTime(long.Parse((String) o["expires_in"]));

            CroudiaAuthEntity authEntity = CroudiaAuthEntity.Parse(responseContent);
            AppSettings["refreshToken"] = authEntity.RefreshToken;
            AppSettings["accessToken"] = authEntity.AccessToken;
            AppSettings.Save();
            return true;
        }