Facebook Android API在登录后无法返回应用

时间:2014-04-21 05:19:13

标签: android facebook facebook-graph-api login

我一直在努力让Facebook登录使用Android API ... 我已经尝试了几个教程并搜索了很多但无法解决我的问题......

我正在尝试在我的应用上进行简单的登录... 登录工作...当我点击登录按钮时,Facebook应用程序或webviewer打开但是在记录帐户并且给出了权限后,我的应用程序不会返回到屏幕... Facebook UI只是关闭,没有任何反应......

这是我的代码:

package com.trucounter.crimemap.app.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.facebook.FacebookException;
import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.model.GraphUser;
import com.facebook.widget.LoginButton;
import com.trucounter.crimemap.app.R;

    import java.util.Arrays;


    /**
    * Created by Gabriel on 19/04/14.
    */
    public class LoadActivity extends Activity {

    private String TAG = "LoadActivity";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.load_layout);

        LoginButton loginButton = (LoginButton)findViewById(R.id.loginButton);

        loginButton.setOnErrorListener(
            new LoginButton.OnErrorListener()
            {
                @Override
                public void onError(FacebookException error)
                {
                    Log.i(TAG, "Error: " + error.getMessage());
                }
            }
        );

        //Setting permission list
        loginButton.setReadPermissions(Arrays.asList("basic_info","email"));

        //Session state callback event
        loginButton.setSessionStatusCallback(
            new Session.StatusCallback()
            {
                @Override
                public void call(Session session, SessionState state, Exception exception)
                {
                    if(session.isOpened())
                    {
                        Log.i(TAG, "Access Token: " + session.getAccessToken());
                        Request meRequest = Request.newMeRequest(
                                session,
                                new Request.GraphUserCallback()
                                {
                                    @Override
                                    public void onCompleted(GraphUser user, Response response)
                                    {
                                        if(user != null)
                                        {
                                            Log.i(TAG, "User ID: " + user.getId());
                                            Log.i(TAG, "Email: " + user.asMap().get("email"));
                                        }
                                    }
                                }
                        );
                        meRequest.executeAsync();
                    }
                }
            }
        );
    }

    @Override
    protected void onPause() {
        super.onPause();
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
    }
}

我对日志一无所知......

我发现了类似的问题,但没有找到有效的解决方案

提前致谢

2 个答案:

答案 0 :(得分:1)

您可以查看Facebook SDK示例。他们会帮忙的。但是下面的代码对我有用。

private Session.StatusCallback statusCallback = new SessionStatusCallback();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mylayout);

    Session session = new Session(getApplicationContext());
    Session.setActiveSession(session);
    session.openForRead(new Session.OpenRequest(MyActivity.this).setDefaultAudience(SessionDefaultAudience.ONLY_ME).setPermissions(Arrays.asList("user_birthday", "email")).setCallback(statusCallback));
                Session.setActiveSession(session);


    onSessionStateChange();
}

private class SessionStatusCallback implements Session.StatusCallback {
    @Override
    public void call(Session session, SessionState state, Exception exception) {
        onSessionStateChange();
    }
}



private void onSessionStateChange() {
    Session session = Session.getActiveSession();
    if(session != null){
        if (session.isOpened()){

            Request getMe = Request.newMeRequest(session, new Request.GraphUserCallback() {
                @Override
                public void onCompleted(GraphUser user, Response response) {
                    if(user != null){
                        finish();
                    }
                }
            }
        }
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}

希望这可以提供帮助。

答案 1 :(得分:-1)

我正在使用socialauth库进行facebook,twitter和linkedin登录。它工作正常。

从此链接https://github.com/3pillarlabs/socialauth-android下载Socialauth库  并在您的工作空间中导入customUI示例。运行此演示。如果您在掌握此代码时遇到任何困难,请问我。