@@ -122,7 +122,7 @@ public IObservable<AuthenticationResult> LogIn(string usernameOrEmail, string pa
122122 // Try to get an authorization token, save it, then get the user to log in:
123123 . SelectMany ( fingerprint => ApiClient . GetOrCreateApplicationAuthenticationCode ( interceptingTwoFactorChallengeHandler ) )
124124 . SelectMany ( saveAuthorizationToken )
125- . SelectMany ( _ => GetUserFromApi ( ) )
125+ . SelectMany ( _ => GetUserFromApiWithDelay ( ) )
126126 . Catch < UserAndScopes , ApiException > ( firstTryEx =>
127127 {
128128 var exception = firstTryEx as AuthorizationException ;
@@ -163,7 +163,7 @@ public IObservable<AuthenticationResult> LogIn(string usernameOrEmail, string pa
163163 } )
164164 // Then save the authorization token (if there is one) and get the user:
165165 . SelectMany ( saveAuthorizationToken )
166- . SelectMany ( _ => GetUserFromApi ( ) ) ;
166+ . SelectMany ( _ => GetUserFromApiWithDelay ( ) ) ;
167167 }
168168
169169 return Observable . Throw < UserAndScopes > ( firstTryEx ) ;
@@ -178,7 +178,7 @@ public IObservable<AuthenticationResult> LogIn(string usernameOrEmail, string pa
178178 // instead of 404 to signal that it's not allowed. In the name of backwards compatibility we
179179 // test for both 404 (NotFoundException) and 403 (ForbiddenException) here.
180180 if ( isEnterprise && ( retryEx is NotFoundException || retryEx is ForbiddenException || retryEx . StatusCode == ( HttpStatusCode ) 422 ) )
181- return GetUserFromApi ( ) ;
181+ return GetUserFromApiWithDelay ( ) ;
182182
183183 // Other errors are "real" so we pass them along:
184184 return Observable . Throw < UserAndScopes > ( retryEx ) ;
@@ -268,6 +268,15 @@ IObservable<AuthenticationResult> LoginWithApiUser(UserAndScopes userAndScopes)
268268 } ) ;
269269 }
270270
271+ IObservable < UserAndScopes > GetUserFromApiWithDelay ( )
272+ {
273+ // It seems that attempting to use a token immediately sometimes fails, add a delay
274+ // of 1s to allow the token to propagate.
275+ return Observable . Defer ( ( ) =>
276+ Observable . Timer ( TimeSpan . FromMilliseconds ( 1000 ) )
277+ . SelectMany ( _ => ApiClient . GetUser ( ) ) ) ;
278+ }
279+
271280 IObservable < UserAndScopes > GetUserFromApi ( )
272281 {
273282 return Observable . Defer ( ( ) => ApiClient . GetUser ( ) ) ;
0 commit comments