我尝试开发一个用于Facebook用户身份验证的Android应用程序我按照facebook official websites的程序
问题没有进入session.isopened()
fb.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
try {
PackageInfo info = getPackageManager().getPackageInfo(
"com.brightvision.dealsocl.mobileapp",
PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
}
} catch (NameNotFoundException e) {
} catch (NoSuchAlgorithmException e) {
}
Session session = Session.getActiveSession();
if (!session.isOpened() && !session.isClosed()) {
Log.i("enter", "Session not opened");
/*Session.OpenRequest openRequest= new Session.OpenRequest(getParent());
openRequest.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
session.openForRead(openRequest);*/
session.openForRead(new Session.OpenRequest(FrontPage.this).setCallback(statusCallback));
} else {
Session.openActiveSession(FrontPage.this,true,new StatusCallback() {
@Override
public void call(final Session session, SessionState state, Exception exception) {
// TODO Auto-generated method stub
if (session.isOpened()) {
Log.i("enter", "Session opened");
Request.executeMeRequestAsync(session,new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user,Response response) {
if (user != null) {
String access_token = session.getAccessToken();
String firstName = user.getFirstName();
String fb_user_id = user.getId();
GraphObject graphObject = response.getGraphObject();
JSONObject jsonObject = graphObject.getInnerJSONObject();
try {
String facebookEmail = jsonObject.getString("email");
System.out.println("facebookEmail Login " + facebookEmail);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// System.out.println("json object -> "+jsonObject);
//
System.out.println("First Name Login:"+ firstName);
System.out.println("FB USER ID Login: " + fb_user_id);
fname= firstName;
}
}
});
System.out.println("session is opened");
}else{
System.out.println("session is not opened");
}
System.out.println("Session state "+state);
if(exception!=null){
System.out.println("Exception in login "+exception.getMessage());
}
}
});
}
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
Log.i("on activity result", "enter");
Session.getActiveSession().onActivityResult(FrontPage.this, requestCode, resultCode, data);
Log.i("on activity result", "completed");
}
private class SessionStatusCallback implements Session.StatusCallback {
@Override
public void call(Session session, SessionState state, Exception exception) {
// your code. will execute when you are already logged in
/* Intent i=new Intent(FrontPage.this,MapList.class);
startActivity(i);*/
Toast.makeText(getApplicationContext(), "welcome to facebook "+fname, 1000).show();
}
}
这是一个Logcat:
06-27 16:36:31.412:D / KeyHash:(750):844bro74Ov8hjX4YlTNgrx3IfT0 = 06-27 16:36:31.412:我/输入(750):会话未打开 06-27 16:36:31.592:I / Adreno200-EGLSUB(750)::格式化RGBA_8888。
任何人都可以解决这个问题吗?
答案 0 :(得分:0)
试试这个。
在onCreate()
之前声明Session.StatusCallback statusCallback = new SessionStatusCallback();
Session session;
在onCreate中粘贴此代码
session = Session.getActiveSession();
if (session == null) {
if (savedInstanceState != null) {
session = Session.restoreSession(this, null, statusCallback, savedInstanceState);
}
if (session == null) {
session = new Session(this);
}
Session.setActiveSession(session);
if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
}
}
将以下代码添加到您的登录按钮
private void onClickLogin() {
Session session = Session.getActiveSession();
if (!session.isOpened() && !session.isClosed()) {
session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
} else {
Session.openActiveSession(this,true,new StatusCallback() {
@Override
public void call(final Session session, SessionState state, Exception exception) {
// TODO Auto-generated method stub
if (session.isOpened()) {
Request.executeMeRequestAsync(session,new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user,Response response) {
if (user != null) {
String access_token = session.getAccessToken();
String firstName = user.getFirstName();
String fb_user_id = user.getId();
GraphObject graphObject = response.getGraphObject();
JSONObject jsonObject = graphObject.getInnerJSONObject();
try {
String facebookEmail = jsonObject.getString("email");
System.out.println("facebookEmail Login " + facebookEmail);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// System.out.println("json object -> "+jsonObject);
//
System.out.println("First Name Login:"+ firstName);
System.out.println("FB USER ID Login: " + fb_user_id);
}
}
});
System.out.println("session is opened");
}else{
System.out.println("session is not opened");
}
System.out.println("Session state "+state);
if(exception!=null){
System.out.println("Exception in login "+exception.getMessage());
}
}
});
}
}
在班级中添加此方法
private class SessionStatusCallback implements Session.StatusCallback {
@Override
public void call(Session session, SessionState state, Exception exception) {
// your code. will execute when you are already logged in
}
}