SHARE
TWEET

Untitled

a guest Jun 25th, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import android.app.Activity;
  2. import android.content.Intent;
  3. import android.content.pm.PackageInfo;
  4. import android.content.pm.PackageManager;
  5. import android.content.pm.Signature;
  6. import android.os.Bundle;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.util.Base64;
  9. import android.util.Log;
  10. import android.view.View;
  11.  
  12. import com.facebook.AccessToken;
  13. import com.facebook.CallbackManager;
  14. import com.facebook.FacebookCallback;
  15. import com.facebook.FacebookException;
  16. import com.facebook.Profile;
  17. import com.facebook.login.LoginResult;
  18. import com.facebook.login.widget.LoginButton;
  19. import com.google.android.gms.auth.api.signin.GoogleSignIn;
  20. import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
  21. import com.google.android.gms.auth.api.signin.GoogleSignInClient;
  22. import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
  23. import com.google.android.gms.common.SignInButton;
  24. import com.google.android.gms.common.api.ApiException;
  25. import com.google.android.gms.tasks.Task;
  26.  
  27. import java.security.MessageDigest;
  28. import java.security.NoSuchAlgorithmException;
  29.  
  30. public class LoginActivity extends AppCompatActivity {
  31.     CallbackManager callbackManager;
  32.     GoogleSignInClient mGoogleSignInClient;
  33.     int RC_SIGN_IN = 101;
  34.  
  35.  
  36.     @Override
  37.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  38.         callbackManager.onActivityResult(requestCode,requestCode,data);
  39.         super.onActivityResult(requestCode, resultCode, data);
  40.  
  41.         // Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
  42.         if (requestCode == RC_SIGN_IN) {
  43.             // The Task returned from this call is always completed, no need to attach
  44.             // a listener.
  45.             Task<GoogleSignInAccount> mTask = GoogleSignIn.getSignedInAccountFromIntent(data);
  46.             handleSignInResult(mTask);
  47.  
  48.         }
  49.     }
  50.  
  51.  
  52.  
  53.     @Override
  54.     protected void onCreate(Bundle savedInstanceState) {
  55.         super.onCreate(savedInstanceState);
  56.         setContentView(R.layout.activity_login);
  57.         LoginButton loginButton = (LoginButton)findViewById(R.id.login_button);
  58.         AccessToken accessToken = AccessToken.getCurrentAccessToken();
  59.         boolean isLoggedIn = accessToken != null && !accessToken.isExpired();
  60.         printKeyHash(this);
  61.         loginButton.setReadPermissions("email");
  62.         callbackManager = CallbackManager.Factory.create();
  63.         loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
  64.             @Override
  65.             public void onSuccess(LoginResult loginResult) {
  66.                 Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
  67.                 startActivity(intent);
  68.             }
  69.             @Override
  70.             public void onCancel() {
  71.                 // App code
  72.             }
  73.             @Override
  74.             public void onError(FacebookException exception) {
  75.                 // App code
  76.             }
  77.         });
  78.  
  79.  
  80.         SignInButton signInButton = findViewById(R.id.sign_in_button);
  81.  
  82.  
  83.         // Configure sign-in to request the user's ID, email address, and basic
  84.         // profile. ID and basic profile are included in DEFAULT_SIGN_IN.
  85.         GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
  86.                 .requestEmail()
  87.                 .build();
  88.  
  89.         // Build a GoogleSignInClient with the options specified by gso.
  90.         mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
  91.  
  92.         // Check for existing Google Sign In account, if the user is already signed in
  93.         // the GoogleSignInAccount will be non-null.
  94.         GoogleSignInAccount mAccount = GoogleSignIn.getLastSignedInAccount(this);
  95.         updateUI(mAccount);
  96.  
  97.         signInButton.setOnClickListener(new View.OnClickListener() {
  98.             @Override
  99.             public void onClick(View v) {
  100.                 signIn();
  101.             }
  102.         });
  103.     }
  104.  
  105.     //Function to update UI
  106.     public void updateUI(GoogleSignInAccount account){
  107.  
  108.         if(account != null){
  109.             String personName = account.getDisplayName();
  110.             String personGivenName = account.getGivenName();
  111.             String personFamilyName = account.getFamilyName();
  112.             String personEmail = account.getEmail();
  113.             String personId = account.getId();
  114.             //Uri personPhoto = account.getPhotoUrl();
  115.             String personPhoto = String.valueOf(account.getPhotoUrl());
  116.  
  117.             Intent signInIntent = new Intent(LoginActivity.this, HomeActivity.class);
  118.             signInIntent.putExtra("person_name",personName);
  119.             signInIntent.putExtra("person_given_name", personGivenName);
  120.             signInIntent.putExtra("person_family_name",personFamilyName);
  121.             signInIntent.putExtra("person_email", personEmail);
  122.             signInIntent.putExtra("person_id", personId);
  123.             signInIntent.putExtra("person_photo", personPhoto);
  124.  
  125.             startActivity(signInIntent);
  126.  
  127.         }
  128.     }
  129.  
  130.     //Function to sign in
  131.     private void signIn() {
  132.         Intent signInIntent = mGoogleSignInClient.getSignInIntent();
  133.         startActivityForResult(signInIntent, RC_SIGN_IN);
  134.     }
  135.  
  136.     //Function to handle sign in result
  137.     public void handleSignInResult(Task<GoogleSignInAccount> completedTask){
  138.  
  139.         try {
  140.             GoogleSignInAccount account = completedTask.getResult(ApiException.class);
  141.  
  142.             // Signed in successfully, show authenticated UI.
  143.             updateUI(account);
  144.         } catch (ApiException e) {
  145.             // The ApiException status code indicates the detailed failure reason.
  146.             // Please refer to the GoogleSignInStatusCodes class reference for more information.
  147.             Log.w("SIGNINFAILED", "signInResult:failed code=" + e.getStatusCode());
  148.             updateUI(null);
  149.         }
  150.  
  151.  
  152.     }
  153.  
  154.     public static String printKeyHash(Activity context) {
  155.         PackageInfo packageInfo;
  156.         String key = null;
  157.         try {
  158.             //getting application package name, as defined in manifest
  159.             String packageName = context.getApplicationContext().getPackageName();
  160.  
  161.             //Retriving package info
  162.             packageInfo = context.getPackageManager().getPackageInfo(packageName,
  163.                     PackageManager.GET_SIGNATURES);
  164.  
  165.             Log.e("Package Name=", context.getApplicationContext().getPackageName());
  166.  
  167.             for (Signature signature : packageInfo.signatures) {
  168.                 MessageDigest md = MessageDigest.getInstance("SHA");
  169.                 md.update(signature.toByteArray());
  170.                 key = new String(Base64.encode(md.digest(), 0));
  171.  
  172.                 // String key = new String(Base64.encodeBytes(md.digest()));
  173.                 Log.e("Key Hash=", key);
  174.             }
  175.         } catch (PackageManager.NameNotFoundException e1) {
  176.             Log.e("Name not found", e1.toString());
  177.         }
  178.         catch (NoSuchAlgorithmException e) {
  179.             Log.e("No such an algorithm", e.toString());
  180.         } catch (Exception e) {
  181.             Log.e("Exception", e.toString());
  182.         }
  183.  
  184.         return key;
  185.     }
  186.  
  187. }```
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top