コード例 #1
0
        public async Task <Result> GetDataAsync(Uri uri, UserAuthenticationTokens userAuthenticationEntity, string language = "ja")
        {
            using (var httpClient = new HttpClient())
            {
                try
                {
                    var    authenticationManager = new AuthManager();
                    Result result = new Result(false, "");
                    if (RefreshTime(userAuthenticationEntity.ExpiresInDate))
                    {
                        var tokens = await authenticationManager.RefreshTokensAsync(userAuthenticationEntity.RefreshToken);

                        userAuthenticationEntity = JsonConvert.DeserializeObject <UserAuthenticationTokens>(tokens.ResultJson);
                        result.Tokens            = tokens.ResultJson;
                    }
                    httpClient.DefaultRequestHeaders.Add("Origin", "http://psapp.dl.playstation.net");
                    httpClient.DefaultRequestHeaders.Add("Accept-Language", language);
                    httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", userAuthenticationEntity.AccessToken);
                    var response = await httpClient.GetAsync(uri);

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

                    result.IsSuccess  = response.IsSuccessStatusCode;
                    result.ResultJson = responseContent;
                    return(result);
                }
                catch (Exception ex)
                {
                    return(ErrorHandler.CreateErrorObject(new Result(), ex.Message, "GetDataAsync"));
                }
            }
        }
コード例 #2
0
        public async Task <IActionResult> RefreshTokens(string uuid, [FromBody] UserAuthenticationTokens tokens, CancellationToken ct = default)
        {
            UserAuthenticationTokens newTokens = await _tokenService.RefreshTokensForUserAsync(uuid, tokens.RefreshToken, ct);

            if (newTokens == null)
            {
                return(Unauthorized("Invalid refresh token"));
            }
            return(Ok(newTokens));
        }
コード例 #3
0
ファイル: UserManager.cs プロジェクト: drasticactions/PsnLib
 /// <summary>
 /// Gets the avatar of the user requested.
 /// </summary>
 /// <param name="userName">The requested username.</param>
 /// <param name="userAuthenticationTokens">The authentication tokens of the current user.</param>
 /// <param name="region">The region of the user, set to JA by default.</param>
 /// <param name="language">The language of the user, set to JA by default.</param>
 /// <returns>A results object set to type 'User'</returns>
 public async Task <Result> GetUserAvatarAsync(string userName, UserAuthenticationTokens userAuthenticationTokens,
                                               string region = "jp", string language = "ja")
 {
     try
     {
         var url = string.Format(EndPoints.UserAvatars, region, userName);
         return(await _webManager.GetDataAsync(new Uri(url), userAuthenticationTokens, language));
     }
     catch (Exception exception)
     {
         throw new Exception("Error getting user avatars", exception);
     }
 }
コード例 #4
0
ファイル: UserManager.cs プロジェクト: drasticactions/PsnLib
 /// <summary>
 /// Gets complete information about a user. You can customise the response by setting the customFields string to
 /// return only specific fields if you don't require them all.
 /// </summary>
 /// <param name="userName">The requested username.</param>
 /// <param name="userAuthenticationTokens">The authentication tokens of the current user.</param>
 /// <param name="customFields">Custom, comma seperated, parameters you wish to search for. If empty, a default set will be used.</param>
 /// <param name="region">The region of the user, set to JA by default.</param>
 /// <param name="language">The language of the user, set to JA by default.</param>
 /// <returns>A results object set to type 'User'</returns>
 public async Task <Result> GetUserAsync(string userName, UserAuthenticationTokens userAuthenticationTokens, string customFields = "",
                                         string region = "jp", string language = "ja")
 {
     try
     {
         var url = string.IsNullOrEmpty(customFields) ? string.Format(EndPoints.UserDefault, region, userName) : string.Format(EndPoints.User, region, userName, customFields);
         return(await _webManager.GetDataAsync(new Uri(url), userAuthenticationTokens, language));
     }
     catch (Exception exception)
     {
         throw new Exception("Error getting user", exception);
     }
 }
コード例 #5
0
        public async Task <Result> PostDataAsync(Uri uri, StringContent content, UserAuthenticationTokens userAuthenticationEntity, string language = "ja", bool isMessage = false)
        {
            var handler = new HttpClientHandler
            {
                AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
            };

            using (var httpClient = new HttpClient(handler))
            {
                try
                {
                    var    authenticationManager = new AuthManager();
                    Result result = new Result(false, "");
                    if (RefreshTime(userAuthenticationEntity.ExpiresInDate))
                    {
                        var tokens = await authenticationManager.RefreshTokensAsync(userAuthenticationEntity.RefreshToken);

                        userAuthenticationEntity = JsonConvert.DeserializeObject <UserAuthenticationTokens>(tokens.ResultJson);
                        result.Tokens            = tokens.ResultJson;
                    }
                    if (isMessage)
                    {
                        httpClient.DefaultRequestHeaders.Add("User-Agent", "PlayStation Messages App/3.20.23");
                    }
                    httpClient.DefaultRequestHeaders.Add("Accept-Language", language);
                    httpClient.DefaultRequestHeaders.Add("Origin", "http://psapp.dl.playstation.net");
                    httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", userAuthenticationEntity.AccessToken);
                    var response = await httpClient.PostAsync(uri, content);

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

                    result.IsSuccess  = response.IsSuccessStatusCode;
                    result.ResultJson = responseContent;
                    return(result);
                }
                catch (Exception ex)
                {
                    return(ErrorHandler.CreateErrorObject(new Result(), ex.Message, "PostDataAsync"));
                }
            }
        }