private static async Task <OAuthCredentials> GetSpotifyCredentials(string PostBody, string clientId, string clientSecret, string refreshToken = null)
        {
            string             b64     = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{clientId}:{clientSecret}"));
            HttpRequestMessage request = WebRequestHelpers.SetupRequest(SpotifyAPITokenUrl, "Basic", b64, HttpMethod.Post);
            HttpContent        content = new StringContent(PostBody, Encoding.UTF8, "application/x-www-form-urlencoded");

            request.Content = content;
            HttpResponseMessage response = await WebRequestHelpers.Client.SendAsync(request);

            if (response.IsSuccessStatusCode)
            {
                string jsonstring = await response.Content.ReadAsStringAsync();

                JObject jobj        = Newtonsoft.Json.Linq.JObject.Parse(jsonstring);
                string  accessToken = jobj.GetValue("access_token").ToString();
                int     expiresIn   = Convert.ToInt32(jobj.GetValue("expires_in").ToString());
                if (string.IsNullOrWhiteSpace(refreshToken))
                {
                    refreshToken = jobj.GetValue("refresh_token").ToString();
                }
                return(new OAuthCredentials("unknown user", accessToken, expiresIn, refreshToken));
            }
            else
            {
                return(OAuthCredentials.CrendentialError);
            }
        }
Exemple #2
0
        /// <summary>
        /// Returns a new paging object with the previous {limit} number of items
        /// Throws an IndexOutOfRangeException if there are no more items
        /// </summary>
        /// <returns></returns>
        public async Task <Paging <T> > GetPrevious(string accessToken)
        {
            if (string.IsNullOrWhiteSpace(Previous) || Offset <= 0)
            {
                throw new IndexOutOfRangeException($"Offset({Offset}) already at or below 0. There are no more items left to get");
            }
            else   /* There are items left to get, so return a new paging object with that offset*/
            {
                HttpRequestMessage  message  = WebRequestHelpers.SetupRequest(this.Previous, accessToken);
                HttpResponseMessage response = await WebRequestHelpers.Client.SendAsync(message);

                if (response.IsSuccessStatusCode)
                {
                    JToken token = await WebRequestHelpers.ParseJsonResponse(response.Content);

                    Paging <T> paging = new Paging <T>(token);
                    return(paging);
                }
            }
            return(null);
        }
        /// <summary>
        /// Returns a new Cursor-Based Paging object with the next {Limit} number of items,
        /// starting after the {After} fields
        /// Throws an IndexOutOfRangeException if there are no more items
        /// </summary>
        /// <returns></returns>
        public async Task <CursorBasedPaging <T> > GetNext(string accessToken)
        {
            /* Offset is past the total - therefore there's nothing left to get */
            if (string.IsNullOrWhiteSpace(Next))
            {
                throw new IndexOutOfRangeException($"There are no more items left to get");
            }
            else   /* There are items left to get, so return a new paging object with that offset*/
            {
                HttpRequestMessage  message  = WebRequestHelpers.SetupRequest(this.Next, accessToken);
                HttpResponseMessage response = await WebRequestHelpers.Client.SendAsync(message);

                if (response.IsSuccessStatusCode)
                {
                    JToken token = await WebRequestHelpers.ParseJsonResponse(response.Content);

                    CursorBasedPaging <T> paging = new CursorBasedPaging <T>(token);
                    return(paging);
                }
            }
            return(null);
        }