Blog

  • Google Plus Authentication Android

    In this blog I am explain google plus login process and accessing other required information from google in Android Studio IDE.

     

    Following are the required steps for accessing API key

     

    1. Visit google developer console

    2. Create App → Select APIs & auth → search Google+ API and enable it

    3. From credentials tab → create new key → select android key → create

     

    Add Google play services in your project

     

    Open project structure → Dependencies → Add Library dependencies → Add google play services library

     

    Add following permission in android Manifest.xml

     

    
    
    
    

     

    
     

     

    Add following code to your Activity class

    public class MainActivity extends Activity  implements
          ConnectionCallbacks, OnConnectionFailedListener
            
    {
         private GoogleApiClient mGoogleApiClient;
    SignInButton mGoogleSignInButton;
    boolean mGoogleSignInStatus;
    
        
    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
    	if (mGoogleSignInStatus ||mFacebookSignInStatus) {
        Intent nextActivity = new Intent(getApplicationContext(), ProfileSettingActivity.class);
        startActivity(nextActivity);
    }
    
            mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Plus.API)
            .addScope(Plus.SCOPE_PLUS_LOGIN)
            .build();
    
    //google plus login
    mGoogleSignInButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mGoogleSignInStatus = true;
            SharedPreferences.Editor editor = mSharedPref.edit();
            editor.putBoolean("GoogleSignInState", mGoogleSignInStatus);
            editor.commit();
            mGoogleApiClient.connect();
        }
    });
      	
        }
    
       protected void onStart() {
        super.onStart();
        mGoogleApiClient.connect();
      }
    
      protected void onStop() {
        super.onStop();
    
        if (mGoogleApiClient.isConnected()) {
          mGoogleApiClient.disconnect();
        }
      }
     

    Add Following to your layout.xml file to create login button

     

     
    

    Accessing Google plus token

    class GetGooglePlusToken extends AsyncTask
        {
            @Override
            protected String doInBackground(Void... params) {
                String accessToken="";
                try {
                    accessToken=GoogleAuthUtil.getToken(mContext, Plus.AccountApi.getAccountName(mGoogleApiClient), "oauth2:"
                            + Scopes.PLUS_LOGIN);
                    Log.d("Access Token ","Google plus Access Token "+accessToken);
                }
                catch (IOException ioException){
                    Toast.makeText(getApplicationContext(), ioException.getMessage(),Toast.LENGTH_SHORT).show();
                }
                catch(UserRecoverableAuthException authExcep){
                    Toast.makeText(getApplicationContext(), authExcep.getMessage(),Toast.LENGTH_SHORT).show();
                }
                catch (GoogleAuthException googleAuthException){
                    Toast.makeText(getApplicationContext(), googleAuthException.getMessage(),Toast.LENGTH_SHORT).show();
                }
        return accessToken;
            }
    

     

    THANKS

Tags: android

View All PostsLeave a Comment
comments powered by Disqus

Mobile Applications

Video Content

Bigdata & NoSQL

SaaS Applications

Miscellaneous

Archives


Alexa Certified Site Stats for www.oodlestechnologies.com