/// <summary> /// An async method to exhange authorization code for auth tokens with the auth server. /// </summary> public static Task <LiveLoginResult> ExchangeCodeForTokenAsync(string clientId, string clientSecret, string redirectUrl, string authorizationCode) { Debug.Assert(!string.IsNullOrEmpty(clientId)); Debug.Assert(!string.IsNullOrEmpty(redirectUrl)); Debug.Assert(!string.IsNullOrEmpty(authorizationCode)); string postContent = LiveAuthUtility.BuildCodeTokenExchangePostContent(clientId, clientSecret, redirectUrl, authorizationCode); return(RequestAccessTokenAsync(postContent)); }
/// <summary> /// An async method to get auth tokens using refresh token. /// </summary> public static Task <LiveLoginResult> RefreshTokenAsync( string clientId, string clientSecret, string redirectUrl, string refreshToken, IEnumerable <string> scopes) { Debug.Assert(!string.IsNullOrEmpty(clientId)); Debug.Assert(!string.IsNullOrEmpty(redirectUrl)); Debug.Assert(!string.IsNullOrEmpty(refreshToken)); string postContent = LiveAuthUtility.BuildRefreshTokenPostContent(clientId, clientSecret, redirectUrl, refreshToken, scopes); return(RequestAccessTokenAsync(postContent)); }
private static LiveLoginResult RequestAccessToken(string postContent) { string url = LiveAuthUtility.BuildTokenUrl(); HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; HttpWebResponse response = null; LiveLoginResult loginResult = null; request.Method = ApiMethod.Post.ToString().ToUpperInvariant(); request.ContentType = TokenRequestContentType; try { using (StreamWriter writer = new StreamWriter(request.GetRequestStream())) { writer.Write(postContent); } response = request.GetResponse() as HttpWebResponse; loginResult = ReadResponse(response); } catch (WebException e) { response = e.Response as HttpWebResponse; loginResult = ReadResponse(response); } catch (IOException ioe) { loginResult = new LiveLoginResult(new LiveAuthException(AuthErrorCodes.ClientError, ioe.Message)); } finally { if (response != null) { response.Close(); } } if (loginResult == null) { loginResult = new LiveLoginResult(new LiveAuthException(AuthErrorCodes.ClientError, ErrorText.RetrieveTokenError)); } return(loginResult); }
/// <summary> /// Creates a LiveConnectSession object based on the parsed response. /// </summary> private static LiveConnectSession CreateSession(IDictionary <string, object> result) { var session = new LiveConnectSession(); Debug.Assert(result.ContainsKey(AuthConstants.AccessToken)); if (result.ContainsKey(AuthConstants.AccessToken)) { session.AccessToken = result[AuthConstants.AccessToken] as string; if (result.ContainsKey(AuthConstants.AuthenticationToken)) { session.AuthenticationToken = result[AuthConstants.AuthenticationToken] as string; } if (result.ContainsKey(AuthConstants.ExpiresIn)) { if (result[AuthConstants.ExpiresIn] is string) { session.Expires = CalculateExpiration(result[AuthConstants.ExpiresIn] as string); } else { session.Expires = DateTimeOffset.UtcNow.AddSeconds((int)result[AuthConstants.ExpiresIn]); } } if (result.ContainsKey(AuthConstants.Scope)) { session.Scopes = LiveAuthUtility.ParseScopeString(result[AuthConstants.Scope] as string); } if (result.ContainsKey(AuthConstants.RefreshToken)) { session.RefreshToken = result[AuthConstants.RefreshToken] as string; } } return(session); }