示例#1
0
        public async Task RefreshAccessToken(UserAccountEntity account)
        {
            try
            {
                var dic = new Dictionary <String, String>();
                dic["grant_type"]    = "refresh_token";
                dic["client_id"]     = EndPoints.ConsumerKey;
                dic["client_secret"] = EndPoints.ConsumerSecret;
                dic["refresh_token"] = account.GetRefreshToken();
                dic["scope"]         = "psn:sceapp";

                account.SetAccessToken("updating", null);
                account.SetRefreshTime(1000);
                var         theAuthClient = new HttpClient();
                HttpContent header        = new FormUrlEncodedContent(dic);
                var         response      = await theAuthClient.PostAsync(EndPoints.OauthToken, header);

                if (response.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    var responseContent = await response.Content.ReadAsStringAsync();

                    var o = JObject.Parse(responseContent);
                    if (string.IsNullOrEmpty(responseContent))
                    {
                        throw new RefreshTokenException("Could not refresh the user token, no response data");
                    }
                    account.SetAccessToken((String)o["access_token"], (String)o["refresh_token"]);
                    account.SetRefreshTime(long.Parse((String)o["expires_in"]));
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Could not refresh the user token", ex);
            }
        }
        public async Task <bool> RefreshAccessToken(UserAccountEntity account)
        {
            try
            {
                //var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
                var dic = new Dictionary <String, String>();
                dic["grant_type"]    = "refresh_token";
                dic["client_id"]     = ConsumerKey;
                dic["client_secret"] = ConsumerSecret;
                dic["refresh_token"] = account.GetRefreshToken();
                dic["scope"]         = "psn:sceapp";

                account.SetAccessToken("updating", null);
                account.SetRefreshTime(1000);
                var                 theAuthClient = new HttpClient();
                HttpContent         header        = new FormUrlEncodedContent(dic);
                HttpResponseMessage response      = await theAuthClient.PostAsync(OauthToken, header);

                if (response.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    string responseContent = await response.Content.ReadAsStringAsync();

                    JObject o = JObject.Parse(responseContent);
                    if (string.IsNullOrEmpty(responseContent))
                    {
                        return(false);
                    }
                    account.SetAccessToken((String)o["access_token"], (String)o["refresh_token"]);
                    account.SetRefreshTime(long.Parse((String)o["expires_in"]));

                    UserAuthenticationEntity authEntity = UserAuthenticationEntity.Parse(responseContent);
                    _appSettings["refreshToken"] = authEntity.RefreshToken;
                    _appSettings["accessToken"]  = authEntity.AccessToken;
                    _appSettings.Save();
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            catch (Exception)
            {
                return(false);
            }
        }