/// <inheritdoc />
 protected override void PrepareHttpRequest(IHttpRequest request)
 {
     if (string.IsNullOrWhiteSpace(AccessToken) == false)
     {
         request.Authorization = "Basic " + SecurityUtils.Base64Encode(AccessToken + ":api_token");
     }
 }
        public void Base64Encode()
        {
            var samples = new[] {
                new { Input = "Hello World", Expected = "SGVsbG8gV29ybGQ=" },
                new { Input = "Rød grød med fløde", Expected = "UsO4ZCBncsO4ZCBtZWQgZmzDuGRl" }
            };

            foreach (var sample in samples)
            {
                Assert.AreEqual(sample.Expected, SecurityHelper.Base64Encode(sample.Input));
                Assert.AreEqual(sample.Expected, SecurityUtils.Base64Encode(sample.Input));
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Exchanges the specified <paramref name="authorizationCode"/> for a refresh token and an access token.
        /// </summary>
        /// <param name="authorizationCode">The authorization code received from the Sonos OAuth dialog.</param>
        /// <returns>An instance of <see cref="SonosTokenResponse"/> representing the response.</returns>
        /// <see>
        ///     <cref>https://developer.sonos.com/reference/authorization-api/create-token/</cref>
        /// </see>
        public SonosTokenResponse GetAccessTokenFromAuthorizationCode(string authorizationCode)
        {
            // Input validation
            if (String.IsNullOrWhiteSpace(authorizationCode))
            {
                throw new ArgumentNullException(nameof(authorizationCode));
            }
            if (String.IsNullOrWhiteSpace(ClientId))
            {
                throw new PropertyNotSetException(nameof(ClientId));
            }
            if (String.IsNullOrWhiteSpace(ClientSecret))
            {
                throw new PropertyNotSetException(nameof(ClientSecret));
            }
            if (String.IsNullOrWhiteSpace(RedirectUri))
            {
                throw new PropertyNotSetException(nameof(RedirectUri));
            }

            // Initialize the POST data
            SocialHttpPostData postData = new SocialHttpPostData {
                { "grant_type", "authorization_code" },
                { "code", authorizationCode },
                { "redirect_uri", RedirectUri }
            };

            // Initialize the request
            SocialHttpRequest request = new SocialHttpRequest {
                Method   = SocialHttpMethod.Post,
                Url      = "https://api.sonos.com/login/v3/oauth/access",
                PostData = postData
            };

            // Update the "Authorization" header
            request.Authorization = "Basic " + SecurityUtils.Base64Encode(ClientId + ":" + ClientSecret);

            // Make a request to the server
            SocialHttpResponse response = request.GetResponse();

            // Parse the JSON response
            return(SonosTokenResponse.ParseResponse(response));
        }
        /// <summary>
        /// Exchanges the specified authorization code for an access token.
        /// </summary>
        /// <param name="authCode">The authorization code received from the Vimeo OAuth dialog.</param>
        /// <returns>An instance of <see cref="VimeoTokenResponse"/> representing the response.</returns>
        public VimeoTokenResponse GetAccessTokenFromAuthCode(string authCode)
        {
            // Some validation
            if (string.IsNullOrWhiteSpace(ClientId))
            {
                throw new PropertyNotSetException(nameof(ClientId));
            }
            if (string.IsNullOrWhiteSpace(ClientSecret))
            {
                throw new PropertyNotSetException(nameof(ClientSecret));
            }
            if (string.IsNullOrWhiteSpace(RedirectUri))
            {
                throw new PropertyNotSetException(nameof(RedirectUri));
            }
            if (string.IsNullOrWhiteSpace(authCode))
            {
                throw new ArgumentNullException(nameof(authCode));
            }

            // Initialize the POST data
            IHttpPostData data = new HttpPostData();

            data.Add("grant_type", "authorization_code");
            data.Add("code", authCode);
            data.Add("redirect_uri", RedirectUri);

            // Initialize the request
            IHttpRequest request = new HttpRequest {
                Method        = HttpMethod.Post,
                Url           = "https://api.vimeo.com/oauth/access_token",
                PostData      = data,
                Authorization = "basic " + SecurityUtils.Base64Encode(ClientId + ":" + ClientSecret)
            };

            // Make the call to the API
            IHttpResponse response = request.GetResponse();

            // Parse the response
            return(new VimeoTokenResponse(response));
        }