使用图形API的Facebook登录 [英] facebook login using graph Api
问题描述
我想用图形API,因为我想user_birthday和USER_LOCATION有Facebook登录。我使用这个链接之前登录:
http://www.androidhive.info/2012/03/android- Facebook的连接教程/
I want to have facebook login using graph Api because I want user_birthday and user_location. Before I was using this link to login: http://www.androidhive.info/2012/03/android-facebook-connect-tutorial/
但我没有得到象出生日期和地点的更多信息。所以我对接近图形API。但没有找到它的任何教程。我尝试了一些链接,并建立一些code,但得到的会话无效。我相信这就是一个实现问题。这里是我的code:
But I was not getting additional information like birth date and location. So I am approaching towards graph Api. But didn't find any tutorial for it. I tried some link and build some code but getting session null. I am sure thats a implementation issue. Here is my code:
fbLogin.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
try {
openActiveSession(LoginScreenActivity.this, true, fbStatusCallback, Arrays.asList(
new String[] { "email", "user_location", "user_birthday"}), savedInstanceState);
}
catch (Exception e) {
Log.i("exception", e.getMessage());
}
Intent intent=new Intent(LoginScreenActivity.this,DealGaliTabActivity.class);
startActivity(intent);
}
});
private Session openActiveSession(Activity activity, boolean allowLoginUI,
StatusCallback callback, List<String> permissions, Bundle savedInstanceState) {
OpenRequest openRequest = new OpenRequest(activity)
.setPermissions(permissions).setLoginBehavior(SessionLoginBehavior
.SSO_WITH_FALLBACK).setCallback(callback)
.setDefaultAudience(SessionDefaultAudience.FRIENDS);
Session session = Session.getActiveSession();
Log.d("session", "" + session);
if (session == null) {
Log.d("sussion_Null", "" + savedInstanceState);
if (savedInstanceState != null) {
session = Session.restoreSession(this, null, fbStatusCallback, savedInstanceState);
}
session = Session.openActiveSessionFromCache(LoginScreenActivity.this);
Session.setActiveSession(session);
Log.d("sessionSet", "" + session);
if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED) || allowLoginUI) {
session.openForRead(openRequest);
return session;
}
}
return session;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}
private Session.StatusCallback fbStatusCallback = new Session.StatusCallback() {
public void call(Session session, SessionState state, Exception exception) {
if (state.isOpened()) {
Request.newMeRequest(session, new Request.GraphUserCallback() {
public void onCompleted(GraphUser user, Response response) {
if (response != null) {
Log.i("Graphresponse", response.toString());
try{
get_id = user.getId();
get_name = user.getName();
get_gender = (String) user.getProperty("gender");
get_email = (String) user.getProperty("email");
get_birthday = user.getBirthday();
get_locale = (String) user.getProperty("locale");
get_location = user.getLocation().toString();
Log.d("GraphSelected", user.getId() + "; " +
user.getName() + "; " +
(String) user.getProperty("gender") + "; " +
(String) user.getProperty("email") + "; " +
user.getBirthday()+ "; " +
(String) user.getProperty("locale") + "; " +
user.getLocation());
} catch(Exception e) {
e.printStackTrace();
Log.d("GraphException", e.getMessage());
}
}
}
});
}
}
};
我也没有得到首次login..please Facebook的屏幕显示我一些解决的办法。
I am also not getting facebook screen for first time login..please suggest me some solution to it.
推荐答案
这是我want..I希望这将可帮助他人......
This is what I want..I hope it would be helpful to others...
public void readData(){
if (Session.getActiveSession() == null
|| Session.getActiveSession().isClosed()) {
Session.openActiveSession(this, true, new StatusCallback() {
@Override
public void call(Session session, SessionState state,
Exception exception) {
System.out.println("State= " + state);
if (session.isOpened()) {
System.out.println("Token=" + session.getAccessToken());
if(session.getAccessToken() != null){
sharedPrefernces();
}
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
public void onCompleted(GraphUser user, Response response) {
if (response != null) {
Log.i("Graphresponse", response.toString());
try{
get_id = user.getId();
get_name = user.getName();
get_gender = (String) user.getProperty("gender");
get_email = (String) user.getProperty("email");
get_birthday = user.getBirthday();
get_locale = (String) user.getProperty("locale");
get_location = user.getLocation().toString();
URL img_value = null;
img_value = new URL("http://graph.facebook.com/"+get_id+"/picture?type=large");
mImage = BitmapFactory.decodeStream(img_value.openConnection().getInputStream());
Log.d("GraphSelected", get_id + " " +
get_name + " " +
get_gender + " " +
get_email + " " +
get_birthday + " " +
get_locale + " " +
get_location);
} catch(Exception e) {
e.printStackTrace();
Log.d("GraphException", e.getMessage());
}
}
}
});
}
if (exception != null) {
System.out.println("Some thing bad happened!");
exception.printStackTrace();
}
}
});
}
}
这篇关于使用图形API的Facebook登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!