您好我使用浏览器
完成了Twitter登录代码
TwitLoginActivity.java(主要活动)
package com.app.twitlogin;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.User;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;
import twitter4j.conf.Configuration;
import twitter4j.conf.ConfigurationBuilder;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.StrictMode;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class TwitLoginActivity extends Activity {
static String TWITTER_CONSUMER_KEY;
static String TWITTER_CONSUMER_SECRET;
static String TWITTER_CALLBACK_URL;
static String URL_TWITTER_OAUTH_VERIFIER;
private static Twitter twitter;
Button btnActualLoginTwitter;
private static RequestToken requestToken;
String twitUserID, twitUserName, twitAccessToken, twitTokenSecret, twitProfileImg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_twit_login);
if (android.os.Build.VERSION.SDK_INT > 11) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
}
TWITTER_CONSUMER_KEY = getString(R.string.twitter_consumer_key);
TWITTER_CONSUMER_SECRET = getString(R.string.twitter_consumer_secret);
TWITTER_CALLBACK_URL = getString(R.string.twitter_callback);
URL_TWITTER_OAUTH_VERIFIER = getString(R.string.twitter_oauth_verifier);
btnActualLoginTwitter = (Button) findViewById(R.id.btn_twit_login);
btnActualLoginTwitter.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
btnActualLoginTwitter.setClickable(false);
Runnable buttonClickable = new Runnable() {
@Override
public void run() {
btnActualLoginTwitter.setClickable(true);
}
};
Handler pdCanceller = new Handler();
pdCanceller.postDelayed(buttonClickable, 2000);
Log.e("Came", "Came");
loginToTwitter();
}
});
Uri uri = getIntent().getData();
if (uri != null && uri.toString().startsWith(TWITTER_CALLBACK_URL)) {
String verifier = uri.getQueryParameter(URL_TWITTER_OAUTH_VERIFIER);
try {
AccessToken accessToken = twitter.getOAuthAccessToken(requestToken, verifier);
twitUserID = "" + twitter.getId();
twitUserName = twitter.getScreenName();
twitAccessToken = accessToken.getToken();
twitTokenSecret = accessToken.getTokenSecret();
User user = twitter.showUser(twitter.getId());
twitProfileImg = user.getProfileImageURL();
Log.e("twitUserID", twitUserID);
Log.e("twitUserName", twitUserName);
Log.e("twitAccessToken", twitAccessToken);
Log.e("twitTokenSecret", twitTokenSecret);
Log.e("twitProfileImg", twitProfileImg);
} catch (Exception e) {
Log.e("Twitter Login Error", "" + e.getMessage());
}
}
}
private void loginToTwitter() {
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setOAuthConsumerKey(TWITTER_CONSUMER_KEY);
builder.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET);
Configuration configuration = builder.build();
TwitterFactory factory = new TwitterFactory(configuration);
twitter = factory.getInstance();
Log.e("TwitterLoginObject", twitter + "");
try {
requestToken = twitter.getOAuthRequestToken(TWITTER_CALLBACK_URL);
this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(requestToken.getAuthenticationURL())));
} catch (TwitterException e) {
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.twit_login, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
的strings.xml
<string name="twitter_consumer_key">May Key</string>
<string name="twitter_consumer_secret">My secret</string>
<string name="twitter_callback">oauth://t4jsample</string>
<string name="twitter_oauth_verifier">oauth_verifier</string>
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.twitlogin"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".TwitLoginActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<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="t4jsample"
android:scheme="oauth" />
</intent-filter>
</activity>
</application>
</manifest>
现在我想使用网页浏览而不是浏览器登录(为什么因为有些时候twitter没有重定向到我的应用程序)。对于这个要求,我看到谷歌和SO的几乎所有建议,但我没有实现这一点。所以,请帮我用WebView登录
答案 0 :(得分:1)
Strp 1:
在loginToTwitter()中这样做:
if (!isLoggedIn) {
final ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setOAuthConsumerKey(consumerKey);
builder.setOAuthConsumerSecret(consumerSecret);
final Configuration configuration = builder.build();
final TwitterFactory factory = new TwitterFactory(configuration);
twitter = factory.getInstance();
try {
requestToken = twitter.getOAuthRequestToken(callbackUrl);
final Intent intent = new Intent(getActivity(), WebViewActivity.class);
intent.putExtra(WebViewActivity.EXTRA_URL, requestToken.getAuthenticationURL());
startActivityForResult(intent, WEBVIEW_REQUEST_CODE);
} catch (TwitterException e) {
e.printStackTrace();
}
}
第2步:
通过在WebViewActivity中扩展WebViewClient来创建webviewclient,如here
第3步:
并实施&#39; onActivityResult&#39;在您的主要活动中:
if (requestCode == WEBVIEW_REQUEST_CODE) {
try {
String verifier = data.getExtras().getString(oAuthVerifier);
AccessToken accessToken = twitter.getOAuthAccessToken(requestToken, verifier);
long userID = accessToken.getUserId();
final User user = twitter.showUser(userID);
String username = user.getName();
saveTwitterInfo(accessToken);
String id = mSharedPreferences.getString(WSKeys.TwitterClass.PREF_USER_ID, "");
String name = mSharedPreferences.getString(WSKeys.TwitterClass.PREF_USER_NAME, "");
wscallSocialSignIn("", id, accessToken.toString(), name, image, "Twitter");
} catch (Exception exception) {
}
}