问题
[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.
回答1:
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;
回答2:
Use Below CallBack_URI for that, it may help you.
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;
回答3:
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?
回答4:
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...
来源:https://stackoverflow.com/questions/11013689/twitter-api-returns-invalid-callback-cannot-authorize