2

我正在尝试以下操作:

  1. 在一台 android 手机上,我正在使用 Facebook 授权我的应用程序,并将访问令牌存储在共享首选项中。
  2. 将此访问令牌发送到第二个 android 设备(某些不同的应用程序具有相同的 Facebook 应用程序密钥。
  3. 现在我无法在第二台设备上使用此访问令牌创建会话。第二个设备没有 UI,因此我不能使用 web/FB 本地应用程序在那里进行授权。

问题:我的问题是如何使用这个传递的访问令牌创建一个 facebook 会话来访问 API。如果有人可以指出一个例子,那将会更有帮助。

4

3 回答 3

2

从 facebook sdk for Android 3.0 final 开始,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 登录/SSO 与 facebook 应用程序 (true) 还是登录 WebView (false) 获取令牌。通过 Facebook 登录获得的令牌可以扩展,此布尔值控制 SDK 是否应自动尝试扩展令牌。

此函数覆盖由 Session 构造函数读取的状态。所以如果你调用这个函数,你需要在之后构造一个 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 类是:

公共类会话事件{

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 回答