3

Okay i was trying this code from Google https://developers.google.com/identity/sign-in/android/sign-in?configured=true

LoginActivity

GoogleApiClient mGoogleApiClient;
GoogleSignInOptions gso;
SignInButton signIn_btn;
private static final int RC_SIGN_IN = 0;
ProgressDialog progress_dialog;

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


    buidNewGoogleApiClient();
    setContentView(R.layout.activity_login);
    customizeSignBtn();
    setBtnClickListeners();


    mNotificationManager = (NotificationManager) this.getApplicationContext().getSystemService(this.NOTIFICATION_SERVICE);

    Typeface myTypeFace = Typeface.createFromAsset(getAssets(), "Big Daddy St.ttf");
    Typeface myTypeFaceFacebook = Typeface.createFromAsset(getAssets(), "Big Daddy St.ttf");


    mainImage = (ImageView) findViewById(R.id.imageView1);

    TextView textView1 = (TextView) findViewById(R.id.textView1);
    textView1.setOnClickListener(this);
    textView1.setTypeface(myTypeFace);

    Button button = (Button) findViewById(R.id.btn_login_facebook);
    button.setOnClickListener(this);
    button.setTypeface(myTypeFaceFacebook);



}

private void buidNewGoogleApiClient() {

    gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .build();
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this, this)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();
}

private void customizeSignBtn() {

    signIn_btn = (SignInButton) findViewById(R.id.sign_in_button);
    signIn_btn.setSize(SignInButton.SIZE_STANDARD);
    signIn_btn.setScopes(gso.getScopeArray());

}

private void setBtnClickListeners() {
    // Button listeners
    signIn_btn.setOnClickListener(this);


}

protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

protected void onStop() {
    super.onStop();
    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
}

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

    // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
    if (requestCode == RC_SIGN_IN) {
        if (resultCode != RESULT_OK) {



        }
        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        getSignInResult(result);
    }
}

private void getSignInResult(GoogleSignInResult result) {
GoogleSignInStatusCodes.getStatusCodeString(result.getStatus().getStatusCode());


    if (result.isSuccess()) {

        // Signed in successfully, show authenticated UI.
        GoogleSignInAccount acct = result.getSignInAccount();
        Toast.makeText(this, "email: " + acct.getEmail(), Toast.LENGTH_SHORT).show();

        //TextView user_name= (TextView)findViewById(R.id.userName);
        // TextView email_id= (TextView)findViewById(R.id.emailId);
        // user_name.setText("UserName: "+ acct.getDisplayName());
        // email_id.setText("Email Id: " + acct.getEmail());

        progress_dialog.dismiss();
    } else {
        // Signed out, show unauthenticated UI.
        try {


        } catch (Exception e) {
            Toast.makeText(this, "Exception: " +e.getMessage(), Toast.LENGTH_LONG).show();
        }

        //updateUI(false);
    }
}






@Override
public void onClick(View view) {




    if(view.getId() == R.id.textView1){
        Intent signupIntent = new Intent(this, SignUpScreen.class);
        startActivity(signupIntent);

    }

    if(view.getId() == R.id.btn_login_facebook){
        Intent intent = new Intent(this, todelete.class);

        //Notification
        // notificationWithButton();
        startActivity(intent);
    }
    if(view.getId()==R.id.sign_in_button){
        Toast.makeText(this, "start sign process", Toast.LENGTH_SHORT).show();
        gSignIn();


    }

}
private void gSignIn() {
    Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
    startActivityForResult(signInIntent, RC_SIGN_IN);
  //  progress_dialog.show();
}


void notificationWithButton(){
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        // Prepare intent which is triggered if the  notification button is pressed
        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

        // Building the notifcation
        NotificationCompat.Builder nBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher) // notification icon
                .setContentTitle("Button notification") // notification title
                .setContentText("Expand to show the buttons...") // content text
                .setTicker("Showing button notification") // status bar message
                .addAction(R.drawable.common_google_signin_btn_icon_dark, "Accept", pIntent) // accept notification button
                .addAction(R.drawable.common_google_signin_btn_icon_dark_normal, "Cancel", pIntent); // cancel notification button

        mNotificationManager.notify(1001, nBuilder.build());

    } else {
        Toast.makeText(this, "You need a higher version", Toast.LENGTH_LONG).show();
    }
}


@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}

}

Manifest.xml

 <uses-permission android:name="android.permission.INTERNET" />
    <!-- To access accounts configured on device -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <!-- To use account credentials -->
    <uses-permission android:name="android.permission.USE_CREDENTIALS" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WIFI" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"

    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key" />
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version"

/>

build.gradle (Project)

  dependencies {
    classpath 'com.android.tools.build:gradle:2.1.3'
    classpath 'com.google.gms:google-services:3.0.0'

build.gradle (App)

compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.jakewharton:butterknife:8.1.0'
compile 'com.google.android.gms:play-services-auth:9.6.1'
compile 'com.google.android.gms:play-services:9.6.1'
compile 'com.android.support:support-v4:24.2.1'
}

apply plugin: 'com.google.gms.google-services'

Your help will be much appreciated.

Edit: Added GoogleSignInStatusCodes.getStatusCodeString(result.getStatus().getStatusCode()); which returns 10, corresponding to DEVELOPER_ERROR.

I had previously created a key for Google Maps, which is included in my Project (In Manifest file, meta tags), now do I have to create another Key? If yes, how do I include it?

AIS
  • 297
  • 1
  • 4
  • 16

3 Answers3

0

To debug this, I would do this: breakpoint / print to log what you get back from Google on success login. That will tell you where the data is what you want.

What is the next Activity? Looks like you are starting something from Google, but it would make more sense to start something of your own making.

edit went over it. Some thoughts:

googleSignInButton = (SignInButton) findViewById(R.id.sign_in_button);
findViewById(R.id.sign_in_button).setOnClickListener(this);

// Can be done this way:
googleSignInButton = (SignInButton) findViewById(R.id.sign_in_button);
googleSignInButton.setOnClickListener(this);

And this is how I have done something like it:

SignInButton buttonGoogle = (SignInButton) findViewById(R.id.button_google);
assert buttonGoogle != null;
buttonGoogle.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
        startActivityForResult(signInIntent, RC_SIGN_IN);
    }
});

And to handle a Successful login from Google you will need something like this: getResources().getString(R.string.google) => Getting a string from the strings.xml

/**
 * Handling the result from Google.
 * @param result                    You know, tha package.
 */
private void handleSignInResult(GoogleSignInResult result) {
    Log.d("handleSignInResult:", "" + result.isSuccess());
    if (result.isSuccess()) {
        // Signed in successfully, show authenticated UI.
        GoogleSignInAccount acct = result.getSignInAccount();
        successfulLogin(getResources().getString(R.string.google),
                acct.getDisplayName(),
                acct.getId(),
                acct.getEmail());
    } else { // but is never used atm.
        String message = String.format(getResources().getString(R.string.communication_signed_out), getResources().getString(R.string.google));
        Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
    }
}
0

Try below code

           private void handleSignInResult(GoogleSignInResult result) {
                Log.d(String.valueOf(LoginActivity.this), "handleSignInResult:" + result.isSuccess());
                if (result.isSuccess()) {
                    // Signed in successfully, show authenticated UI.
                    GoogleSignInAccount acct = result.getSignInAccount();
              if (acct != null) {

                        Intent iWelcomeActivity = new Intent(MainActivity.this, WelcomeActivity.class);
                        iWelcomeActivity.putExtra("USERNAME", acct.getDisplayName());
                        iWelcomeActivity.putExtra("EMAIL", acct.getEmail();
);
                        startActivity(iWelcomeActivity);
                    }
                } else {
                    // Signed out, show unauthenticated UI.
                    //updateUI(false);
                }
            }

When user taps sigin button an intent will prompts the user to select a Google account to sign in with. onActivityResult method, retrieve the sign-in result and handleSignInResult(GoogleSignInResult result) is called .

If sign-in succeeded, you can call the getSignInAccount method to get a GoogleSignInAccount object that contains information about the signed-in user. Use GoogleSignInAccount object to retrieve information as acct.getDisplayName(),acct.getEmail() and pass it to next activity as done in above code.

Hopes it will help

Rissmon Suresh
  • 13,173
  • 5
  • 29
  • 38
  • Tried your code, 11-06 19:36:33.118 468-468/com.example......LoginActivity@5956e2d: handleSignInResult:false – AIS Nov 06 '16 at 14:38
  • When debugging, in OnActivityResult, GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data); result has nothing – AIS Nov 06 '16 at 14:41
  • is data in OnActivityResult null?? – Rissmon Suresh Nov 06 '16 at 14:46
  • Yes, nothing in data. Do you want me to post all the contents in data?As – AIS Nov 06 '16 at 14:57
  • data = {Intent@5130} "Intent { (has extras) }" mAction = null mCategories = null mClipData = null mComponent = null mContentUserHint = -2 mData = null mExtras = {Bundle@5177} "Bundle[{googleSignInStatus=Status{statusCode=DEVELOPER_ERROR, resolution=null}}]" mFlags = 0 mPackage = null mSelector = null mSourceBounds = null mType = null shadow$_klass_ = {Class@1392} "class android.content.Intent" shadow$_monitor_ = -2000768399 – AIS Nov 06 '16 at 15:02
  • When I uninstall the app, and start again, it shows dialog with gmail accounts but does not start ActivityUserProfile. From then on, even the dialog doesnt open on clicking – AIS Nov 06 '16 at 15:04
0

I was getting this same error, until I found this SO question. The problem was that if you don't configure your debug build to use your release signing key, it will use a default debugkeystore instead. If you've set up the API Key correctly, they won't match. Obviously, it would work if you build a release .apk with your release signing key, but if you are debugging (in development) you may have overlooked this process.

The Solution

  1. Goto Build > Edit Build Types...

  2. On the Signing Tab, Create a new entry for your release signing key alias and keystore. You can name it whatever you like, I used 'release'.

  3. Now, on the Build Types Tab, for the 'debug' build type, Set the Signing Config to whatever was created in step 2.

  4. Click OK, and Run the app again.

Bob's your Auntie

Community
  • 1
  • 1
Matthew Weber
  • 889
  • 9
  • 22