2014년 9월 12일 금요일

Signing in with Google+ for Android

First step is to go to the Google Developers Console (https://console.developers.google.com/project) and create project as well as Client ID.



Second step would be adding some permissions in AndroidManifest.xml to access Google+ API, account name as part of sign in, OAuth 2.0 tokens or invalidate tokens to disconnect. The following permissions should be requested.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />

Now we're ready to add some codes to initialize GoogleApiClient.
Some might wonder what GoogleApiClient really does, it basically wraps a ServiceConnection to Google Play services, this GoogleApiClient object is used to communicate with Google+ API and becomes functional after the asynchronous connection has been established with the service, and typically GoogleApiClient is managed like this:
- initialize GoogleApiClient in Activity.onCreate()
- invoke GoogleApiClient.connect() during Activity.onStart()
- invoke GoolgeApiClient.disconnect() during Acitivity.onStop()


import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.plus.Plus;

public class PlusActivity extends Activity 
        implements ConnectionCallbacks, OnConnectionFailedListener {

 /* Request code used to invoke sign in user interactions. */
 private static final int RC_SIGN_IN = 0;

 private GoogleApiClient mGoogleClient;

 private ProgressDialog mConnectionProgressDialog;

 /*
  * A flag indicating that a PendingIntent is in progress and prevents us
  * from starting further intents.
  */
 private boolean mIntentInProgress;

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

  mGoogleClient = new GoogleApiClient.Builder(this).addApi(Plus.API)
    .addScope(Plus.SCOPE_PLUS_LOGIN).addConnectionCallbacks(this)
    .addOnConnectionFailedListener(this).build();

  mConnectionProgressDialog = new ProgressDialog(this);
  mConnectionProgressDialog.setMessage("Signing in...");
 }

 @Override
 protected void onStart() {
  super.onStart();
  mGoogleClient.connect();
 }

 @Override
 protected void onStop() {
  super.onStop();

  if (mGoogleClient.isConnected()) {
   mGoogleClient.disconnect();
  }
 }

 @Override
 protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
  if (requestCode == RC_SIGN_IN) {
   mIntentInProgress = false;

   if (!mGoogleClient.isConnecting()) {
    mGoogleClient.connect();
   }
  }
 }

 @Override
 public void onConnectionFailed(ConnectionResult connectionResult) {
  Toast.makeText(this, "GoogleAPIClient Failed to Connect", Toast.LENGTH_SHORT).show();

  if (!mIntentInProgress && connectionResult.hasResolution()) {
   try {
    mIntentInProgress = true;
    startIntentSenderForResult(connectionResult.getResolution().getIntentSender(), RC_SIGN_IN, null, 0, 0, 0);
   } catch (SendIntentException e) {
    mIntentInProgress = false;
    mGoogleClient.connect();
   }
  }
 }

 @Override
 public void onConnected(Bundle bundle) {
  Toast.makeText(this, "GoogleAPIClient Connected", Toast.LENGTH_SHORT).show();
  mConnectionProgressDialog.dismiss();
 }

 @Override
 public void onConnectionSuspended(int arg0) {
  Toast.makeText(this, "GoogleAPIClient Connection Suspended", Toast.LENGTH_SHORT).show();
  mGoogleClient.connect();
 }
}

This is it, we done signing in Google+, will try to add some codes for accessing Google+ API to post some comments, retrieving friends list next time.





댓글 없음:

댓글 쓰기