OAuth2Service extends OAuthService { public void requestGuestAuthToken(final Callback<GuestAuthToken> callback) { final Callback<OAuth2Token> appAuthCallback = new Callback<OAuth2Token>() { @Override public void success(Result<OAuth2Token> result) { final OAuth2Token appAuthToken = result.data; final Callback<GuestTokenResponse> guestTokenCallback = new Callback<GuestTokenResponse>() { @Override public void success(Result<GuestTokenResponse> result) { final GuestAuthToken guestAuthToken = new GuestAuthToken( appAuthToken.getTokenType(), appAuthToken.getAccessToken(), result.data.guestToken); callback.success(new Result<>(guestAuthToken, null)); } @Override public void failure(TwitterException error) { Twitter.getLogger().e(TwitterCore.TAG, "Your app may not allow guest auth. Please talk to us " + "regarding upgrading your consumer key.", error); callback.failure(error); } }; requestGuestToken(guestTokenCallback, appAuthToken); } @Override public void failure(TwitterException error) { Twitter.getLogger().e(TwitterCore.TAG, "Failed to get app auth token", error); if (callback != null) { callback.failure(error); } } }; requestAppAuthToken(appAuthCallback); } OAuth2Service(TwitterCore twitterCore, TwitterApi api); void requestGuestAuthToken(final Callback<GuestAuthToken> callback);  }
@Test public void testRequestGuestAuthToken_guestAuthSuccess() { service.api = new MockOAuth2Api(); service.requestGuestAuthToken(new Callback<GuestAuthToken>() { @Override public void success(Result<GuestAuthToken> result) { assertEquals(GUEST_TOKEN, result.data); } @Override public void failure(TwitterException error) { fail(); } }); }
@Test public void testRequestGuestAuthToken_guestFailure() { service.api = new MockOAuth2Api() { @Override public Call<GuestTokenResponse> getGuestToken(@Header(OAuthConstants.HEADER_AUTHORIZATION) String auth) { return Calls.failure(new IOException()); } }; service.requestGuestAuthToken(new Callback<GuestAuthToken>() { @Override public void success(Result<GuestAuthToken> result) { fail(); } @Override public void failure(TwitterException error) { assertNotNull(error); } }); }
@Test public void testRequestGuestAuthToken_appAuthFailure() { service.api = new MockOAuth2Api() { @Override public Call<OAuth2Token> getAppAuthToken(@Header(OAuthConstants.HEADER_AUTHORIZATION) String auth, @Field(OAuthConstants.PARAM_GRANT_TYPE) String grantType) { return Calls.failure(new IOException()); } }; service.requestGuestAuthToken(new Callback<GuestAuthToken>() { @Override public void success(Result<GuestAuthToken> result) { fail(); } @Override public void failure(TwitterException error) { assertNotNull(error); } }); }