how to implement facebook login in android using facebook sdk 4.7

前端 未结 6 1478
既然无缘
既然无缘 2020-12-01 15:30

I am trying to login with facebook using android sdk 4.7. I have tried the following link http://www.theappguruz.com/blog/android-facebook-integration-tutorial http://www.an

6条回答
  •  庸人自扰
    2020-12-01 16:10

    1. First Go to https://developers.facebook.com/ ,Login and select My Apps and create a App.

    2. Follow the given instruction on by one properly.

    3. Give packagename and packagename.ActivityName, select use App name then save it.

    4. Generate Hash Key (Download OpenSSL) and (Java JDK). - For windows!

    5. Extract the OpenSSL stuffs to C:\OpenSSL

    6. Go to CMD prompt set the current path to JDK's bin folder.

    7. Then use this command :

    keytool -exportcert -alias androiddebugkey -keystore "C:\Users\Shredder\.android\debug.keystore" | "C:\OpenSSL\bin\openssl" sha1 -binary | "C:\OpenSSL\bin\openssl" base64

    Make sure you are using proper Paths like UserAccountName etc.Mine was shredder.After that use Password : 123456.

    1. Paste the hashKey to the required field. Then follow along....

    2. Use the following code if required.

      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_sign__in);
          String fb_id ="";
          String fb_fName ="";
          String fb_lName ="";
          String fb_email ="";
          String EMAIL = "email";
      
          CallbackManager callbackManager = CallbackManager.Factory.create();
          final LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
          loginButton.setReadPermissions(Arrays.asList(EMAIL));
      
          loginButton.registerCallback(callbackManager, new FacebookCallback() {
              @Override
              public void onSuccess(LoginResult loginResult) {
      
                  String userId = loginResult.getAccessToken().getUserId();
      
                  GraphRequest graphRequest = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                      @Override
                      public void onCompleted(JSONObject object, GraphResponse response) {
                          getUserFbUserInfo (object);
                      }
                  });
      
                  Bundle parameters = new Bundle();
                  parameters.putString("fields", "first_name,last_name,email,id");
                  graphRequest.setParameters(parameters);
                  graphRequest.executeAsync();
      
      
                  Intent it = new Intent(getApplicationContext(), Home_Page.class);
                  it.putExtra("fbLogin", true);
                  startActivity(it);
              }
      
              @Override
              public void onCancel() {
                  // App code
              }
      
              @Override
              public void onError(FacebookException exception) {
                  // App code
              }
          });
      
      
      }
      
      private void getUserFbUserInfo(JSONObject object) {
      
      
              try {
                  fb_email = object.getString("email");
                  fb_fName = object.getString("first_name");
                  fb_lName = object.getString("last_name");
                  fb_id = object.getString("id");
              } 
              catch (JSONException e) {
                  e.printStackTrace(); 
              }   
      }
      
      @Override
      protected void onActivityResult ( int requestCode, int resultCode, Intent data){
          callbackManager.onActivityResult(requestCode, resultCode, data);
          super.onActivityResult(requestCode, resultCode, data);
      }
      

      }

提交回复
热议问题