本文介绍了Twitter的API返回无效的回调 - 不能授权的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[解决了,但我乐于接受新的建议...]

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

我用twitter4j整合微博进入我的Andr​​oid应用程序。

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

当我尝试授权与Twitter,我打电话跟我的OAuth令牌以下端点:

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

其中重定向我:

我的回调:/// oauth_token = ***&放大器; oauth_verifier = ***

但相反,它重定向我:

HTTPS://api.twitter.comMY-CALLBACK/// oauth_token = ***&放大器; oauth_verifier = ***

这显然不是一个有效的URL。
(另外,缺失 - 它应该是我的回调:/// ...

请注意,我用的WebView我的电话


我可以操控这个字符串,使一切工作,但必须有一个更好的办法...



我通过我的回调URL

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回调:///);

和已经设置了意向性过滤器对我的活动与

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

<数据机器人:计划=X-oauthflow-微博/>

此外,该活动有安卓launchMode =singleInstance



我究竟做错了什么?


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

没有哈克的一部分,在这个code结果网页不可用错误,因为该网址是无效的:

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 = ***

如果该URL是我的回调:/// 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...

与哈克的一部分,我的code工作,但我想,以避免那块code。

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

推荐答案

我发现我只是无法得到它的下面是我见过的导游网上后,这种方式工作。

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

最后我用我自己的自定义 WebViewClient 与code:

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;

这篇关于Twitter的API返回无效的回调 - 不能授权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 22:32