2015-11-13 94 views
4

我想在使用Fabric的Android中的片段中實現twitter登錄按鈕。我在一個活動中使用它,但無法使它在一個片段中工作。Twitter Android登錄/驗證片段

這裏是我TwitterFragment類(擴展片段)

@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

<?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行顯示器的

Authorization completed with an error 
com.twitter.sdk.android.core.TwitterAuthException: Authorize failed. 

任何想法是什麼問題?

謝謝

回答

4

我有同樣的問題,並解決它以這樣的方式

    在MainActivity
  1. ,配置Twitter和添加onActivityResult功能:

TwitterAuthConfig authConfig =新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

    @覆蓋

    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
          super.onActivityResult(requestCode, resultCode, data); 
          twitterlogin.onActivityResult(requestCode, resultCode, data); 
        } 
    
  • 希望這會有所幫助,如果不是你的話這個令人沮喪的問題的其他人。

    +1

    良好的工作老兄......它正在 –

    +0

    如何從Twitter獲取電子郵件.. plz幫助我 –

    +0

    @ExceptionLover使用此代碼從Twitter .TwitterAuthClient authClient =新TwitterAuthClient()獲取電子郵件; authClient.requestEmail(會話,新的回調(){ 覆蓋 公共無效成功(結果結果){// 與做的結果是,它提供了電子郵件地址 } 覆蓋 公共無效失敗中學習(TwitterException例外){ //在失敗時做點事 } }); –

    0
    1. 在主要活動中添加twitter身份驗證和祕密。

    private static final String TWITTER_KEY =「Your Key」;
    private static final String TWITTER_SECRET =「Your Secret」;

    1. 在主活動的onCreate()方法中添加Twitter身份驗證配置。

    TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY,TWITTER_SECRET); Fabric.with(this,new Twitter(authConfig));

    1. 在主Activity中添加onActivityResult()方法。

      @Override protected void onActivityResult(int requestCode,int resultCode,Intent data){ super。onActivityResult(requestCode,resultCode,data);

      List<Fragment> allFragments = getSupportFragmentManager().getFragments(); 
      for (Fragment fragmento : allFragments) { 
          if (fragmento instanceof TwitterSignIn) { 
           ((TwitterSignIn) fragmento).onActivityResult(requestCode, resultCode, data); 
          } 
      } 
      

      }

    2. 創建微博登錄按鈕的對象。
      'TwitterLoginButton twitterLoginButton';

    5>更新片段上'onCreateView'中的代碼。

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
             Bundle savedInstanceState) 
    { 
        // Inflate the layout for this fragment 
        view = inflater.inflate(R.layout.fragment_twitter_sign_in, container, false); 
        twitterLoginButton = (TwitterLoginButton)view.findViewById(R.id.twitterLogin); 
        textView = (TextView) view.findViewById(R.id.textView); 
        parentLayout = (RelativeLayout) view.findViewById(R.id.twitterFragmentParentLinearLayout); 
        parentLayout.setOnClickListener(new View.OnClickListener() { 
         @Override 
         public void onClick(View v) { 
          Utility.hideKeyboard(v, getContext()); 
         } 
        }); 
    
        twitterLoginButton.setCallback(new Callback<TwitterSession>() { 
         @Override 
         public void success(Result<TwitterSession> result) { 
    
          TwitterSession session = result.data; 
    
          Twitter.getApiClient().getAccountService().verifyCredentials(true, false).enqueue(new Callback<User>(){ 
           @Override 
           public void success(Result<User> userResult) 
           { 
            try 
            { 
             User user = userResult.data; 
    
             textView.setText("UserNmae:"+user.name+"\nEmail:"+user.email+"\nImageUrl:"+user.profileImageUrl); 
            } catch (Exception e) 
            { 
             e.printStackTrace(); 
            } 
           } 
    
           @Override 
           public void failure(TwitterException e) 
           { 
    
           } 
          }); 
          String msg = "@" + session.getUserName() + " logged in! (#" + session.getUserId() + ")"; 
    
         } 
    
         @Override 
         public void failure(TwitterException exception) { 
          Log.d("TwitterKit", "Login with Twitter failure", exception); 
         } 
        }); 
    
        return view; 
    } 
    
  • 最後,在片段添加 'onctivityResult()'。

    @Override 公共無效onActivityResult(INT requestCode,INT發送resultCode,意圖數據){ super.onActivityResult(requestCode,resultCode爲,數據); twitterLoginButton.onActivityResult(requestCode,resultCode,data); }

  • 完成後,測試應用程序它應該工作。