Twitter API 返回无效回调 - 无法授权 [英] Twitter API returns invalid callback - Cannot authorize
问题描述
[已解决,但我愿意接受新建议...]
[SOLVED, but I'm open to new suggestions...]
我正在使用 twitter4j 将 Twitter 集成到我的 Android 应用中.
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
应该将我重定向到:
MY-CALLBACK:///?oauth_token=***&oauth_verifier=***
但相反,它将我重定向到:
but instead, it redirects me to:
https://api.twitter.comMY-CALLBACK///?oauth_token=***&oauth_verifier=***
这显然不是一个有效的网址.
(此外,缺少 :
- 应该是 MY-CALLBACK:///...
)
请注意,我正在使用 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-CALLBACK:///");
并且已经为我的活动设置了意图过滤器
and have already set the intent-filter for my activity with
此外,该活动具有 android: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());
没有hacky部分,这段代码会导致网页不可用"错误,因为网址无效:
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 是 MY-CALLBACK:///?oauth_token=***&oauth_verifier=***
那么我的活动将收到一个 Intent,一切都会好的...
If the url was MY-CALLBACK:///?oauth_token=***&oauth_verifier=***
then my activity would receive an Intent, and everything would be ok...
有了hacky 部分",我的代码可以工作,但我想避免使用那段代码.
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
代码:
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 返回无效回调 - 无法授权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!