我一直在尝试将 facebook 集成到我的应用程序中,但它总是返回一个 Null Graph 用户。我尝试添加 onActivityResult 方法作为解决方案告诉 here ,但似乎仍然无济于事。
public class FacebookRegistartionActivity extends FragmentActivity
{
private Session.StatusCallback mStatusCallback = new SessionStatusCallback();
// private static final int REQUEST_CODE_SIGN_IN = 1;
Session session;
@Override
protected void onCreate(Bundle arg0) {
// TODO Auto-generated method stub
super.onCreate(arg0);
fbRegistration();
}
public void fbRegistration() {
Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);
session = Session.getActiveSession();
Log.i("", "Session before : "+session);
if (session == null) {
session = new Session(FacebookRegistartionActivity.this);
if (session != null) {
Session.setActiveSession(session);
if (session != null && !session.isOpened()&& !session.isClosed()) {
session.openForRead(new Session.OpenRequest(this).setCallback(mStatusCallback));
} else {
}
}
} else {
Log.i("", "Session null ... : "+session);
// start Facebook Login
Session.openActiveSession(this, true, mStatusCallback);
}
}
代码甚至没有到达 onActivityResult() 方法。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
Log.i("", "requestCode : "+requestCode);
Log.i("", "resultCode : "+resultCode);
Log.i("", "data : "+data);
if (resultCode == RESULT_OK) {
Session.getActiveSession().onActivityResult(this, requestCode,resultCode, data);
}
}
private class SessionStatusCallback implements Session.StatusCallback {
// callback when session changes state
@Override
public void call(Session session, SessionState state,
Exception exception) {
Log.i("", "session in on status call back....."+session);
// Here the session is there, not null
if (session.isOpened()) {
Log.i("", "session is opened....."+session.isOpened());
// make request to the /me API
Request.newMeRequest(session, new Request.GraphUserCallback() {
// callback after Graph API response with user object
@Override
public void onCompleted(final GraphUser user, Response response) {
Log.i("in SignUp", "USER :: " + user);
// Log.i("in SignUp", "response :: " + response);
if (user != null) {
try{
String fqlQuery = "select url from profile_pic where id=me() and width=200 and height=200";
Bundle params1 = new Bundle();
params1.putString("q", fqlQuery);
Session session = Session.getActiveSession();
Request request = new Request(session, "/fql", params1,
HttpMethod.GET, new Request.Callback() {
public void onCompleted(Response response) {
try {
String serverresponse=response.getGraphObject().getInnerJSONObject().toString();
Log.e("serverresponse", ""+serverresponse);
JSONObject jsonobj=new JSONObject(serverresponse);
JSONArray array=jsonobj.getJSONArray("data");
Log.e("","data object : "+array);
jsonobj = array.getJSONObject(0);
String fbUserImageUrl = jsonobj.getString("url");
Log.e("","image url : "+fbUserImageUrl);
Logger.show(Log.INFO, "", " AccessToken with publish stream:: " +Session.getActiveSession().getAccessToken());
String uniqueId = user.getId();
String userName = user.getName();
String image = fbUserImageUrl; //"http://graph.facebook.com/"+user.getId()+"/picture?type=large";
String emailId = user.asMap().get("email").toString();
Logger.show(Log.INFO,"","uniqueId :: "+ uniqueId);
Logger.show(Log.INFO,"","userName :: "+ userName);
Logger.show(Log.INFO,"","image :: "+ image);
Logger.show(Log.INFO,"","emailId :: "+ emailId);
}
catch(Exception e)
{
e.printStackTrace();
}
}
});
Request.executeBatchAsync(request);
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
else
{
Log.i("", "Graph user null");
// Always the Graph user is returning Null
}
}
}).executeAsync();
}
else
{
Log.i("", "Session not opened still.....");
}
}
}
}
编辑:
我检查了答案,然后意识到,我只是错过了将权限添加到 Android Manifest 文件中。
才发现!除了在 Android Manifest 文件中添加以下权限外,我做了所有事情。
<uses-permission android:name="android.permission.INTERNET" />
但是,仍然想知道我是如何获得 session 的。意思是,它不需要网络连接?考虑到我测试的设备中安装了 Facebook 应用程序。
session 是由 Facebook 应用程序提供的吗?希望了解 facebook 应用程序如何使用 facebook sdk 在我们应用程序的登录流程中工作。
如有任何帮助,我们将不胜感激。
最佳答案
正如我在评论中提到的 - 是的,当创建 facebook Session 对象时,它确实会尝试从 TokenCachingStrategy 初始化自身。但是必须有一个 Session token 缓存。正如您所说,您使用的是来自其他应用程序的相同应用程序 ID,该应用程序已获得用户授权, session 已创建并保存在缓存中。
因为 facebook 根据应用程序 ID 管理 session ,并且由于 session 已经创建,它会从缓存中为您的新应用程序获取它。但是当您调用 Request.newMeRequest 获取图表用户时,它正在建立互联网连接,因为 list 中没有互联网许可,您的应用无法连接到 Facebook 服务器来获取相同的信息。
关于android - 图用户返回 Null,即使 session 已创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24053562/