/// <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); }
/// <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); }