无法从Facebook与Android集成

时间:2016-12-19 15:10:00

标签: android facebook facebook-graph-api android-navigation-drawer

我试图将Facebook作为登录集成在我的导航抽屉应用程序中,当我点击Facebook按钮时,我可以登录但登录后该应用程序不会显示姓名,电子邮件和图片。< / p>

我已经尝试了不同的方法来获取姓名,电子邮件和图片,但它不起作用。其中一种方法是将Profile profileDefault = Profile.getCurrentProfile()放入&#34; On Sucess&#34;然后是profileDefault.getFirstName(),但是当我运行应用程序并在登录后,应用程序停止。

当我使用getFaceBookProfileDetails功能时,我可以登录,然后应用程序继续运行,但不会显示姓名,电子邮件和图片。

这是主要活动

package com.example.abdiel.culi;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.util.Log;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import com.facebook.AccessToken;
import com.facebook.AccessTokenTracker;
import com.facebook.FacebookSdk;
import com.facebook.Profile;
import com.facebook.ProfileTracker;
import com.facebook.appevents.AppEventsLogger;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.login.LoginManager;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;
import com.squareup.picasso.Picasso;
import com.facebook.GraphRequest;
import com.facebook.GraphResponse;
import org.json.JSONObject;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.support.v4.app.FragmentTabHost;
import static android.view.View.INVISIBLE;
import static com.example.abdiel.culi.R.id.imageviewFotoPerfil;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    private CallbackManager callbackManager;
   // private AccessTokenTracker accessTokenTracker;
   // private ProfileTracker profileTracker;

    LoginButton buttonLoginFacebook;
    TextView textViewName, textViewEmail;
    ImageView imageViewPhoto;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        FacebookSdk.sdkInitialize(getApplicationContext());
        AppEventsLogger.activateApp(this);
        callbackManager = CallbackManager.Factory.create();


        setContentView(R.layout.activity_main);
        textViewName = (TextView) findViewById(R.id.textviewNombre);
        buttonLoginFacebook = (LoginButton) findViewById(R.id.login_button);
        textViewEmail = (TextView) findViewById(R.id.textviewEmail);
        buttonLoginFacebook.setReadPermissions("email");
        imageViewPhoto = (ImageView) findViewById(R.id.imageviewFotoPerfil);


        buttonLoginFacebook.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {

                //Método usado para obtener los campos o atributos solciitados
                 getFaceBookProfileDetails(loginResult.getAccessToken());
                //buttonLoginFacebook.setVisibility(View.GONE);

            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onError(FacebookException error) {

            }
        });



        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }
    private void getFaceBookProfileDetails(final AccessToken accessToken) {

        GraphRequest request = GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() {

            @Override
            public void onCompleted(final JSONObject object, GraphResponse response) {
               try {
                    //Profile clase que contiene las características báscias de la cuenta de facebook (No retorna email)
                    Profile profileDefault = Profile.getCurrentProfile();
                    //Librería usada para poder mostrar la foto de perfil de facebook con una transformación circular
                    Picasso.with(MainActivity.this).load(profileDefault.getProfilePictureUri(100,100)).transform(new CircleTransform()).into(imageViewPhoto);
                    textViewName.setText(profileDefault.getFirstName());
                    textViewEmail.setText(object.getString("email"));
                } catch (Exception e) {
                    Log.e("E-MainActivity", "getFaceBook" + e.toString());
                }
            }
        });
        Bundle parameters = new Bundle();
        //solicitando el campo email
        parameters.putString("fields",   "email");
        request.setParameters(parameters);
        request.executeAsync();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //Retorna la reppuesta después del ingreso de las credenciales de facebook
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onResume() {
        super.onResume();

        // Logs 'install' and 'app activate' App Events.
        AppEventsLogger.activateApp(this);
    }

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

        // Logs 'app deactivate' App Event.
        AppEventsLogger.deactivateApp(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.itemBusqueda) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

这是清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.abdiel.culi">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/facebook_app_id" />

        <activity
            android:name="com.facebook.FacebookActivity"
            android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
            android:label="@string/app_name" />

        <provider
            android:name="com.facebook.FacebookContentProvider"
            android:authorities="com.facebook.app.FacebookContentProvider442630429253125"
            android:exported="true" />

        <activity android:name=".Recomendados_Activity"></activity>
    </application>

</manifest>

这是Build Gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "25.0.0"
    defaultConfig {
        applicationId "com.example.abdiel.culi"
        minSdkVersion 16
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
repositories {
    mavenCentral()
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.facebook.android:facebook-android-sdk:4.17.0'
    compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'com.android.support:design:24.2.1'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.android.support:support-v4:24.2.1'
    testCompile 'junit:junit:4.12'
}

这是导航抽屉的内容主XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/content_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.abdiel.culi.MainActivity"
    tools:showIn="@layout/app_bar_main">

    <TextView
        android:id="@+id/textviewEmail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

    <com.facebook.login.widget.LoginButton
        android:id="@+id/login_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        />

</RelativeLayout>

这是导航抽屉的活动主XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

我也按照答案的步骤尝试了这段代码,但是当我运行应用程序时,它会停止。

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    private CallbackManager callbackManager;
    private AccessTokenTracker accessTokenTracker;
   // private ProfileTracker profileTracker;

    Profile profile;
    private ProfileTracker mProfileTracker;

    LoginButton buttonLoginFacebook;
    TextView textViewName, textViewEmail;
    ImageView imageViewPhoto;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        AppEventsLogger.activateApp(this);

        FacebookSdk.sdkInitialize(getApplicationContext());
        callbackManager = CallbackManager.Factory.create();
        updateWithToken(AccessToken.getCurrentAccessToken());
        accessTokenTracker = new AccessTokenTracker() {
            @Override
            protected void onCurrentAccessTokenChanged(AccessToken oldToken, AccessToken newToken) {
                updateWithToken(newToken);
            }
        };
        accessTokenTracker.startTracking();

        buttonLoginFacebook = (LoginButton) findViewById(R.id.login_button);
        buttonLoginFacebook.setReadPermissions(Arrays.asList(
                "public_profile", "email", "user_friends"));
        //buttonLoginFacebook.registerCallback(callbackManager,callback);
        buttonLoginFacebook.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(LoginResult loginResult) {
                        try {
                            if (Profile.getCurrentProfile() == null) {
                                mProfileTracker = new ProfileTracker() {
                                    @Override
                                    protected void onCurrentProfileChanged(Profile profile_old, Profile profile_new) {
                                        // profile2 is the new profile
                                        profile = profile_new;
                                        mProfileTracker.stopTracking();
                                    }
                                };
                                mProfileTracker.startTracking();
                            } else {
                                profile = Profile.getCurrentProfile();
                                Log.v("facebook - profile", profile.getFirstName());
                            }

                            GraphRequest request = GraphRequest.newMeRequest(
                                    loginResult.getAccessToken(),
                                    new GraphRequest.GraphJSONObjectCallback() {
                                        @Override
                                        public void onCompleted(JSONObject object, GraphResponse response) {
                                            Log.v("FACEBOOK LOGIN", response.toString());
                                            // Application code
                                            try {
                                                //Profile clase que contiene las características báscias de la cuenta de facebook (No retorna email)
                                                Profile profileDefault = Profile.getCurrentProfile();
                                                //Librería usada para poder mostrar la foto de perfil de facebook con una transformación circular
                                                Picasso.with(MainActivity.this).load(profileDefault.getProfilePictureUri(100,100)).transform(new CircleTransform()).into(imageViewPhoto);
                                                textViewName.setText(profileDefault.getFirstName());
                                                textViewEmail.setText(object.getString("email"));
                                            } catch (JSONException e) {
                                                e.printStackTrace();
                                            }
                                            //show facebook data in your activity
                                        }
                                    });
                            Bundle parameters = new Bundle();
                            parameters.putString("fields", "name,email,picture");
                            request.setParameters(parameters);
                            request.executeAsync();
                        } catch (Exception e) {
                            Log.d("ERROR", e.toString());
                        }
                    }

                    @Override
                    public void onCancel() {

                    }

                    @Override
                    public void onError(FacebookException e) {
                        Log.d("FACEBOOK ERRROR", e.toString());

                    }
                });




        setContentView(R.layout.activity_main);
        textViewName = (TextView) findViewById(R.id.textviewNombre);

        textViewEmail = (TextView) findViewById(R.id.textviewEmail);

        imageViewPhoto = (ImageView) findViewById(R.id.imageviewFotoPerfil);




        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }



    private void updateWithToken(AccessToken currentAccessToken) {

        if (currentAccessToken != null) {
            //User logged In--do your stuff
            //log out user first
            LoginManager.getInstance().logOut();
        } else {
            //user not logged in
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //Retorna la reppuesta después del ingreso de las credenciales de facebook
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onResume() {
        super.onResume();

        // Logs 'install' and 'app activate' App Events.
        AppEventsLogger.activateApp(this);
    }

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

        // Logs 'app deactivate' App Event.
        AppEventsLogger.deactivateApp(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.itemBusqueda) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

2 个答案:

答案 0 :(得分:0)

创建一个manageCallBackOfFacebook()方法或直接在您的活动中添加回调代码。在你的活动课上处理你的facebook回调。

public class LoginActivity extends AppCompatActivity
{

    private CallbackManager callbackManager;
    private AccessTokenTracker accessTokenTracker;
    private ProfileTracker profileTracker;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        //initialize facebook Sdk
        FacebookSdk.sdkInitialize(getApplicationContext());
        manageCallBackOfFacebook();
        callbackManager = CallbackManager.Factory.create();

        setContentView();
        ...
    }

    private void manageCallBackOfFacebook()
    {
        try
        {
            callbackManager = CallbackManager.Factory.create();
            accessTokenTracker = new AccessTokenTracker() {
                @Override
                protected void onCurrentAccessTokenChanged(AccessToken oldToken, AccessToken newToken) {

                }
            };

            profileTracker = new ProfileTracker()
            {
                @Override
                protected void onCurrentProfileChanged(Profile oldProfile, Profile newProfile)
                {
                    if (newProfile != null)
                    {
                        Log.i(TAG +" getName ", newProfile.getName());""+newProfile.getLinkUri());
                        Log.i(TAG +" getProfilePictureUri", ""+newProfile.getProfilePictureUri(200, 150));
                    }
                }
            };

            accessTokenTracker.startTracking();
            profileTracker.startTracking();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

答案 1 :(得分:0)

尝试此更改..

声明

Profile profile;
private ProfileTracker mProfileTracker;

在你的onCreate()

    FacebookSdk.sdkInitialize(getApplicationContext());
    callbackManager = CallbackManager.Factory.create();
    updateWithToken(AccessToken.getCurrentAccessToken());
    accessTokenTracker = new AccessTokenTracker() {
        @Override
        protected void onCurrentAccessTokenChanged(AccessToken oldToken, AccessToken newToken) {
            updateWithToken(newToken);
        }
    };
    accessTokenTracker.startTracking();

   loginButton = (LoginButton) findViewById(R.id.login_button);
   loginButton.setReadPermissions(Arrays.asList(
            "public_profile", "email", "user_friends"));
    loginButton.registerCallback(callbackManager, callback);
你的updateWithToken()方法中的

    private void updateWithToken(AccessToken currentAccessToken) {

    if (currentAccessToken != null) {
        //User logged In--do your stuff
        //log out user first
        LoginManager.getInstance().logOut();
    } else {
        //user not logged in
      }
   }

同样在你的回调方法中 - 将此添加到活动

 private FacebookCallback<LoginResult> callback = new FacebookCallback<LoginResult>() {

    @Override
    public void onSuccess(LoginResult loginResult) {
        try {
            if (Profile.getCurrentProfile() == null) {
                mProfileTracker = new ProfileTracker() {
                    @Override
                    protected void onCurrentProfileChanged(Profile profile_old, Profile profile_new) {
                        // profile2 is the new profile
                        profile = profile_new;
                        mProfileTracker.stopTracking();
                    }
                };
                mProfileTracker.startTracking();
            } else {
                profile = Profile.getCurrentProfile();
                Log.v("facebook - profile", profile.getFirstName());
            }

            GraphRequest request = GraphRequest.newMeRequest(
                    loginResult.getAccessToken(),
                    new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(JSONObject object, GraphResponse response) {
                            Log.v("FACEBOOK LOGIN", response.toString());
                            // Application code
                            try {
                                fb_id = object.getString("id");
                                fb_name = object.getString("name");
                                profilePicUrl = "https://graph.facebook.com/" + fb_id + "/picture?width=200&height=200";
                                fb_email = object.getString("email");
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            //show facebook data in your activity
                         }
                    });
            Bundle parameters = new Bundle();
            parameters.putString("fields", "id,name,email,picture.type(small)");
            request.setParameters(parameters);
            request.executeAsync();
        } catch (Exception e) {
            Log.d("ERROR", e.toString());
        }
    }

    @Override
    public void onCancel() {

    }

    @Override
    public void onError(FacebookException e) {
        Log.d("FACEBOOK ERRROR", e.toString());
    }
};