Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
469 views
in Technique[技术] by (71.8m points)

android - Twitter API returns invalid callback - Cannot authorize

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

I'm integrating Twitter into my Android app using twitter4j.

When I try to authorize with Twitter, I am calling 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, the : is missing - it should be MY-CALLBACK:///...)

Please note I'm using WebView for my calls


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



I am passing my callback URL to

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

and have already set the intent-filter for my activity with

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

Also, the activity has android:launchMode="singleInstance"



What am I doing wrong?


[edit:more details]

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());

WITHOUT the hacky part, this code results in "Webpage not available" error, because the url is invalid:

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

If the url was MY-CALLBACK:///?oauth_token=***&oauth_verifier=*** then my activity would receive an Intent, and everything would be ok...

WITH the "hacky part", my code works, but I would like to avoid that piece of code.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I found I just could not get it to work this way after following the guides I've seen online.

I ended up using my own custom WebViewClient with the code:

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...