gpt4 book ai didi

java - "cancel"点击推特后页面请求token无效(Twitter4j)

转载 作者:太空狗 更新时间:2023-10-29 15:02:35 25 4
gpt4 key购买 nike

我正在使用 Twitter4j 让我的用户登录到他们的 Twitter 帐户。他们能够成功登录和注销,但是如果他们在 Twitter 弹出对话框中点击“取消”并返回到应用程序然后重新登录,则会出现问题,登录会出错,指出页面的请求 token 是无效的。我知道我有正确的消费者/ secret key ,因为如果我不单击取消并正常登录,则登录有效。有什么想法吗?

image

主 Activity

public class MainActivity extends Activity {

SharedPreferences pref;

private static String CONSUMER_KEY = "mykey";
private static String CONSUMER_SECRET = "mykey";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pref = getPreferences(0);
SharedPreferences.Editor edit = pref.edit();
edit.putString("CONSUMER_KEY", CONSUMER_KEY);
edit.putString("CONSUMER_SECRET", CONSUMER_SECRET);
edit.commit();

Fragment login = new LoginFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, login);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commit();
}

}

登录 fragment

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;

@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);
twitter = new TwitterFactory().getInstance();
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) {
// TODO Auto-generated method stub
new TokenGet().execute();

}
}

private class TokenGet extends AsyncTask<String, String, String> {

@Override
protected String doInBackground(String... args) {

try {
requestToken = twitter.getOAuthRequestToken();
oauth_url = requestToken.getAuthorizationURL()
+ "&force_login=true";
Log.d("URL", oauth_url);
} catch (TwitterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
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();

}
}

}
}

最佳答案

尝试为对话框添加一个 onCancelListener,并在其中将 OAuthAccessToken 设置为 null。

mAuth_dialog.setOnCancelListener(new OnCancelListener() {       
@Override
public void onCancel(DialogInterface dialog) {
sTwitter.setOAuthAccessToken(null);
}
});

关于java - "cancel"点击推特后页面请求token无效(Twitter4j),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25064391/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com