Bước 1 : Đầu tiên vào console.developers.google.com tạo mới 1 project
Bước 2 : Vào mục API& auth /APIs enable google+ sign-in
Bước 3 : Vào mục API& auth /Credential nhấp vô tạo mới 1 cái Client ID
SH1 lấy bằng cách :
gõ cmd đến đường dẫn sau . . . .
SH1 lấy bằng cách :
gõ cmd đến đường dẫn sau . . . .
- C:\Program Files\Java\jdk1.7.0_75\bin
rồi gõ
keytool -exportcert -alias androiddebugkey -keystore%HOMEPATH%/.android/debug.keystore -list -vpass là : android
cái đoạn in đậm là đường dẫn của keystore —> nếu chưa có thì down về
copy cái dòng SHA1
copy cái dòng SHA1
Bước 4 : Code
Các Bạn Cần Thay đổi những phần sau
MainActivity + AndroidManifest + UI + Build.gradle
MainActivity + AndroidManifest + UI + Build.gradle
Build.gradle
- dependencies {
- compile fileTree(dir: 'libs', include: ['*.jar'])
- compile 'com.android.support:appcompat-v7:22.2.1'
- <strong>compile 'com.google.android.gms:play-services:7.5.0'</strong>
- }
MainActivity
- import android.app.Activity;
- import android.content.Intent;
- import android.content.IntentSender;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import com.google.android.gms.common.ConnectionResult;
- import com.google.android.gms.common.Scopes;
- import com.google.android.gms.common.SignInButton;
- import com.google.android.gms.common.api.CommonStatusCodes;
- import com.google.android.gms.common.api.GoogleApiClient;
- import com.google.android.gms.common.api.ResultCallback;
- import com.google.android.gms.common.api.Scope;
- import com.google.android.gms.plus.People;
- import com.google.android.gms.plus.Plus;
- import com.google.android.gms.plus.model.people.Person;
- import com.google.android.gms.plus.model.people.PersonBuffer;
- public class MainActivity extends Activity implements
- GoogleApiClient.ConnectionCallbacks,
- GoogleApiClient.OnConnectionFailedListener,
- View.OnClickListener, ResultCallback<People.LoadPeopleResult> {
- private String TAG = "MAINACTIVITY" ;
- /* Request code used to invoke sign in user interactions. */
- private static final int RC_SIGN_IN = 0;
- /* Is there a ConnectionResult resolution in progress? */
- private boolean mIsResolving = false;
- /* Should we automatically resolve ConnectionResults when possible? */
- private boolean mShouldResolve = false;
- /* Client used to interact with Google APIs. */
- private GoogleApiClient mGoogleApiClient;
- private SignInButton mSignInButton;
- private Button mSignOutButton;
- private Button mRevokeButton ;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- mSignInButton = (SignInButton) findViewById(R.id.sign_in_button);
- mSignOutButton = (Button) findViewById(R.id.sign_out_button);
- mSignOutButton.setVisibility(View.INVISIBLE); // hide button SignOut
- mRevokeButton = (Button) findViewById(R.id.revoke_button) ;
- mRevokeButton.setVisibility(View.INVISIBLE); // hide button revoke
- // Build GoogleApiClient with access to basic profile
- mGoogleApiClient = new GoogleApiClient.Builder(this)
- .addConnectionCallbacks(this)
- .addOnConnectionFailedListener(this)
- .addApi(Plus.API)
- .addScope(new Scope(Scopes.PROFILE))
- .build();
- mSignInButton.setOnClickListener(this);
- mSignOutButton.setOnClickListener(this);
- mRevokeButton.setOnClickListener(this);
- }
- // Invoke GoogleApiClient.connect during Activity.onStart .
- @Override
- protected void onStart() {
- super.onStart();
- mGoogleApiClient.connect();
- }
- // Invoke GoogleApiClient.disconnect during Activity.onStop .
- @Override
- protected void onStop() {
- super.onStop();
- mGoogleApiClient.disconnect();
- }
- @Override
- public void onConnected(Bundle bundle) {
- // onConnected indicates that an account was selected on the device, that the selected
- // account has granted any requested permissions to our app and that we were able to
- // establish a service connection to Google Play services.
- Log.d(TAG, "onConnected");
- // Retrieve some profile information to personalize our app for the user.
- Person currentUser = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
- Log.e(TAG, "onConnected : " + currentUser.getDisplayName());
- Log.e(TAG, "onConnected : avatar ---> " + currentUser.getImage().getUrl()); // get image
- Log.e(TAG, "onConnected : " + currentUser.getCover().getCoverPhoto().getUrl()); // get image
- Log.e(TAG, "onConnected : " + currentUser.getUrl());
- String email = Plus.AccountApi.getAccountName(mGoogleApiClient); // getemail
- Log.e(TAG, "onConnected : " + email);
- mShouldResolve = false;
- // Show the signed-in UI
- showSignedInUI();
- }
- private void showSignedInUI() {
- mSignInButton.setVisibility(View.INVISIBLE);
- mSignOutButton.setVisibility(View.VISIBLE);
- mRevokeButton.setVisibility(View.VISIBLE);
- Plus.PeopleApi.loadVisible(mGoogleApiClient, null)
- .setResultCallback(this);
- }
- @Override
- public void onConnectionSuspended(int i) {
- }
- @Override
- public void onClick(View v) {
- if (v.getId() == R.id.sign_in_button) {
- onSignInClicked();
- }
- else if(v.getId() == R.id.sign_out_button){
- onSignOutClicked() ;
- }
- else {
- onRevokeClicked() ;
- }
- }
- private void onRevokeClicked() {
- // After we revoke permissions for the user with a GoogleApiClient
- // instance, we must discard it and create a new one.
- if (mGoogleApiClient.isConnected()) {
- Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
- Plus.AccountApi.revokeAccessAndDisconnect(mGoogleApiClient);
- mGoogleApiClient.disconnect();
- }
- showSignedOutUI();
- }
- private void onSignOutClicked() {
- if (mGoogleApiClient.isConnected()) {
- Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
- mGoogleApiClient.disconnect();
- showSignedOutUI();
- }
- }
- private void onSignInClicked() {
- // User clicked the sign-in button, so begin the sign-in process and automatically
- // attempt to resolve any errors that occur.
- mShouldResolve = true;
- mGoogleApiClient.connect();
- }
- @Override
- public void onConnectionFailed(ConnectionResult connectionResult) {
- // Could not connect to Google Play Services. The user needs to select an account,
- // grant permissions or resolve an error in order to sign in. Refer to the javadoc for
- // ConnectionResult to see possible error codes.
- Log.d(TAG, "onConnectionFailed:" + connectionResult);
- if (!mIsResolving && mShouldResolve) {
- if (connectionResult.hasResolution()) {
- try {
- connectionResult.startResolutionForResult(this, RC_SIGN_IN);
- mIsResolving = true;
- } catch (IntentSender.SendIntentException e) {
- Log.e(TAG, "Could not resolve ConnectionResult.", e);
- mIsResolving = false;
- mGoogleApiClient.connect();
- }
- } else {
- // Could not resolve the connection result, show the user an
- // error dialog.
- showErrorDialog(connectionResult);
- }
- } else {
- // Show the signed-out UI
- showSignedOutUI();
- }
- }
- private void showErrorDialog(ConnectionResult connectionResult) {
- }
- private void showSignedOutUI() {
- mSignInButton.setVisibility(View.VISIBLE);
- mSignOutButton.setVisibility(View.INVISIBLE);
- mRevokeButton.setVisibility(View.INVISIBLE);
- }
- @Override
- public void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- Log.d(TAG, "onActivityResult:" + requestCode + ":" + resultCode + ":" + data);
- if (requestCode == RC_SIGN_IN) {
- // If the error resolution was not successful we should not resolve further.
- if (resultCode != RESULT_OK) {
- mShouldResolve = false;
- }
- mIsResolving = false;
- mGoogleApiClient.connect();
- }
- }
- @Override
- public void onResult(People.LoadPeopleResult loadPeopleResult) {
- if (loadPeopleResult.getStatus().getStatusCode() == CommonStatusCodes.SUCCESS) {
- PersonBuffer personBuffer = loadPeopleResult.getPersonBuffer();
- try {
- int count = personBuffer.getCount();
- for (int i = 0; i < count; i++) {
- Log.e("TAG","onResult : "+personBuffer.get(i).getDisplayName()) ;
- }
- } finally {
- personBuffer.close();
- }
- } else {
- Log.e(TAG, "onResult ---> Error requesting visible circles: " + loadPeopleResult.getStatus());
- }
- }
- }
AndroidManifest
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.boxopens.login" >
- <uses-permission<strong> android:name="android.permission.INTERNET" />
- </strong> <uses-permission<strong> android:name="android.permission.GET_ACCOUNTS" />
- </strong> <uses-permission<strong> android:name="android.permission.USE_CREDENTIALS" /></strong>
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name">
- <activity
- android:name=".MainActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <meta-data
- android:name="com.google.android.gms.version"
- android:value="@integer/google_play_services_version" />
- </application>
- </manifest>
UI
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
- android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
- <com.google.android.gms.common.SignInButton
- android:id="@+id/sign_in_button"
- android:layout_width="match_parent"
- android:foregroundGravity="center"
- android:layout_height="wrap_content" />
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Sign-out"
- android:id="@+id/sign_out_button" />
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Revoke"
- android:id="@+id/revoke_button"/>
- </LinearLayout>
- </RelativeLayout>