我想在我的应用中添加Facebook登录功能。请注意:
唯一的区别是Request.executeMeRequestAsync()
hss已更改为Request.newMeRequest()
。我做了这个改变,但似乎我的会话总是很接近,虽然我可以通过调试项目看到Session.setActiveSession(session);
方法运行。所以,我们不知道为什么session
方法总是接近call()
。
任何想法都会受到赞赏。感谢。
我的代码:
public class FacebookLogin extends FragmentActivity
{
private static final String TAG = "FacebookLogin";
private static final List<String> READ_PERMISSIONS =
Arrays.asList("email", "user_about_me", "user_photos");
// private static final List<String> WRITE_PERMISSIONS = Arrays.asList("");
private final Session.StatusCallback statusCallback = new Session.StatusCallback()
{
@Override
public void call(final Session session, SessionState state, Exception exception)
{
if (session.isOpened())
{
// make request to the /me API
Request request = Request.newMeRequest(session, new Request.GraphUserCallback()
{
// callback after Graph API response with user object
@Override
public void onCompleted(GraphUser user, Response response)
{
if (user != null)
{
MyLog.d(TAG, "User name: " + user.getName() + "!, Login successfully :)");
MyLog.d(TAG, "User id: " + user.getId());
MyLog.d(TAG, "Access token is: " + session.getAccessToken());
MyLog.d(TAG, "Application id: " + session.getApplicationId());
MyLog.d(TAG, "JSON Object: " + user.getInnerJSONObject());
SpStorage.setKeyFacebook(FacebookLogin.this, session.getAccessToken());
SpStorage.setFacebookUserId(FacebookLogin.this, user.getId());
// erson person = parseJSON(user.getInnerJSONObject().toString());
// registerUser();
// Close activity
FacebookLogin.this.finish();
}
}
});
request.executeAsync();
}
else if (state.isClosed()) {
MyLog.d(TAG, "Facebook session closed");
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Find device hash key (should not be used in production)
// printHashKey();
// start Facebook Login
openActiveSession(this, true, statusCallback, READ_PERMISSIONS);
}
private static Session openActiveSession(Activity activity, boolean allowLoginUI,
Session.StatusCallback callback, List<String> permissions)
{
Session.OpenRequest openRequest = new Session
.OpenRequest(activity)
.setPermissions(permissions)
.setCallback(callback);
Session session = new Session.Builder(activity).build();
if (SessionState.CREATED_TOKEN_LOADED.equals(session.getState()) || allowLoginUI)
{
Session.setActiveSession(session);
session.openForRead(openRequest);
return session;
}
return null;
}
}
我在logcat中获得的内容:
Facebook session closed
答案 0 :(得分:2)
您需要覆盖Fragment类中的onActivityResult方法并将其传递给会话以完成会话转换。
无论您是否使用UiLifecycleHelper,都需要这样做。
答案 1 :(得分:1)
您需要覆盖onActivityResult并将值传递给facebook SDK。 来自Facebook的SDK文档:
为确保正确设置会话,您的片段必须 覆盖片段生命周期方法:onCreate(),onResume(), onPause(),onDestroy(),onActivityResult()和onSaveInstanceState() 并调用相应的UiLifecycleHelper方法。例如, 调用UiLifecycleHelper对象中的onCreate()方法创建 Facebook会话,如果缓存的令牌是自动打开它 可用。
答案 2 :(得分:1)
最近Facebook正在使用API V2.2并删除了一些方法。 最近在1周之前我做了一些获取用户信息。 这可能对你有所帮助。
public void getProfileData(View button){
Session activeSession = Session.getActiveSession();
new Request(
activeSession,
"me",
null,
HttpMethod.GET,
new Request.Callback() {
public void onCompleted(Response response) {
/* handle the result */
try
{ GraphObject go = response.getGraphObject();
JSONObject jso = go.getInnerJSONObject();
String name23 = jso.getString("name");
Log.e("Name response",""+name23);
name = name23;
Toast.makeText(getApplicationContext(), "Name: " + name , Toast.LENGTH_LONG).show();
}
catch ( Throwable t )
{
t.printStackTrace();
}
String name = response.toString();
Log.e("Name request",""+name);
}
}
).executeAsync();
}