这是我的第一篇文章,如果我不遵守社区规则,请告诉我。
现在来问题了!我一直在尝试为应用程序设置Facebook登录过程,我已经获得了添加GUI元素(内置的Facebook登录按钮),并且按下按钮并要求用户/通行证,授予应用访问用户帐户的权限。我面临的问题是,当使用以下代码从应用程序获取回调值时,我什么也得不到(没有打印值)。现在我打印到LogCat任意值,但是一旦打印工作,我会查看用户access_token和其他信息(最初没有工作)。任何帮助表示赞赏!
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_connect_accounts);
Log.i("fromMe", "test1");
FacebookSdk.sdkInitialize(this.getApplicationContext());
callbackManager = CallbackManager.Factory.create();
LoginButton loginButton = (LoginButton)findViewById(R.id.login_button);
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>(){
public void onSuccess(LoginResult loginResult) {
Log.i("fromMe", "1");
}
public void onCancel() {
Log.i("fromMe", "2");
}
public void onError(FacebookException exception) {
Log.i("fromMe", "3");
}
});
}
答案 0 :(得分:1)
我从未使用内置的facebook按钮,而是使用普通按钮并单击一个监听器。我在onClick()
。
myButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
//Starting facebook signIN
startFbSignIn();
}
});
<强> startFbSignIn(); 强>
private void startFbSignIn() {
List<String> permissions = new ArrayList<>();
permissions.add("email");
Session.openActiveSession(this, true, permissions, new Session.StatusCallback() {
@Override
public void call(final Session session, SessionState sessionState, Exception e) {
Log.d("X", "Call Called");
if (session.isOpened()) {
Log.d("X", "SessionOpened" + session.getAccessToken());
//Showing some loading
Request.newMeRequest(session, new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser graphUser, Response response) {
//FACEBOOOK SIGNIN SUCCESS
Log.i("X", "Facebook Login Success");
//Log
Log.d("X", "GRAPH USER:" + graphUser.toString());
String name = graphUser.getName();
//.....
}
}).executeAsync();
} else {
Log.d("X", "SessioNOTopened");
}
}
});
}
上述方法成功地为我工作,您的onActivityResult()
必须看起来像这样
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//Facebook Session Manager
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}
答案 1 :(得分:1)
如果Log.i("fromMe", "1");
有效,您可以致电:
AccessToken accessToken = loginResult.getAccessToken();
在onSuccess()
。
并在onActivityResult(int requestCode, int resultCode, Intent data)
添加:
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
答案 2 :(得分:1)
可能导致facebook SDK中的错误无法直接获取accessToken。我们需要创建一个accessToken跟踪器,然后获取accessToken
private AccessTokenTracker accessTokenTracker;
accessTokenTracker = new AccessTokenTracker() {
@Override
protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken newAccessToken) {
try {
if (newAccessToken.getToken() != null) {
facebookAccessToken = newAccessToken.getToken();
//this is your accessToken here
}
else if (oldAccessToken.getToken() != null) {
facebookAccessToken = oldAccessToken.getToken();
//this is your accessToken here
}else {
Toast.makeText(getBaseContext(), "Facebook Login Failed", Toast.LENGTH_LONG).show();
}
if(oldAccessToken.getToken()!=null)
Log.e("old", oldAccessToken.getToken());
}catch (Exception e){
e.printStackTrace();
}
}
};
成功完成后,在活动的onStop方法旁边,并确保在那里停止跟踪accessToken。
accessTokenTracker.stopTracking();