Android片段中的Twitter登录/身份验证 [英] Twitter Login/Authentication in Android Fragment
问题描述
我正在尝试使用Fabric在Android的一个片段中实现Twitter登录按钮.我让它可以在一个活动中工作,但无法使其零碎地工作.
I am trying to implement a twitter login button in a fragment in Android using Fabric. I got it to work in an activity, but cannot get it working in a fragment.
这是我的TwitterFragment类(扩展了片段)
Here is my TwitterFragment class (extends fragment)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
TwitterAuthConfig authConfig = new TwitterAuthConfig(mTWITTER_KEY, mTWITTER_SECRET);
Fabric.with(super.getActivity(), new Twitter(authConfig));
View view = inflater.inflate(R.layout.twitter_fragment, container, false);
loginButton = (TwitterLoginButton) view.findViewById(R.id.twitter_login_button);
loginButton.setCallback(new Callback<TwitterSession>() {
@Override
public void success(Result<TwitterSession> result) {
TwitterSession session = result.data;
String msg = "@" + session.getUserName() + " logged in! (#" + session.getUserId() + ")";
Toast.makeText(getActivity().getApplicationContext(), msg, Toast.LENGTH_LONG).show();
}
@Override
public void failure(TwitterException exception) {
Log.d("TwitterKit", "Login with Twitter failure", exception);
}
});
return view;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//loginButton.onActivityResult(requestCode, resultCode, data);
Fragment fragment = getFragmentManager().findFragmentById(R.id.twitter_login_button);
if (fragment != null) {
fragment.onActivityResult(requestCode, resultCode, data);
}
}
还有我的twitter_fragment.xml
And my twitter_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp">
<com.twitter.sdk.android.core.identity.TwitterLoginButton
android:id="@+id/twitter_login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
</LinearLayout>
我在监视器的前2行中得到了
I am getting this in the first 2 lines of the monitor
Authorization completed with an error
com.twitter.sdk.android.core.TwitterAuthException: Authorize failed.
有什么想法吗?
谢谢
推荐答案
我遇到了同样的问题,并以此方式解决了该问题:
I had the same issue and solved it in this way:
- 在MainActivity中,配置Twitter并添加onActivityResult函数:
TwitterAuthConfig authConfig =新的TwitterAuthConfig(TWITTER_KEY,TWITTER_SECRET);Fabric.with(this,new Twitter(authConfig));
TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET); Fabric.with(this, new Twitter(authConfig));
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
FragmentManager fragment = getSupportFragmentManager();
if (fragment != null) {
fragment.findFragmentByTag("LoginFragment").onActivityResult(requestCode, resultCode, data);
}
else Log.d("Twitter", "fragment is null");
}
-
在LoginFragment中,让您的Twitter按钮的onActivityResult
in your LoginFragment let your twitter button's onActivityResult
@Override
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
twitterlogin.onActivityResult(requestCode, resultCode, data);
}
希望这对您很有帮助的问题,如果不是您的话,那么会对其他人有所帮助.
Hope this helps, if not you, then some others with this frustrating issue.
这篇关于Android片段中的Twitter登录/身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!