Twitter API returns invalid callback - not allowed

[SOLVED, but I'm open to new suggestions ...]

I integrate Twitter into my Android app using twitter4j.

When I try to log in using Twitter, I call the following endpoint with my oauth token:

https://api.twitter.com/oauth/authenticate?oauth_token=MY_VALID_TOKEN

which should redirect me to:

MY-CALLBACK:///?oauth_token=***&oauth_verifier=***

but instead it redirects me to:

https://api.twitter.comMY-CALLBACK///?oauth_token=***&oauth_verifier=***

which is obviously not a valid URL. (Also missing :- it should be MY-CALLBACK:///...)

Please note that I use WebView for my calls


I could manipulate this line to make everything work, but there should be a better way ...



I pass the callback url

getOAuthRequestToken("MY-CALLBACK:///");

and already set an intent filter for my activity using

<data android:scheme="x-oauthflow-twitter" />

, android:launchMode="singleInstance"



?


[edit: ]

mTwitter = new TwitterFactory().getInstance();
mTwitter.setOAuthConsumer(Constants.TWITTER_CONSUMER_KEY, Constants.TWITTER_CONSUMER_SECRET);

twitterWebView = new WebView(ActivityTwitterAuthorize.this);

twitterWebView.setWebViewClient(new WebViewClient() {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.startsWith(Constants.TWITTER_CALLBACK_URL)) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(url));
            startActivity(intent);

        // HACKY PART!
        // I added the following code to force it to work, but this is a dirty hack...
        // String TWITTER_CALLBACK_INVALID_PREFIX = "https://api.twitter.comx-oauthflow-twitter///";
        // TWITTER_CALLBACK_URL = "MY-CALLBACK:///";
        // BEGIN
        } else if (url.startsWith(TWITTER_CALLBACK_INVALID_PREFIX)) {
            url = url.substring(TWITTER_CALLBACK_INVALID_PREFIX.length());
            url = Constants.TWITTER_CALLBACK_URL + url;
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(url));
            startActivity(intent);
        // END

        } else {
            view.loadUrl(url);
        }
        return true;
    }

});

mTwitterReqToken = mTwitter.getOAuthRequestToken(Constants.TWITTER_CALLBACK_URL);

twitterWebView.loadUrl(mTwitterReqToken.getAuthenticationURL());

, " -", URL- :

https://api.twitter.comMY-CALLBACK///?oauth_token=***&oauth_verifier=***

url MY-CALLBACK:///?oauth_token=***&oauth_verifier=***, Intent, ...

" " , .

0
4

, , , .

WebViewClient :

if ( url.contains( "MY-CALLBACK:///" ) )
{
    final int start = url.indexOf( '?' ) + 1;
    final String params = url.substring( start );
    final String verifierToken = "oauth_verifier=";
    if ( params.contains( verifierToken ) )
    {
        final int value = params.indexOf( verifierToken ) + verifierToken.length();
        final String token = params.substring( value );
        view.stopLoading();                  
        authoriseNewUser( token );
    }
    else if ( params.contains( "denied" ) )
    {
        view.stopLoading();
        finish();
    }
}
else
{
    view.loadUrl( url );
}
return true;
+3

CallBack_URI, .

public static final String  OAUTH_CALLBACK_SCHEME = "x-oauthflow-twitter";
public static final String  OAUTH_CALLBACK_HOST = "callback";
public static final String  CALLBACK_URL = OAUTH_CALLBACK_SCHEME + "://" + OAUTH_CALLBACK_HOST;
0
public static final String OAUTH_CALLBACK_SCHEME = "x-oauthflow-twitter";
public static final String OAUTH_CALLBACK_HOST = "litestcalback";
public static final String OAUTH_CALLBACK_URL = OAUTH_CALLBACK_SCHEME+ "://" +OAUTH_CALLBACK_HOST;

use this type of callback_url in the code and manifest file ...

0
source

I think there is nothing wrong with your code. Yesterday I got the same result, but today it works like a charm. This is probably a server side issue. Could you try your original solution again (without the hacker part), pls?

0
source

Source: https://habr.com/ru/post/1785249/


All Articles