Created
October 5, 2019 06:02
-
-
Save DiganVaghasiya/5396e5fd8cf6cbeff34d0b5ed85fff4d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.n.socialintegration; | |
| import androidx.annotation.Nullable; | |
| import androidx.appcompat.app.AppCompatActivity; | |
| import android.content.Context; | |
| import android.content.Intent; | |
| import android.content.pm.PackageInfo; | |
| import android.content.pm.PackageManager; | |
| import android.content.pm.Signature; | |
| import android.os.Bundle; | |
| import android.util.Base64; | |
| import android.util.Log; | |
| import android.view.View; | |
| import com.facebook.AccessToken; | |
| import com.facebook.CallbackManager; | |
| import com.facebook.FacebookCallback; | |
| import com.facebook.FacebookException; | |
| import com.facebook.Profile; | |
| import com.facebook.login.LoginResult; | |
| import com.facebook.login.widget.LoginButton; | |
| import com.google.android.gms.auth.api.signin.GoogleSignIn; | |
| import com.google.android.gms.auth.api.signin.GoogleSignInAccount; | |
| import com.google.android.gms.auth.api.signin.GoogleSignInClient; | |
| import com.google.android.gms.auth.api.signin.GoogleSignInOptions; | |
| import com.google.android.gms.common.SignInButton; | |
| import com.google.android.gms.common.api.ApiException; | |
| import com.google.android.gms.tasks.Task; | |
| import java.security.MessageDigest; | |
| import java.security.NoSuchAlgorithmException; | |
| public class MainActivity extends AppCompatActivity { | |
| private static final int RC_SIGN_IN = 1; | |
| LoginButton mLoginButton; | |
| CallbackManager mCallManager; | |
| private String TAG = "MainActivity"; | |
| private GoogleSignInClient mGoogleSignInClient; | |
| SignInButton signInButton; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| // TODO: 2019-10-05 Integrating Google Sign-In into Your Android App | |
| GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) | |
| .requestEmail() | |
| .build(); | |
| signInButton = findViewById(R.id.sign_in_button); | |
| signInButton.setSize(SignInButton.SIZE_STANDARD); | |
| signInButton.setOnClickListener(new View.OnClickListener() { | |
| @Override | |
| public void onClick(View view) { | |
| Intent signInIntent = mGoogleSignInClient.getSignInIntent(); | |
| startActivityForResult(signInIntent, RC_SIGN_IN); | |
| } | |
| }); | |
| // TODO: 2019-10-05 Integrating Facebook Sign-In into Your Android App | |
| mGoogleSignInClient = GoogleSignIn.getClient(this, gso); | |
| mCallManager = CallbackManager.Factory.create(); | |
| mLoginButton = findViewById(R.id.login_button); | |
| printHashKey(getApplicationContext()); | |
| mLoginButton.registerCallback(mCallManager, new FacebookCallback<LoginResult>() { | |
| @Override | |
| public void onSuccess(LoginResult loginResult) { | |
| AccessToken accessToken = loginResult.getAccessToken(); | |
| Profile profile = Profile.getCurrentProfile(); | |
| Log.e(TAG, "onSuccess: " + accessToken.getUserId()); | |
| } | |
| @Override | |
| public void onCancel() { | |
| } | |
| @Override | |
| public void onError(FacebookException exception) { | |
| exception.printStackTrace(); | |
| Log.e(TAG, "onError: " + exception.getMessage()); | |
| } | |
| }); | |
| } | |
| @Override | |
| protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { | |
| super.onActivityResult(requestCode, resultCode, data); | |
| // TODO: 2019-10-05 Integrating Facebook Sign-In into Your Android App | |
| mCallManager.onActivityResult(requestCode, resultCode, data); | |
| // TODO: 2019-10-05 Integrating Google Sign-In into Your Android App | |
| if (requestCode == RC_SIGN_IN) { | |
| Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data); | |
| handleSignInResult(task); | |
| } | |
| } | |
| private void handleSignInResult(Task<GoogleSignInAccount> completedTask) { | |
| try { | |
| GoogleSignInAccount account = completedTask.getResult(ApiException.class); | |
| Log.e(TAG,"Email-->>"+account.getEmail()); | |
| } catch (ApiException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| public static void printHashKey(Context pContext) { | |
| try { | |
| PackageInfo info = pContext.getPackageManager().getPackageInfo(pContext.getPackageName(), | |
| PackageManager.GET_SIGNATURES); | |
| for (Signature signature : info.signatures) { | |
| MessageDigest md = MessageDigest.getInstance("SHA"); | |
| md.update(signature.toByteArray()); | |
| String hashKey = new String(Base64.encode(md.digest(), 0)); | |
| Log.e("MainActivity", "printHashKey: " + hashKey); | |
| } | |
| } catch (PackageManager.NameNotFoundException e) { | |
| e.printStackTrace(); | |
| } catch (NoSuchAlgorithmException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment