2

私は次のことを試みています:

  1. 1 つの Android モバイルで、Facebook でアプリを承認し、アクセス トークンを共有設定に保存しています。
  2. このアクセス トークンを 2 番目の Android デバイスに送信します (Facebook 用に同じ appKey を持つ別のアプリを使用)。
  3. 2 番目のデバイスでこのアクセス トークンを使用してセッションを作成できません。2 番目のデバイスには UI がないため、Web/FB ネイティブ アプリを使用して認証することはできません。

質問: 私の質問は、この渡されたアクセス トークンを使用して、API にアクセスするための facebook セッションを作成する方法です。誰かが例を示すことができれば、より役に立ちます。

4

3 に答える 3

2

Android 3.0 最終版の facebook SDK の時点で、API が変更され、@rightparen によって提案された回答は機能しません。新しい API では、キーはSession.openActiveSessionWithAccessToken()静的メソッドを使用することです。この方法は、fb sdk 2.x から 3.x への移行用に設計されています。

次のコードは私にとってはうまくいきます。

@SuppressWarnings("deprecation")
public static void migrateFbTokenToSession() {
    Facebook facebook = Utils.getFacebookObject();
    AccessToken accessToken = AccessToken.createFromExistingAccessToken(facebook.getAccessToken(), new Date(facebook.getAccessExpires()),
            new Date(facebook.getLastAccessUpdate()), AccessTokenSource.FACEBOOK_APPLICATION_NATIVE, Arrays.asList(Constants.FB_APP_PERMISSIONS));
    Session.openActiveSessionWithAccessToken(ICheezApplication.getContext(), accessToken , new Session.StatusCallback() {

        @Override
        public void call(Session session, SessionState state, Exception exception) {
            sLogger.debug("fbshare Migration to newer icheez state has been successful");
            if(session != null && session.isOpened()) {
                Session.setActiveSession(session);
            }
        }
    });

}
于 2012-12-19T11:04:58.527 に答える
1

この関数は、渡されたパラメーターでデフォルトの sdk キャッシュ トークン状態を上書きすることにより、既存のトークン状態を移行できます。

private final void migrateToken(String accessToken, long expiresMilliseconds,
                                List<String> permissions, boolean isSSO,
                                long lastRefreshMilliseconds) {
    Bundle bundle = new Bundle();
    TokenCache.putToken(bundle, accessToken);
    TokenCache.putExpirationMilliseconds(bundle, expiresMilliseconds);
    TokenCache.putPermissions(bundle, permissions);
    TokenCache.putLastRefreshMilliseconds(bundle, lastRefreshMilliseconds);
    TokenCache.putIsSSO(bundle, isSSO);

    SharedPreferencesTokenCache cache = new SharedPreferencesTokenCache(this);
    cache.save(bundle);
}

アクセス許可を保存していない場合は、トークンを取得したときに要求したアクセス許可のリストを渡すか、要求していないか不明な場合は空の ArrayList を渡す必要があります。

isSSO パラメーターは、Facebook ログイン/Facebook アプリでの SSO を使用してトークンを取得したか (true)、ログイン WebView を使用してトークンを取得したか (false) を指定します。Facebook ログイン経由で取得したトークンは拡張できます。このブール値は、SDK がトークンの拡張を自動的に試行するかどうかを制御します。

この関数は、Session コンストラクターによって読み取られた状態を上書きします。したがって、関数を呼び出す場合は、後でそれを使用するためにセッションを作成する必要があります。ロジックは次のようになります。

    Session session = Session.openActiveSession(this);
    if ((session == null) && hasOldTokenState()) {
        migrateToken(...);
        session = Session.openActiveSession(this);
    }
    // if session is still null, user will have to log in...
于 2012-11-09T06:57:22.500 に答える
0

そのアクセストークンを取得してユーザーを承認する方法がわかりませんが、セッションが有効でない場合は、以下のコードを使用してセッションを再作成しています。

private SessionListener mSessionListener = new SessionListener();

Utility.mFacebook = new Facebook(APP_ID);
Utility.mAsyncRunner = new AsyncFacebookRunner(Utility.mFacebook);
SessionStore.restore(Utility.mFacebook, getApplicationContext());

SessionEvents.addAuthListener(mSessionListener);
SessionEvents.addLogoutListener(mSessionListener);

私のSessionEventクラスは次のとおりです。

パブリッククラスSessionEvents {

private static LinkedList<AuthListener> mAuthListeners = new LinkedList<AuthListener>();
private static LinkedList<LogoutListener> mLogoutListeners = new LinkedList<LogoutListener>();

/**
 * Associate the given listener with this Facebook object. The listener's
 * callback interface will be invoked when authentication events occur.
 * 
 * @param listener
 *            The callback object for notifying the application when auth
 *            events happen.
 */
public static void addAuthListener(AuthListener listener) {
    mAuthListeners.add(listener);
}

/**
 * Remove the given listener from the list of those that will be notified
 * when authentication events occur.
 * 
 * @param listener
 *            The callback object for notifying the application when auth
 *            events happen.
 */
public static void removeAuthListener(AuthListener listener) {
    mAuthListeners.remove(listener);
}

/**
 * Associate the given listener with this Facebook object. The listener's
 * callback interface will be invoked when logout occurs.
 * 
 * @param listener
 *            The callback object for notifying the application when log out
 *            starts and finishes.
 */
public static void addLogoutListener(LogoutListener listener) {
    mLogoutListeners.add(listener);
}

/**
 * Remove the given listener from the list of those that will be notified
 * when logout occurs.
 * 
 * @param listener
 *            The callback object for notifying the application when log out
 *            starts and finishes.
 */
public static void removeLogoutListener(LogoutListener listener) {
    mLogoutListeners.remove(listener);
}

public static void onLoginSuccess() {
    for (AuthListener listener : mAuthListeners) {
        listener.onAuthSucceed();
    }
}

public static void onLoginError(String error) {
    for (AuthListener listener : mAuthListeners) {
        listener.onAuthFail(error);
    }
}

public static void onLogoutBegin() {
    for (LogoutListener l : mLogoutListeners) {
        l.onLogoutBegin();
    }
}

public static void onLogoutFinish() {
    for (LogoutListener l : mLogoutListeners) {
        l.onLogoutFinish();
    }
}

/**
 * Callback interface for authorization events.
 */
public static interface AuthListener {

    /**
     * Called when a auth flow completes successfully and a valid OAuth
     * Token was received. Executed by the thread that initiated the
     * authentication. API requests can now be made.
     */
    public void onAuthSucceed();

    /**
     * Called when a login completes unsuccessfully with an error.
     * 
     * Executed by the thread that initiated the authentication.
     */
    public void onAuthFail(String error);
}

/**
 * Callback interface for logout events.
 */
public static interface LogoutListener {
    /**
     * Called when logout begins, before session is invalidated. Last chance
     * to make an API call. Executed by the thread that initiated the
     * logout.
     */
    public void onLogoutBegin();

    /**
     * Called when the session information has been cleared. UI should be
     * updated to reflect logged-out state.
     * 
     * Executed by the thread that initiated the logout.
     */
    public void onLogoutFinish();
}

}

それはあなたを助けるかもしれません。

于 2012-11-07T10:10:38.263 に答える