public async virtual Task<ActionResult> IndexAsync(AuthorizationCodeResponseUrl authorizationCode, CancellationToken taskCancellationToken) { if (string.IsNullOrEmpty(authorizationCode.Code)) { var errorResponse = new TokenErrorResponse(authorizationCode); Logger.Info("Received an error. The response is: {0}", errorResponse); return OnTokenError(errorResponse); } Logger.Debug("Received \"{0}\" code", authorizationCode.Code); var returnUrl = Request.Url.ToString(); returnUrl = returnUrl.Substring(0, returnUrl.IndexOf("?")); var token = await Flow.ExchangeCodeForTokenAsync(UserId, authorizationCode.Code, returnUrl, taskCancellationToken).ConfigureAwait(false); // Extract the right state. var oauthState = await AuthWebUtility.ExtracRedirectFromState(Flow.DataStore, UserId, authorizationCode.State).ConfigureAwait(false); return new RedirectResult(oauthState); }
public async override Task<ActionResult> IndexAsync(AuthorizationCodeResponseUrl authorizationCode, CancellationToken taskCancellationToken) { if (string.IsNullOrEmpty(authorizationCode.Code)) { var errorResponse = new TokenErrorResponse(authorizationCode); Logger.Info("Received an error. The response is: {0}", errorResponse); return OnTokenError(errorResponse); } Logger.Debug("Received \"{0}\" code", authorizationCode.Code); var returnUrl = Request.Url.ToString(); returnUrl = returnUrl.Substring(0, returnUrl.IndexOf("?")); var token = await Flow.ExchangeCodeForTokenAsync(UserId, authorizationCode.Code, returnUrl, taskCancellationToken).ConfigureAwait(false); // Extract the right state. var oauthState = await AuthWebUtility.ExtracRedirectFromState(Flow.DataStore, UserId, authorizationCode.State).ConfigureAwait(false); string json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(token); System.IO.File.WriteAllText("D://json.txt", json); return RedirectToAction("Index", "Home"); //TODO: Move to Home/Connect //ToDO: move to Home/Upload //TODO: Load from Disk }
public async Task<UserCredential> AuthorizeAsync(string userId, CancellationToken taskCancellationToken) { // Try to load a token from the data store. var token = await Flow.LoadTokenAsync(userId, taskCancellationToken).ConfigureAwait(false); // Check if a new authorization code is needed. if (ShouldRequestAuthorizationCode(token)) { // Create an authorization code request. var redirectUri = CodeReceiver.RedirectUri; AuthorizationCodeRequestUrl codeRequest = Flow.CreateAuthorizationCodeRequest(redirectUri); // Receive the code. var response = await CodeReceiver.ReceiveCodeAsync(codeRequest, taskCancellationToken) .ConfigureAwait(false); if (string.IsNullOrEmpty(response.Code)) { var errorResponse = new TokenErrorResponse(response); Logger.Info("Received an error. The response is: {0}", errorResponse); throw new TokenResponseException(errorResponse); } Logger.Debug("Received \"{0}\" code", response.Code); // Get the token based on the code. token = await Flow.ExchangeCodeForTokenAsync(userId, response.Code, CodeReceiver.RedirectUri, taskCancellationToken).ConfigureAwait(false); } return new UserCredential(flow, userId, token); }
public async Task<UserCredential> AuthorizeAsync(string userId, CancellationToken taskCancellationToken) { // Try to load a token from the data store. var token = await Flow.LoadTokenAsync(userId, taskCancellationToken).ConfigureAwait(false); // If the stored token is null or it doesn't have a refresh token and the access token is expired we need // to retrieve a new authorization code. if (token == null || (token.RefreshToken == null && token.IsExpired(flow.Clock))) { // Create an authorization code request. var redirectUri = CodeReceiver.RedirectUri; AuthorizationCodeRequestUrl codeRequest = Flow.CreateAuthorizationCodeRequest(redirectUri); // Receive the code. var response = await CodeReceiver.ReceiveCodeAsync(codeRequest, taskCancellationToken) .ConfigureAwait(false); if (string.IsNullOrEmpty(response.Code)) { var errorResponse = new TokenErrorResponse(response); Logger.Info("Received an error. The response is: {0}", errorResponse); throw new TokenResponseException(errorResponse); } Logger.Debug("Received \"{0}\" code", response.Code); // Get the token based on the code. token = await Flow.ExchangeCodeForTokenAsync(userId, response.Code, CodeReceiver.RedirectUri, taskCancellationToken).ConfigureAwait(false); } return new UserCredential(flow, userId, token); }
/// <summary> /// A callback which gets the error when this controller didn't receive an authorization code. The default /// implementation throws a <see cref="Google.Apis.Auth.OAuth2.Responses.TokenResponseException"/>. /// </summary> protected virtual ActionResult OnTokenError(TokenErrorResponse errorResponse) { throw new TokenResponseException(errorResponse); }
/// <summary>Constructs a new token response exception from the given error.</summary> public TokenResponseException(TokenErrorResponse error) : base(error.ToString()) { Error = error; }
/// <summary>Constructs a new token response exception from the given error nad optional HTTP status code.</summary> public TokenResponseException(TokenErrorResponse error, HttpStatusCode?statusCode) : base(error.ToString()) { Error = error; StatusCode = statusCode; }
/// <summary>Constructs a new token response exception from the given error.</summary> public TokenResponseException(TokenErrorResponse error) : this(error, null) { }