Пример #1
0
        /// <summary>
        /// Refresh expired access token
        /// </summary>
        /// <param name="token">Access token</param>
        /// <returns>Refreshed access token</returns>
        public static ICloudStorageAccessToken RefreshToken(ICloudStorageAccessToken token)
        {
            var sdToken = token as OAuth20Token;

            if (sdToken == null || !CanRefresh(sdToken))
            {
                throw new ArgumentException("Can not refresh given token", "token");
            }

            var query = string.Format("client_id={0}&client_secret={1}&redirect_uri={2}&grant_type=refresh_token&refresh_token={3}",
                                      sdToken.ClientID, sdToken.ClientSecret, sdToken.RedirectUri, sdToken.RefreshToken);

            var json = SkyDriveRequestHelper.PerformRequest(SkyDriveConstants.OAuth20TokenUrl, "application/x-www-form-urlencoded", "POST", query, 2);

            if (json != null)
            {
                var refreshed = OAuth20Token.FromJson(json);
                refreshed.ClientID     = sdToken.ClientID;
                refreshed.ClientSecret = sdToken.ClientSecret;
                refreshed.RedirectUri  = sdToken.RedirectUri;
                refreshed.Timestamp    = DateTime.UtcNow;
                return(refreshed);
            }

            return(token);
        }
Пример #2
0
        /// <summary>
        /// Exchange your authorization code for a valid access token
        /// </summary>
        /// <param name="clientID">ID of your app</param>
        /// <param name="clientSecret">Secret of your app</param>
        /// <param name="redirectUri">Redirect uri you used to obtained authorization code. Serves as request validator</param>
        /// <param name="authCode">Authorization code</param>
        /// <returns>Access token</returns>
        public static ICloudStorageAccessToken GetAccessToken(String clientID, String clientSecret, String redirectUri, String authCode)
        {
            if (String.IsNullOrEmpty(clientID))
            {
                throw new ArgumentNullException("clientID");
            }
            if (String.IsNullOrEmpty(clientSecret))
            {
                throw new ArgumentNullException("clientSecret");
            }
            if (String.IsNullOrEmpty(authCode))
            {
                throw new ArgumentNullException("authCode");
            }

            if (String.IsNullOrEmpty(redirectUri))
            {
                redirectUri = SkyDriveConstants.DefaultRedirectUri;
            }

            String query = String.Format("client_id={0}&redirect_uri={1}&client_secret={2}&code={3}&grant_type=authorization_code",
                                         clientID, redirectUri, clientSecret, authCode);

            byte[] bytes = Encoding.UTF8.GetBytes(query);

            WebRequest request = WebRequest.Create(SkyDriveConstants.OAuth20TokenUrl);

            request.Method        = "POST";
            request.ContentType   = "application/x-www-form-urlencoded";
            request.ContentLength = bytes.Length;
            using (var rs = request.GetRequestStream())
            {
                rs.Write(bytes, 0, bytes.Length);
            }

            WebResponse response = request.GetResponse();

            using (var rs = response.GetResponseStream())
            {
                if (rs != null)
                {
                    String json  = new StreamReader(rs).ReadToEnd();
                    var    token = OAuth20Token.Deserialize(json);
                    token.ClientID     = clientID;
                    token.ClientSecret = clientSecret;
                    token.RedirectUri  = redirectUri;
                    token.Timestamp    = DateTime.UtcNow;
                    return(token);
                }
            }

            return(null);
        }
Пример #3
0
        /// <summary>
        /// Refresh expired access token
        /// </summary>
        /// <param name="token">Access token</param>
        /// <returns>Refreshed access token</returns>
        public static ICloudStorageAccessToken RefreshToken(ICloudStorageAccessToken token)
        {
            var sdToken = token as OAuth20Token;

            if (sdToken == null || !CanRefresh(sdToken))
            {
                throw new ArgumentException("Can not refresh given token", "token");
            }

            String query = String.Format("client_id={0}&client_secret={1}&redirect_uri={2}&grant_type=refresh_token&refresh_token={3}",
                                         sdToken.ClientID, sdToken.ClientSecret, sdToken.RedirectUri, sdToken.RefreshToken);

            byte[] bytes = Encoding.UTF8.GetBytes(query);

            WebRequest request = WebRequest.Create(SkyDriveConstants.OAuth20TokenUrl);

            request.Method        = "POST";
            request.ContentType   = "application/x-www-form-urlencoded";
            request.ContentLength = bytes.Length;
            using (var rs = request.GetRequestStream())
            {
                rs.Write(bytes, 0, bytes.Length);
            }

            WebResponse response = request.GetResponse();

            using (var rs = response.GetResponseStream())
            {
                if (rs != null)
                {
                    String json      = new StreamReader(rs).ReadToEnd();
                    var    refreshed = OAuth20Token.Deserialize(json);
                    refreshed.ClientID     = sdToken.ClientID;
                    refreshed.ClientSecret = sdToken.ClientSecret;
                    refreshed.RedirectUri  = sdToken.RedirectUri;
                    refreshed.Timestamp    = DateTime.UtcNow;
                    return(refreshed);
                }
            }

            return(null);
        }
Пример #4
0
        /// <summary>
        /// Exchange your authorization code for a valid access token
        /// </summary>
        /// <param name="clientID">ID of your app</param>
        /// <param name="clientSecret">Secret of your app</param>
        /// <param name="redirectUri">Redirect uri you used to obtained authorization code. Serves as request validator</param>
        /// <param name="authCode">Authorization code</param>
        /// <returns>Access token</returns>
        public static ICloudStorageAccessToken GetAccessToken(string clientID, string clientSecret, string redirectUri, string authCode)
        {
            if (string.IsNullOrEmpty(clientID))
            {
                throw new ArgumentNullException("clientID");
            }
            if (string.IsNullOrEmpty(clientSecret))
            {
                throw new ArgumentNullException("clientSecret");
            }
            if (string.IsNullOrEmpty(authCode))
            {
                throw new ArgumentNullException("authCode");
            }

            if (string.IsNullOrEmpty(redirectUri))
            {
                redirectUri = SkyDriveConstants.DefaultRedirectUri;
            }

            var query = string.Format("client_id={0}&redirect_uri={1}&client_secret={2}&code={3}&grant_type=authorization_code",
                                      clientID, redirectUri, clientSecret, authCode);

            var json = SkyDriveRequestHelper.PerformRequest(SkyDriveConstants.OAuth20TokenUrl, "application/x-www-form-urlencoded", "POST", query, 2);

            if (json != null)
            {
                var token = OAuth20Token.FromJson(json);
                token.ClientID     = clientID;
                token.ClientSecret = clientSecret;
                token.RedirectUri  = redirectUri;
                token.Timestamp    = DateTime.UtcNow;
                return(token);
            }

            return(null);
        }
Пример #5
0
 private static bool CanRefresh(OAuth20Token token)
 {
     return(!string.IsNullOrEmpty(token.ClientID) && !string.IsNullOrEmpty(token.ClientSecret) && !string.IsNullOrEmpty(token.RedirectUri));
 }