2011-09-25 138 views
1

我編寫了一個集成了Facebook的Android應用程序,但登錄到facebook步驟失敗。我所做的基本上是執行授權,然後詢問會話是否有效。答案總是否定的。如果我想要一個簡單的要求,如:Facebook通過android登錄

String response = facebookClient.request("me"); 

我得到這樣的響應:

{「錯誤」:{「消息」:「一個積極的訪問令牌,必須使用查詢 關於當前用戶的信息。「,」type「:」OAuthException「}}

也許我有錯誤的哈希鍵(通過我讀了很好的線程如何得到它)。我想知道這是否是確保密鑰匹配的一種方式。

我在此基礎上我的代碼 - Android/Java -- Post simple text to Facebook wall?,並添加一些小的改動。這是代碼:

public class FacebookActivity extends Activity implements DialogListener 
{ 

    private Facebook facebookClient; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     this.setContentView(R.layout.main);//my layout xml 

    } 



    public void login(View view) 
    { 
      facebookClient = new Facebook("my APP ID"); 
      facebookClient.authorize(this, this); 
      if (facebookClient.isSessionValid() == true) 
       Log.d("Valid:", "yes"); 
      else 
       Log.d("Valid:", "no"); 
    } 
} 

回答

0

請注意,授權方法是異步的。

您應該實現DialogListener的方法的onComplete,讓所有你需要的工作(如圖形API me請求)那裏。

+0

感謝我被搞砸了很多代碼部分的答覆,讓我忘了檢查監聽器的響應。 它繼續 - onFacebookError(FacebookError錯誤),原因是因爲我認爲 - 「invalid_key」 – leonprou

+0

你可以複製 - 粘貼錯誤信息在聽衆中的onerror? –

+0

是的,這是相當短的: 「D/Facebook的授權(278):登錄失敗:invalid_key」 至少現在我已經跟蹤問題 – leonprou

0

使用以下要在應用代碼:

public class FacebookLogin { 
    private AsyncFacebookRunner mAsyncRunner; 
    private Facebook facebook; 
    private Context mContext; 
    private String mFName; 

    public static final String[] PERMISSIONS = new String[] {"email", "publish_checkins", "publish_stream","offline_access"}; 

    public FacebookLogin(Context mContext) { 
     this.mContext=mContext; 
     facebook=new Facebook(YOUR_APP_ID); 
     mAsyncRunner = new AsyncFacebookRunner(facebook); 
    } 

    public void Login() { 
     facebook.authorize((Activity) mContext,PERMISSIONS,Facebook.FORCE_DIALOG_AUTH,new LoginDialogListener()); 
    } 

    public void Logout() throws MalformedURLException, IOException { 
     facebook.logout(mContext); 
    } 
    public boolean isValidUser() { 
     return facebook.isSessionValid(); 
    } 

    class LoginDialogListener implements DialogListener { 
     public void onComplete(Bundle values) { 
      //Save the access token and access expire for future use in shared preferece 
       String profile=facebook.request("me") 
       String uid = profile.getString("id"); 
       mFName= profile.optString("first_name"); 
       new Session(facebook, uid, mFName).save(mContext); 
     } 

     public void onFacebookError(FacebookError error) { 
      displayMessage("Opps..! Check for Internet Connection, Authentication with Facebook failed."); 
     } 

     public void onError(DialogError error) { 
      displayMessage("Opps..! Check for Internet Connection, Authentication with Facebook failed."); 
     } 

     public void onCancel() { 
      displayMessage("Authentication with Facebook failed due to Login cancel."); 
     } 
    } 
} 

在登錄完成保存facebook的訪問令牌和訪問你的共享偏好到期,而再次使用一個Facebook對象訪問令牌和訪問後集過期Facebook的對象它不會給出你的代碼中發生的錯誤。

你可以使用下面的類:

public class Session { 

    private static Session singleton; 
    private static Facebook fbLoggingIn; 

    // The Facebook object 
    private Facebook fb; 

    // The user id of the logged in user 
    private String uid; 

    // The user name of the logged in user 
    private String name; 

    /** 
    * Constructor 
    * 
    * @param fb 
    * @param uid 
    * @param name 
    */ 
    public Session(Facebook fb, String uid, String name) { 
     this.fb = fb; 
     this.uid = uid; 
     this.name = name; 
    } 

    /** 
    * Returns the Facebook object 
    */ 
    public Facebook getFb() { 
     return fb; 
    } 

    /** 
    * Returns the session user's id 
    */ 
    public String getUid() { 
     return uid; 
    } 

    /** 
    * Returns the session user's name 
    */ 
    public String getName() { 
     return name; 
    } 

    /** 
    * Stores the session data on disk. 
    * 
    * @param context 
    * @return 
    */ 
    public boolean save(Context context) { 

      Editor editor = 
      context.getSharedPreferences(ConstantsFacebook.KEY, Context.MODE_PRIVATE).edit(); 
      editor.putString(ConstantsFacebook.TOKEN, fb.getAccessToken()); 
      editor.putLong(ConstantsFacebook.EXPIRES, fb.getAccessExpires()); 
      editor.putString(ConstantsFacebook.UID, uid); 
      editor.putString(ConstantsFacebook.NAME, name); 
      editor.putString(ConstantsFacebook.APP_ID, fb.getAppId()); 
      editor.putBoolean(ConstantsFacebook.LOGIN_FLAG,true); 
     if (editor.commit()) { 
      singleton = this; 
      return true; 
      } 
     return false; 
    } 

    /** 
    * Loads the session data from disk. 
    * 
    * @param context 
    * @return 
    */ 
    public static Session restore(Context context) { 
     if (singleton != null) { 
      if (singleton.getFb().isSessionValid()) { 
       return singleton; 
      } else { 
       return null; 
      } 
     } 

     SharedPreferences prefs = 
      context.getSharedPreferences(ConstantsFacebook.KEY, Context.MODE_PRIVATE); 

      String appId = prefs.getString(ConstantsFacebook.APP_ID, null); 

     if (appId == null) { 
      return null; 
     } 

     Facebook fb = new Facebook(appId); 
     fb.setAccessToken(prefs.getString(ConstantsFacebook.TOKEN, null)); 
     fb.setAccessExpires(prefs.getLong(ConstantsFacebook.EXPIRES, 0)); 
     String uid = prefs.getString(ConstantsFacebook.UID, null); 
     String name = prefs.getString(ConstantsFacebook.NAME, null); 

     if (!fb.isSessionValid() || uid == null || name == null) { 
      return null; 
     } 

     Session session = new Session(fb, uid, name); 
     singleton = session; 
     return session; 
    } 

    /** 
    * Clears the saved session data. 
    * 
    * @param context 
    */ 
    public static void clearSavedSession(Context context) { 
     Editor editor = 
      context.getSharedPreferences(ConstantsFacebook.KEY, Context.MODE_PRIVATE).edit(); 
     editor.clear(); 
     editor.commit(); 
     singleton = null; 
    } 

    /** 
    * Freezes a Facebook object while it's waiting for an auth callback. 
    */ 
    public static void waitForAuthCallback(Facebook fb) { 
     fbLoggingIn = fb; 
    } 

    /** 
    * Returns a Facebook object that's been waiting for an auth callback. 
    */ 
    public static Facebook wakeupForAuthCallback() { 
     Facebook fb = fbLoggingIn; 
     fbLoggingIn = null; 
     return fb; 
    } 

    public static String getUserFristName(Context context) { 
     SharedPreferences prefs = 
        context.getSharedPreferences(ConstantsFacebook.KEY, Context.MODE_PRIVATE); 
     String frist_name = prefs.getString(ConstantsFacebook.NAME, null); 
     return frist_name; 

    } 

    public static boolean checkValidSession(Context context) { 
     SharedPreferences prefs = 
       context.getSharedPreferences(ConstantsFacebook.KEY, Context.MODE_PRIVATE); 
     Boolean login=prefs.getBoolean(ConstantsFacebook.LOGIN_FLAG,false); 
     return login; 
    } 


}