使用Facebook API时,配置文件返回null

时间:2015-12-23 11:39:38

标签: android facebook

profile.getProfile()在代码下运行时返回null 我使用正确的hashkey,正确的应用程序ID 我在onDestory()方法中停止配置文件并访问跟踪器 我没有使用任何Facebook应用程序 有任何想法来解决这个问题。

     @Override
        public void onCreate(Bundle savedInstane)
        {
            super.onCreate(savedInstane);
            // Add code to print out the key hash

            FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
            callbackManager = CallbackManager.Factory.create();
            accessTokenTracker = new AccessTokenTracker() {
                @Override
                protected void onCurrentAccessTokenChanged(AccessToken accessToken, AccessToken accessToken1) {

                }
            };
            profileTracker = new ProfileTracker() {
                @Override
                protected void onCurrentProfileChanged(Profile profile, Profile profile1) {

                }
            };
            accessTokenTracker.startTracking();
            profileTracker.startTracking();
        }


     private FacebookCallback<LoginResult> mcallBack = new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                AccessToken token = loginResult.getAccessToken();
                Profile profile = Profile.getCurrentProfile();
                Uri link = profile.getProfilePictureUri(20, 20);
                Log.d("Message",link.toString());
                display(profile);
            }
        };


 @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }

void facebookLogin()
    {
        FacebookSdk.sdkInitialize(getApplicationContext());
        callbackManager = CallbackManager.Factory.create();
        setContentView(R.layout.activity_signin);
        LoginButton login = (LoginButton)findViewById(R.id.login_button_facebook);
        login.setReadPermissions(Arrays.asList("public_profile", "user_friends"));
        login.registerCallback(callbackManager, mcallBackFacebook);
    }

1 个答案:

答案 0 :(得分:0)

请按照以下步骤操作:

  1. 在您的活动中

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
    
        FacebookSdk.sdkInitialize(this.getApplicationContext());
    
        callbackManager = CallbackManager.Factory.create();
    
    
        LoginManager.getInstance().registerCallback(callbackManager,
            new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {
                    GraphRequest request = GraphRequest.newMeRequest(
                            loginResult.getAccessToken(),
                            new GraphRequest.GraphJSONObjectCallback() {
                                @Override
                                public void onCompleted(
                                    JSONObject object,
                                    GraphResponse response) {
                                    // Call service for check facebook auth from server
                                    try {
                                        mFBID = encodeToBase64("" + object.get("email"));
                                        System.out.println("Your facebook is is-----" + mFBID);
                                        //YOUR CODE
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                }
                            });
    
                    //its will return id, name and email
                    Bundle parameters = new Bundle();
                    parameters.putString("fields", "id,name,email");
                    request.setParameters(parameters);
                    request.executeAsync();
    
                }
    
                @Override
                public void onCancel() {
                    Toast.makeText(LoginActivity.this, "Login Cancel", Toast.LENGTH_LONG).show();
                }
    
                @Override
                public void onError(FacebookException exception) {
                    Toast.makeText(LoginActivity.this, exception.getMessage(), Toast.LENGTH_LONG).show();
                }
            });
    
    
        setContentView(R.layout.XML);
    
    }
    
  2. 调用FB按钮的onClick事件:

    LoginManager.getInstance().logInWithReadPermissions(_activity, Arrays.asList("public_profile", "email", "user_friends"));
    
  3. onActivityResult()

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }
    
  4. 获取hashKey并创建密钥并在清单文件中注册FacebookActivity。

  5. 试试..在这里工作正常