我正在尝试以下方法:
问题:我的问题是如何使用此传递的访问令牌创建访问API的facebook会话。如果有人可以指出一个例子,那将会更有帮助。
答案 0 :(得分:2)
截至Android 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);
}
}
});
}
答案 1 :(得分: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...
答案 2 :(得分: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类是:
public class 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();
}
}
可能会帮助你。