redouane59 / twittered

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

Twitter OAuth Access Token Error: Request token missing #310

Closed weieast1314 closed 2 years ago

weieast1314 commented 2 years ago

I'm trying to run through a proof of concept social sign in flow with Twitter using Postman, following this guide: https://developer.twitter.com/en/docs/twitter-for-websites/log-in-with-twitter/guides/implementing-sign-in-with-twitter

I'm stuck on step 3: Converting the request token to an access token

public RequestToken getOAuth1AccessToken(String oauth_token, String oauth_verifier) {
        TwitterClient twitterClient = generateTwitterClient();

        if (cacheI.hasKey(oauth_token)) {
            RequestToken oauth1Token = new RequestToken();
            oauth1Token.setOauthToken(oauth_token);
            oauth1Token.setOauthTokenSecret(cacheI.get(oauth_token).toString());
            log.info("oauth1Token: {}", JSONUtil.toJsonStr(oauth1Token));
            RequestToken oauth1AccessToken = twitterClient.getOAuth1AccessToken(oauth1Token, oauth_verifier);
            log.info("oauth1AccessToken: {}", JSONUtil.toJsonStr(oauth1AccessToken));
            return oauth1AccessToken;
        }
        return null;
}

and result is:

io.github.redouane59.twitter.helpers.AbstractRequestHelper : (POST) Error calling https://api.twitter.com/oauth/access_token Request token missing - 401
io.github.redouane59.twitter.dto.others.RequestToken : Illegal character in query at index 19: twitter.com?Request token missing

java.net.URISyntaxException: Illegal character in query at index 19: twitter.com?Request token missing
    at java.net.URI$Parser.fail(URI.java:2848)
    at java.net.URI$Parser.checkChars(URI.java:3021)
    at java.net.URI$Parser.parseHierarchical(URI.java:3111)
    at java.net.URI$Parser.parse(URI.java:3063)
    at java.net.URI.<init>(URI.java:588)
    at io.github.redouane59.twitter.dto.others.RequestToken.<init>(RequestToken.java:29)
    at io.github.redouane59.twitter.TwitterClient.getOAuth1AccessToken(TwitterClient.java:1033)
redouane59 commented 2 years ago

Hello,

Did you check this method ?

weieast1314 commented 2 years ago

Hello,

Did you check this method ?

I checked it. It works normally In addition, I found that if the post request of getOAuth1AccessToken is not encrypted, the execution result is normal. I wonder why

TwitterClient twitterClient = generateTwitterClient();
RequestToken oauth1Token = new RequestToken();
oauth1Token.setOauthToken(oauth_token);
oauth1Token.setOauthTokenSecret(cacheI.get(oauth_token).toString());
log.info("oauth1Token: {}", JSONUtil.toJsonStr(oauth1Token));
String url = "https://api.twitter.com/oauth/access_token";
Map<String, String> parameters = new HashMap();
parameters.put("oauth_verifier", oauth_verifier);
parameters.put("oauth_token", oauth1Token.getOauthToken());
String stringResponse = twitterClient.getRequestHelperV1().makeRequest(Verb.POST, url, parameters, null, false, String.class).orElseThrow(NoSuchElementException::new);
redouane59 commented 2 years ago

Hmm honestly I didn't work at all on this use case. I just implemented the method few months ago to test it manually but I could not help you a lot. If you manage to find any solution to hesitate to explain us and submit a pull request if needed to improve the library.

redouane59 commented 2 years ago

Solved by #328