Android Twitter API 返回无效回调 - 无法授权

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11013689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:44:07  来源:igfitidea点击:

Twitter API returns invalid callback - Cannot authorize

androidtwittertwitter-oauthtwitter4j

提问by Bojan Radivojevic Bomber

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

[已解决,但我愿意接受新的建议...]

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

我正在使用 twitter4j 将 Twitter 集成到我的 Android 应用程序中。

When I try to authorize with Twitter, I am calling the following endpoint with my oauth token:

当我尝试使用 Twitter 进行授权时,我使用我的 oauth 令牌调用以下端点:

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

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

which shouldredirect me to:

应该将我重定向到:

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

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

but instead, it redirects me to:

但相反,它将我重定向到:

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

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

这显然不是有效的网址。
(此外,:缺少 - 应该是MY-CALLBACK:///...

请注意,我正在使用 WebView 进行调用,


我可以操作此字符串以使一切正常,但必须有更好的方法......



我将回调 URL 传递给

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

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

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

并且已经为我的活动设置了意图过滤器

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

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

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



What am I doing wrong?


[edit:more details]

此外,该活动有android:launchMode="singleInstance"



我做错了什么?


[编辑:更多细节]

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:

如果没有 hacky 部分,此代码会导致“网页不可用”错误,因为 url 无效:

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

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...

如果网址是MY-CALLBACK:///?oauth_token=***&oauth_verifier=***那么我的活动将收到一个意图,一切都会好起来的......

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

使用“hacky 部分”,我的代码可以工作,但我想避免使用那段代码。

回答by ScouseChris

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 WebViewClientwith the code:

我最终使用我自己的自定义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;

回答by Dipak Keshariya

Use Below CallBack_URI for that, it may help you.

为此使用 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;

回答by Dara Saini

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 code and manifest file...

在代码和清单文件中使用这种类型的 callback_url ...

回答by Andre Pedralho

I guess there is nothing wrong with your code. I was getting the same result yesterday, but today it works like a charm. It is probably a server side issue. Could you try again your original (with no hacky part) solution, pls?

我想你的代码没有问题。我昨天得到了同样的结果,但今天它就像一个魅力。这可能是服务器端的问题。你能再试一次你的原始(没有hacky部分)解决方案吗?