redouane59 / twittered

Twitter API client for Java developers
Apache License 2.0
236 stars 64 forks source link

Add ability to explicitly set bearer token in TwitterCredentials #315

Closed PhantomYdn closed 2 years ago

PhantomYdn commented 2 years ago

It's needed to fix #313

redouane59 commented 2 years ago

I think a big part is missing no ? How to instantiate the TwitterClient with only a bearer token and nothing else? And then call the related endpoints.

PhantomYdn commented 2 years ago

In the same way as before: through TwitterCredentials. https://github.com/redouane59/twittered/blob/develop/src/main/java/io/github/redouane59/twitter/TwitterClient.java#L138 Previously BearerToken was explicitly requested with the aid of ApiKey and ApiSecretKey :

https://github.com/redouane59/twittered/blob/develop/src/main/java/io/github/redouane59/twitter/helpers/RequestHelperV2.java#L125-L139

But if client does know BearerToken - it can be explicitly specified in TwitterCredentials

redouane59 commented 2 years ago

I think it's a bit more complex. Example running this test in ITwitterClientV2Test.java :

  @Test
  public void testBearerToken(){
    TwitterClient twitterClient2 = new TwitterClient(
        TwitterCredentials.builder().accessToken(twitterClient.getRequestHelperV2().getBearerToken()).build());
    String userName = "RedouaneBali";
    User   result   = twitterClient2.getUserFromUserName(userName);
    assertEquals(userName, result.getName());
  }

This is throwing a java.lang.IllegalArgumentException: Invalid Api key because of the ServiceBuilder constructor expecting an api key.

PhantomYdn commented 2 years ago

It should be like this:

@Test
  public void testBearerToken(){
    TwitterClient twitterClient2 = new TwitterClient(
        TwitterCredentials.builder().bearerToken("MY BEARER TOKEN").build());
    String userName = "RedouaneBali";
    User   result   = twitterClient2.getUserFromUserName(userName);
    assertEquals(userName, result.getName());
  }
redouane59 commented 2 years ago

That's exactly what I've done, I just got the bearer token from the other client I instantiated but even putting the String value, the exception is thrown. Just try on your side :)

PhantomYdn commented 2 years ago

Just checked with

@Test
  public void testBearerToken(){
    String bearerToken = "MY BEARER TOKEN";
    TwitterClient twitterClient2 = new TwitterClient(
        TwitterCredentials.builder()
            .bearerToken(bearerToken)
            .apiKey("SECRET")
            .apiSecretKey("SECRET")
            .build());
    String userName = "RedouaneBali";
    User   result   = twitterClient2.getUserFromUserName(userName);
    assertEquals(userName, result.getName());
  }

image

API Key and API Secret Key are needed to get Bearer Token - so if we have last one - we can just pass some value to create a service. It's a little bit ugly, but having only BearerToken is also ugly:)

redouane59 commented 2 years ago

API Key and API Secret Key are needed to get Bearer Token - so if we have last one - we can just pass some value to create a service. It's a little bit ugly, but having only BearerToken is also ugly:)

Yes I think it would be awesome to propose a service just with the bearer token. Would it be possible for you to implement it ?

PhantomYdn commented 2 years ago

Done - please check from your side. image

sonarcloud[bot] commented 2 years ago

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

0.0% 0.0% Coverage
0.0% 0.0% Duplication

redouane59 commented 2 years ago

Working well, thank you so much @PhantomYdn !!!