使用 twitter 进行身份验证后,我遇到了回调问题。我在 onNewItem 上获得了令牌,但网页仍然存在。
这是我的代码
public class TwitterRequest extends Activity {
private Twitter niceTwitter;
public RequestToken niceRequestToken;
private Twitter twitter;
private RequestToken requestToken;
public final static String TWIT_KEY = "dsadsdsdsads";
public final static String TWIT_SECRET = "sdsdsdsd";
public final static String TWIT_URL = "callbackapp://tweeter";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
niceTwitter = new TwitterFactory().getInstance();
niceTwitter.setOAuthConsumer(TWIT_KEY, TWIT_SECRET);
niceRequestToken = null;
try {
niceRequestToken = niceTwitter.getOAuthRequestToken(TWIT_URL);
} catch (TwitterException e) {
e.printStackTrace();
Dbg.p("niceRequestToken: " + niceRequestToken);
}
String authURL = niceRequestToken.getAuthenticationURL();
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authURL)));
}
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Uri twitURI = intent.getData();
Intent response = new Intent();
if (twitURI != null && twitURI.toString().startsWith(TWIT_URL)) {
String oaVerifier = twitURI.getQueryParameter("oauth_verifier");
try {
AccessToken accToken = niceTwitter.getOAuthAccessToken(
niceRequestToken, oaVerifier);
String token = accToken.getToken();
response.putExtra("token", token);
} catch (TwitterException te) {
Log.e("tag",
"Failed to get access token: " + te.getMessage());
response.putExtra("token", "error");
}
setResult(RESULT_OK, response);
finish();
}
}
}
打开网页时,我登录 twitter 并收到令牌,但浏览器可见并且 android 不会关闭页面。
这是我的清单
<activity
android:name=".activities.twitter.TwitterRequest"
android:launchMode="singleInstance" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="tweeter"
android:scheme="callbackapp" />
</intent-filter>
</activity>