private async Task ReAuth(CommandContext ctx) { ctx.LogInfo($"GoogleAssistant: Attempting to refresh access token."); using (IAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer { ClientSecrets = new ClientSecrets { ClientId = KarvisConfiguration.GoogleAssistantConfiguration.ClientId, ClientSecret = KarvisConfiguration.GoogleAssistantConfiguration.ClientSecret }, Scopes = new[] { "https://www.googleapis.com/auth/assistant-sdk-prototype" } })) { var tokenResponse = await flow.RefreshTokenAsync( KarvisConfiguration.GoogleAssistantConfiguration.DebugUser, new GoogleOAuth(KarvisConfiguration).GetRefreshTokenForUser(ctx.User.Id), new CancellationToken()); new GoogleOAuth(KarvisConfiguration).StoreCredentialsForUser( KarvisConfiguration.GoogleAssistantConfiguration.DebugUser, tokenResponse.AccessToken, tokenResponse.RefreshToken, ctx.User.Id); var channelCredentials = ChannelCredentials.Create(new SslCredentials(), GoogleGrpcCredentials.FromAccessToken(tokenResponse.AccessToken)); UserEmbeddedAssistantClients[ctx.User.Id] = new EmbeddedAssistant.EmbeddedAssistantClient( new Channel("embeddedassistant.googleapis.com", 443, channelCredentials)); } }
/// <summary> /// This class get the information of the user credentials for DRIVE /// </summary> /// <returns></returns> private static UserCredential getUserCredential() { //el objeto que contiene el client_secret.json clsClientSecret secret = new clsClientSecret(); var flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer { ClientSecrets = new ClientSecrets { ClientId = secret.client_id, ClientSecret = secret.client_secret }, Scopes = new[] { DriveService.Scope.Drive, DriveService.Scope.DriveFile } }); //refresa el access token cada vez que necesite acceso access_token = flow.RefreshTokenAsync(secret.client_id, refresh_token, CancellationToken.None).Result.RefreshToken; //crea el credential con access token y refresh token de cada usuario var credential = new UserCredential(flow, UserIdentifier, new TokenResponse { AccessToken = access_token, RefreshToken = refresh_token }); return(credential); }
public GmailServiceResponse <Token> RefreshToken(string refreshToken) { var result = new GmailServiceResponse <Token>(); try { var credential = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer { ClientSecrets = new ClientSecrets { ClientId = ClientId, ClientSecret = ClientSecret, }, Scopes = new[] { Google.Apis.Gmail.v1.GmailService.Scope.GmailModify } }); var task = credential.RefreshTokenAsync("", refreshToken, CancellationToken.None); var tokenResponse = task.Result; var token = new Token() { AccessToken = tokenResponse.AccessToken, ExpiresInSeconds = tokenResponse.ExpiresInSeconds, IdToken = tokenResponse.IdToken, Issued = tokenResponse.Issued, IssuedUtc = tokenResponse.IssuedUtc, RefreshToken = tokenResponse.RefreshToken, Scope = tokenResponse.Scope, TokenType = tokenResponse.TokenType, }; result.Result = token; } catch (Exception ex) { result.Error = ex.Message; } return(result); }