private async Task ExecuteMainAsync() { Console.WriteLine("Access token: "); var accessToken = Console.ReadLine(); Console.WriteLine("Remote file name: "); var fileName = Console.ReadLine(); Console.WriteLine("Local file path: "); var localFilePath = Console.ReadLine(); Console.WriteLine("Parent folder Id: "); var parentFolderId = Console.ReadLine(); var timer = Stopwatch.StartNew(); var auth = new OAuthSession(accessToken, "YOUR_REFRESH_TOKEN", 3600, "bearer"); var config = new BoxConfig("YOUR_CLIENT_ID", "YOUR_CLIENT_ID", new Uri("http://boxsdk")); var client = new BoxClient(config, auth); var file = File.OpenRead(localFilePath); var fileRequest = new BoxFileRequest { Name = fileName, Parent = new BoxFolderRequest { Id = parentFolderId } }; var bFile = await client.FilesManager.UploadAsync(fileRequest, file); Console.WriteLine("{0} uploaded to folder: {1} as file: {2}",localFilePath, parentFolderId, bFile.Id); Console.WriteLine("Time spend : {0} ms", timer.ElapsedMilliseconds); }
public virtual async Task <OAuthSession> AuthenticateBoxDeveloperEditionAsync() { string assertion = JWTConstructAssertion(); BoxRequest boxRequest = new BoxRequest(_config.BoxApiDeveloperEditionTokenUri) .Method(RequestMethod.Post) .Header(Constants.RequestParameters.UserAgent, _config.UserAgent) .Payload(Constants.RequestParameters.GrantType, Constants.RequestParameters.JWT) .Payload(Constants.RequestParameters.Assertion, assertion) .Payload(Constants.RequestParameters.ClientId, _config.ClientId) .Payload(Constants.RequestParameters.ClientSecret, _config.ClientSecret) .Payload(Constants.RequestParameters.BoxDeviceId, _config.DeviceId) .Payload(Constants.RequestParameters.BoxDeviceName, _config.DeviceName); IBoxResponse <OAuthSession> boxResponse = await _service.ToResponseAsync <OAuthSession>(boxRequest).ConfigureAwait(false); boxResponse.ParseResults(_converter); var session = boxResponse.ResponseObject; using (await _mutex.LockAsync().ConfigureAwait(false)) { Session = session; OnSessionAuthenticated(session); } return(session); }
/// <summary> /// Instantiates a new AuthRepository /// </summary> /// <param name="boxConfig">The Box configuration that should be used</param> /// <param name="boxService">The Box service that will be used to make the requests</param> /// <param name="converter">How requests/responses will be serialized/deserialized respectively</param> /// <param name="session">The current authenticated session</param> public AuthRepository(IBoxConfig boxConfig, IBoxService boxService, IBoxConverter converter, OAuthSession session) { _config = boxConfig; _service = boxService; _converter = converter; Session = session; }
public async Task<bool> Claim(Uri uri, string documentTitle) { IDictionary<string, string> keyDictionary = new Dictionary<string, string>(); var qSplit = uri.Query.Split('?'); foreach (var kvp in qSplit[qSplit.Length - 1].Split('&')) { var kvpSplit = kvp.Split('='); if (kvpSplit.Length == 2) { keyDictionary.Add(kvpSplit[0], kvpSplit[1]); } } if (!keyDictionary.ContainsKey("code")) return false; var authCode = keyDictionary["code"]; if (string.IsNullOrEmpty(authCode)) return false; _api = BoxHelper.GetClient(); _token = await _api.Auth.AuthenticateAsync(authCode); return _token != null && _token.RefreshToken != null && _token.AccessToken != null; }
public async Task <OAuthSession> AuthenticateAsync(string authCode) { if (string.IsNullOrWhiteSpace(authCode)) { throw new ArgumentException("Auth code cannot be null or empty", "authCode"); } BoxRequest boxRequest = new BoxRequest(_config.BoxApiHostUri, Constants.AuthTokenEndpointString) .Method(RequestMethod.Post) .Payload(Constants.RequestParameters.GrantType, Constants.RequestParameters.AuthorizationCode) .Payload(Constants.RequestParameters.Code, authCode) .Payload(Constants.RequestParameters.ClientId, _config.ClientId) .Payload(Constants.RequestParameters.ClientSecret, _config.ClientSecret) .Payload(Constants.RequestParameters.BoxDeviceId, _config.DeviceId) .Payload(Constants.RequestParameters.BoxDeviceName, _config.DeviceName); IBoxResponse <OAuthSession> boxResponse = await _service.ToResponseAsync <OAuthSession>(boxRequest).ConfigureAwait(false); boxResponse.ParseResults(_converter); using (await _mutex.LockAsync().ConfigureAwait(false)) { Session = boxResponse.ResponseObject; var handler = SessionAuthenticated; if (handler != null) { handler(this, new SessionAuthenticatedEventArgs(Session)); } } return(boxResponse.ResponseObject); }
public async Task<OAuthSession> AuthenticateAsync(string authCode) { if (string.IsNullOrWhiteSpace(authCode)) throw new ArgumentException("Auth code cannot be null or empty", "authCode"); BoxRequest boxRequest = new BoxRequest(_config.BoxApiHostUri, Constants.AuthTokenEndpointString) .Method(RequestMethod.Post) .Payload(Constants.RequestParameters.GrantType, Constants.RequestParameters.AuthorizationCode) .Payload(Constants.RequestParameters.Code, authCode) .Payload(Constants.RequestParameters.ClientId, _config.ClientId) .Payload(Constants.RequestParameters.ClientSecret, _config.ClientSecret) .Payload(Constants.RequestParameters.BoxDeviceId, _config.DeviceId) .Payload(Constants.RequestParameters.BoxDeviceName, _config.DeviceName); IBoxResponse<OAuthSession> boxResponse = await _service.ToResponseAsync<OAuthSession>(boxRequest).ConfigureAwait(false); boxResponse.ParseResults(_converter); using (await _mutex.LockAsync().ConfigureAwait(false)) { Session = boxResponse.ResponseObject; var handler = SessionAuthenticated; if (handler != null) { handler(this, new SessionAuthenticatedEventArgs(Session)); } } return boxResponse.ResponseObject; }
/// <summary> /// Refreshes the session by exchanging the access token for a new Access/Refresh token pair. In general, /// this method should not need to be called explicitly, as an automatic refresh is invoked when the SDK /// detects that the tokens have expired. /// </summary> /// <param name="accessToken"></param> /// <returns></returns> public virtual async Task <OAuthSession> RefreshAccessTokenAsync(string accessToken) { OAuthSession session; using (await _mutex.LockAsync().ConfigureAwait(false)) { if (_expiredTokens.Contains(accessToken)) { session = Session; } else { // Add the expired token to the list so subsequent calls will get new acces token. Add // token to the list before making the network call. This way, if refresh fails, subsequent calls // with the same refresh token will not attempt te call. _expiredTokens.Add(accessToken); session = await ExchangeRefreshToken(Session.RefreshToken).ConfigureAwait(false); Session = session; OnSessionAuthenticated(session); } } return(session); }
private void OnSessionAuthenticated(OAuthSession session) { var handler = SessionAuthenticated; if (handler != null) { handler(this, new SessionAuthenticatedEventArgs(session)); } }
public BoxResourceManagerTestIntegration() { _auth = new OAuthSession("HrMywfaXCtAStbY9FTvoCbMZCBPeBgud", "Qx34izsfUuvbcm90x88gPs6ZiRXexxN3jbQGqNoY2r9to6mppRBoCD4iqlSVrm0F", 3600, "bearer"); _handler = new HttpRequestHandler(); _parser = new BoxJsonConverter(); _config = new BoxConfig(ClientId, ClientSecret, RedirectUri); _client = new BoxClient(_config, _auth); }
public BoxResourceManagerTestIntegration() { _auth = new OAuthSession("YOUR_ACCESS_TOKEN", "YOUR_REFRESH_TOKEN", 3600, "bearer"); _handler = new HttpRequestHandler(); _parser = new BoxJsonConverter(); _config = new BoxConfig(ClientId, ClientSecret, RedirectUri); _client = new BoxClient(_config, _auth); }
/// <summary> /// Allows sub classes to invoke the SessionAuthenticated event /// </summary> /// <param name="e"></param> protected void OnSessionAuthenticated(OAuthSession session) { var handler = SessionAuthenticated; if (handler != null) { handler(this, new SessionAuthenticatedEventArgs(session)); } }
private static BoxClient CreateClientByToken(string token) { var auth = new OAuthSession(token, "YOUR_REFRESH_TOKEN", 3600, "bearer"); var config = new BoxConfig(string.Empty, string.Empty, new Uri("http://boxsdk")); var client = new BoxClient(config, auth); return client; }
public static BoxClient GetClient(OAuthSession session) { var handler = new BoxHttpRequestHandler(); var converter = new BoxJsonConverter(); var service = new BoxService(handler); var authRepository = new AuthRepository(Config, service, converter, session); var client = new BoxClient(Config, converter, handler, service, authRepository); return client; }
public BoxResourceManagerTestIntegration() { _auth = new OAuthSession("YOUR_ACCESS_TOKEN", "YOUR_REFRESH_TOKEN", 3600, "bearer"); _handler = new HttpRequestHandler(); _parser = new BoxJsonConverter(); _config = new BoxConfig(ClientId, ClientSecret, RedirectUri); _client = new BoxClient(_config, _auth); _boxDeveloperEditionConfig = new BoxConfig(EnterpriseId, "enterprise", ClientId, ClientSecret, PrivateKey, PrivateKeyPassword); _boxDeveloperEditionClient = new BoxClient(_boxDeveloperEditionConfig); }
public BoxServiceTest() { // Initial Setup _converter = new BoxJsonConverter(); _handler = new Mock<IRequestHandler>(); _service = new BoxService(_handler.Object); _boxConfig = new Mock<IBoxConfig>(); OAuthSession session = new OAuthSession("fakeAccessToken", "fakeRefreshToken", 3600, "bearer"); _authRepository = new AuthRepository(_boxConfig.Object, _service, _converter, session); }
/// <summary> /// Instantiates a BoxClient with the provided config object and auth session /// </summary> /// <param name="boxConfig">The config object to be used</param> /// <param name="authSession">A fully authenticated auth session</param> public BoxClient(IBoxConfig boxConfig, OAuthSession authSession) { _config = boxConfig; IRequestHandler handler = new HttpRequestHandler(); _converter = new BoxJsonConverter(); _service = new BoxService(handler); Auth = new AuthRepository(_config, _service, _converter, authSession); InitManagers(); }
/// <summary> /// Authenticates the session by exchanging the provided auth code for a Access/Refresh token pair /// </summary> /// <param name="authCode"></param> /// <returns></returns> public virtual async Task <OAuthSession> AuthenticateAsync(string authCode) { OAuthSession session = await ExchangeAuthCode(authCode); using (await _mutex.LockAsync().ConfigureAwait(false)) { Session = session; OnSessionAuthenticated(session); } return(session); }
/// <summary> /// Authenticates the session by exchanging the provided auth code for a Access/Refresh token pair /// </summary> /// <param name="authCode">Authorization Code. The authorization code is only valid for 30 seconds.</param> /// <returns>The session of the Box Client after authentification</returns> public virtual async Task<OAuthSession> AuthenticateAsync(string authCode) { OAuthSession session = await ExchangeAuthCode(authCode); using (await _mutex.LockAsync().ConfigureAwait(false)) { Session = session; OnSessionAuthenticated(session); } return session; }
public static async Task<BoxClient> GetClient(AccountConfiguration account) { if (Cache.ContainsKey(account.Id)) return Cache[account.Id]; var session = new OAuthSession(null, account.Secret, 0, "bearer"); var client = GetClient(session); client.Auth.SessionAuthenticated += (sender, args) => account.Secret = args.Session.RefreshToken; session = await client.Auth.RefreshAccessTokenAsync(account.Secret); Cache.Add(account.Id, client); return client; }
public async Task<OAuthSession> AuthenticateAsync(string authCode) { if (string.IsNullOrWhiteSpace(authCode)) throw new ArgumentException("Auth code cannot be null or empty", "authCode"); BoxRequest boxRequest = new BoxRequest(_config.BoxApiHostUri, Constants.AuthTokenEndpointString) .Method(RequestMethod.Post) .Payload("grant_type", "authorization_code") .Payload("code", authCode) .Payload("client_id", _config.ClientId) .Payload("client_secret", _config.ClientSecret); IBoxResponse<OAuthSession> boxResponse = await _service.ToResponseAsync<OAuthSession>(boxRequest).ConfigureAwait(false); boxResponse.ParseResults(_converter); using (await _mutex.LockAsync().ConfigureAwait(false)) Session = boxResponse.ResponseObject; return boxResponse.ResponseObject; }
/// <summary> /// Authenticates the session by exchanging the provided auth code for a Access/Refresh token pair /// </summary> /// <param name="authCode">Authorization Code. The authorization code is only valid for 30 seconds.</param> /// <returns>The session of the Box Client after authentification</returns> public virtual async Task <OAuthSession> AuthenticateAsync(string authCode) { OAuthSession session = await ExchangeAuthCode(authCode); await _mutex.WaitAsync().ConfigureAwait(false); // using (await _mutex.LockAsync().ConfigureAwait(false)) try { Session = session; OnSessionAuthenticated(session); } finally { _mutex.Release(); } return(session); }
public async Task<OAuthSession> RefreshAccessTokenAsync(string accessToken) { OAuthSession session; using (await _mutex.LockAsync().ConfigureAwait(false)) { if (_expiredTokens.Contains(accessToken)) session = Session; else { session = await ExchangeRefreshToken(Session.RefreshToken).ConfigureAwait(false); Session = session; // Add the expired token to the list so subsequent calls will get new acces token _expiredTokens.Add(accessToken); } } return session; }
public async Task <OAuthSession> RefreshAccessTokenAsync(string accessToken) { OAuthSession session; using (await _mutex.LockAsync().ConfigureAwait(false)) { if (_expiredTokens.Contains(accessToken)) { session = Session; } else { session = await ExchangeRefreshToken(Session.RefreshToken).ConfigureAwait(false); Session = session; // Add the expired token to the list so subsequent calls will get new acces token _expiredTokens.Add(accessToken); } } return(session); }
public async Task <OAuthSession> AuthenticateAsync(string authCode) { if (string.IsNullOrWhiteSpace(authCode)) { throw new ArgumentException("Auth code cannot be null or empty", "authCode"); } BoxRequest boxRequest = new BoxRequest(_config.BoxApiHostUri, Constants.AuthTokenEndpointString) .Method(RequestMethod.Post) .Payload("grant_type", "authorization_code") .Payload("code", authCode) .Payload("client_id", _config.ClientId) .Payload("client_secret", _config.ClientSecret); IBoxResponse <OAuthSession> boxResponse = await _service.ToResponseAsync <OAuthSession>(boxRequest).ConfigureAwait(false); boxResponse.ParseResults(_converter); using (await _mutex.LockAsync().ConfigureAwait(false)) Session = boxResponse.ResponseObject; return(boxResponse.ResponseObject); }
/// <summary> /// Constructor JWT auth. repository /// </summary> /// <param name="session">OAuth session</param> /// <param name="boxJWTAuth">JWT authentication</param> /// <param name="userId">Id of the user</param> public JWTAuthRepository(OAuthSession session, BoxJWTAuth boxJWTAuth, string userId = null) { this.Session = session; this.BoxJWTAuth = boxJWTAuth; this.UserId = userId; }
public SessionAuthenticatedEventArgs(OAuthSession session) { Session = session; }
/// <summary> /// Allows sub classes to invoke the SessionAuthenticated event. /// </summary> ///<param name="session">Authenticated session.</param> protected void OnSessionAuthenticated(OAuthSession session) { SessionAuthenticated?.Invoke(this, new SessionAuthenticatedEventArgs(session)); }
/// <summary> /// Refreshes the session by exchanging the access token for a new Access/Refresh token pair. In general, /// this method should not need to be called explicitly, as an automatic refresh is invoked when the SDK /// detects that the tokens have expired. /// </summary> /// <param name="accessToken">The access token is what’s needed to sign your API requests to Box</param> /// <returns>Refreshed session of Box Client</returns> public virtual async Task<OAuthSession> RefreshAccessTokenAsync(string accessToken) { OAuthSession session; using (await _mutex.LockAsync().ConfigureAwait(false)) { if (_expiredTokens.Contains(accessToken)) { session = Session; } else { // Add the expired token to the list so subsequent calls will get new acces token. Add // token to the list before making the network call. This way, if refresh fails, subsequent calls // with the same refresh token will not attempt te call. _expiredTokens.Add(accessToken); session = await ExchangeRefreshToken(Session.RefreshToken).ConfigureAwait(false); Session = session; OnSessionAuthenticated(session); } } return session; }
public virtual async Task<OAuthSession> AuthenticateBoxDeveloperEditionAsync() { string assertion = JWTConstructAssertion(); BoxRequest boxRequest = new BoxRequest(_config.BoxApiDeveloperEditionTokenUri) .Method(RequestMethod.Post) .Header(Constants.RequestParameters.UserAgent, _config.UserAgent) .Payload(Constants.RequestParameters.GrantType, Constants.RequestParameters.JWT) .Payload(Constants.RequestParameters.Assertion, assertion) .Payload(Constants.RequestParameters.ClientId, _config.ClientId) .Payload(Constants.RequestParameters.ClientSecret, _config.ClientSecret) .Payload(Constants.RequestParameters.BoxDeviceId, _config.DeviceId) .Payload(Constants.RequestParameters.BoxDeviceName, _config.DeviceName); IBoxResponse<OAuthSession> boxResponse = await _service.ToResponseAsync<OAuthSession>(boxRequest).ConfigureAwait(false); boxResponse.ParseResults(_converter); var session = boxResponse.ResponseObject; using (await _mutex.LockAsync().ConfigureAwait(false)) { Session = session; OnSessionAuthenticated(session); } return session; }