我尝试过使用twitter4j让用户在twitter上发帖,但遗憾的是我不断收到401错误。我已经三次检查了我的凭据,并要求提供新密钥,但这没有结果。这是我的代码:
public class LoginFragment extends Fragment {
ImageView login;
Twitter twitter;
RequestToken requestToken = null;
AccessToken accessToken;
String oauth_url,oauth_verifier,profile_url;
Dialog auth_dialog;
WebView web;
SharedPreferences pref;
ProgressDialog progress;
Bitmap bitmap;
String TAG = LoginFragment.class.getSimpleName();
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.login_fragment, container, false);
login = (ImageView)view.findViewById(R.id.login);
pref = getActivity().getPreferences(0);
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setDebugEnabled(true);
builder.setOAuthConsumerKey(pref.getString("CONSUMER_KEY", ""));
builder.setOAuthConsumerSecret(pref.getString("CONSUMER_SECRET",""));
builder.setOAuthAccessToken(null);
builder.setOAuthAccessTokenSecret(null);
TwitterFactory factory = new TwitterFactory(builder.build());
twitter = factory.getInstance();
// twitter = new TwitterFactory().getInstance();
// Toast.makeText(getActivity(),"Key is " + pref.getString("CONSUMER_KEY","") + "Secret is " + pref.getString("CONSUMER_SECRET",""),Toast.LENGTH_LONG).show();
// twitter.setOAuthConsumer(pref.getString("CONSUMER_KEY", ""), pref.getString("CONSUMER_SECRET", ""));
login.setOnClickListener(new LoginProcess());
return view;
}
private class LoginProcess implements OnClickListener {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(),"Pressed",Toast.LENGTH_LONG).show();
// TODO Auto-generated method stub
new TokenGet().execute();
}}
private class TokenGet extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... args) {
try {
//Toast.makeText(getActivity(),"Do in Background",Toast.LENGTH_LONG).show();
// requestToken = twitter.getOAuthRequestToken();
// Log.d(TAG, "Sheldon the RequestToken is " + requestToken);
// oauth_url = requestToken.getAuthorizationURL();
requestToken = twitter.getOAuthRequestToken("http://tweetie.com");
Log.d(TAG, "Sheldon the RequestToken is " + requestToken);
oauth_url = requestToken.getAuthorizationURL();
//End
} catch (TwitterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d(TAG,"Sheldon the OAUTH URL is " + oauth_url);
return oauth_url;
}
@Override
protected void onPostExecute(String oauth_url) {
if(oauth_url != null){
Log.e("URL", oauth_url);
auth_dialog = new Dialog(getActivity());
auth_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
auth_dialog.setContentView(R.layout.auth_dialog);
web = (WebView)auth_dialog.findViewById(R.id.webv);
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl(oauth_url);
web.setWebViewClient(new WebViewClient() {
boolean authComplete = false;
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon){
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (url.contains("oauth_verifier") && authComplete == false){
authComplete = true;
Log.e("Url",url);
Uri uri = Uri.parse(url);
oauth_verifier = uri.getQueryParameter("oauth_verifier");
auth_dialog.dismiss();
new AccessTokenGet().execute();
}else if(url.contains("denied")){
auth_dialog.dismiss();
Toast.makeText(getActivity(), "Sorry !, Permission Denied", Toast.LENGTH_SHORT).show();
}
}
});
auth_dialog.show();
auth_dialog.setCancelable(true);
}else{
Toast.makeText(getActivity(), "Sorry !, Network Error or Invalid Credentials", Toast.LENGTH_SHORT).show();
}
}
}
private class AccessTokenGet extends AsyncTask<String, String, Boolean> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progress = new ProgressDialog(getActivity());
progress.setMessage("Fetching Data ...");
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.setIndeterminate(true);
progress.show();
}
@Override
protected Boolean doInBackground(String... args) {
try {
accessToken = twitter.getOAuthAccessToken(requestToken, oauth_verifier);
SharedPreferences.Editor edit = pref.edit();
edit.putString("ACCESS_TOKEN", accessToken.getToken());
edit.putString("ACCESS_TOKEN_SECRET", accessToken.getTokenSecret());
User user = twitter.showUser(accessToken.getUserId());
profile_url = user.getOriginalProfileImageURL();
edit.putString("NAME", user.getName());
edit.putString("IMAGE_URL", user.getOriginalProfileImageURL());
edit.commit();
} catch (TwitterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
@Override
protected void onPostExecute(Boolean response) {
if(response){
progress.hide();
Fragment profile = new ProfileFragment();
FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, profile);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
}
}
}
}
答案 0 :(得分:0)
在Twitter设置开发帐户中填写“回拨网址”字段和“组织网址”。 但是不要启用回调锁定, 这对我来说就像一块宝石!