Thứ Tư, 29 tháng 7, 2015

Đăng nhập bằng cách liên kết tài khoản google+

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 . . . . 
  1. C:\Program Files\Java\jdk1.7.0_75\bin
rồi gõ 
keytool -exportcert -alias androiddebugkey -keystore%HOMEPATH%/.android/debug.keystore -list -v
pass 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
Bước 4 : Code 
Các Bạn Cần Thay đổi những phần sau 
MainActivity + AndroidManifest + UI + Build.gradle 
Build.gradle
  1. dependencies {
  2. compile fileTree(dir: 'libs', include: ['*.jar'])
  3. compile 'com.android.support:appcompat-v7:22.2.1'
  4. <strong>compile 'com.google.android.gms:play-services:7.5.0'</strong>
  5. }

MainActivity
  1. import android.app.Activity;
  2. import android.content.Intent;
  3. import android.content.IntentSender;
  4. import android.os.Bundle;
  5. import android.util.Log;
  6. import android.view.View;
  7. import android.widget.Button;
  8.  
  9. import com.google.android.gms.common.ConnectionResult;
  10. import com.google.android.gms.common.Scopes;
  11. import com.google.android.gms.common.SignInButton;
  12. import com.google.android.gms.common.api.CommonStatusCodes;
  13. import com.google.android.gms.common.api.GoogleApiClient;
  14. import com.google.android.gms.common.api.ResultCallback;
  15. import com.google.android.gms.common.api.Scope;
  16. import com.google.android.gms.plus.People;
  17. import com.google.android.gms.plus.Plus;
  18. import com.google.android.gms.plus.model.people.Person;
  19. import com.google.android.gms.plus.model.people.PersonBuffer;
  20.  
  21.  
  22. public class MainActivity extends Activity implements
  23. GoogleApiClient.ConnectionCallbacks,
  24. GoogleApiClient.OnConnectionFailedListener,
  25. View.OnClickListener, ResultCallback&lt;People.LoadPeopleResult&gt; {
  26.  
  27. private String TAG = "MAINACTIVITY" ;
  28.  
  29. /* Request code used to invoke sign in user interactions. */
  30. private static final int RC_SIGN_IN = 0;
  31.  
  32. /* Is there a ConnectionResult resolution in progress? */
  33. private boolean mIsResolving = false;
  34.  
  35. /* Should we automatically resolve ConnectionResults when possible? */
  36. private boolean mShouldResolve = false;
  37.  
  38.  
  39. /* Client used to interact with Google APIs. */
  40. private GoogleApiClient mGoogleApiClient;
  41. private SignInButton mSignInButton;
  42. private Button mSignOutButton;
  43. private Button mRevokeButton ;
  44.  
  45.  
  46. @Override
  47. protected void onCreate(Bundle savedInstanceState) {
  48. super.onCreate(savedInstanceState);
  49. setContentView(R.layout.activity_main);
  50. mSignInButton = (SignInButton) findViewById(R.id.sign_in_button);
  51. mSignOutButton = (Button) findViewById(R.id.sign_out_button);
  52. mSignOutButton.setVisibility(View.INVISIBLE); // hide button SignOut
  53. mRevokeButton = (Button) findViewById(R.id.revoke_button) ;
  54. mRevokeButton.setVisibility(View.INVISIBLE); // hide button revoke
  55. // Build GoogleApiClient with access to basic profile
  56. mGoogleApiClient = new GoogleApiClient.Builder(this)
  57. .addConnectionCallbacks(this)
  58. .addOnConnectionFailedListener(this)
  59. .addApi(Plus.API)
  60. .addScope(new Scope(Scopes.PROFILE))
  61. .build();
  62. mSignInButton.setOnClickListener(this);
  63. mSignOutButton.setOnClickListener(this);
  64. mRevokeButton.setOnClickListener(this);
  65. }
  66.  
  67. // Invoke GoogleApiClient.connect during Activity.onStart .
  68.  
  69. @Override
  70. protected void onStart() {
  71. super.onStart();
  72. mGoogleApiClient.connect();
  73. }
  74.  
  75. // Invoke GoogleApiClient.disconnect during Activity.onStop .
  76.  
  77. @Override
  78. protected void onStop() {
  79. super.onStop();
  80. mGoogleApiClient.disconnect();
  81. }
  82.  
  83. @Override
  84. public void onConnected(Bundle bundle) {
  85. // onConnected indicates that an account was selected on the device, that the selected
  86. // account has granted any requested permissions to our app and that we were able to
  87. // establish a service connection to Google Play services.
  88. Log.d(TAG, "onConnected");
  89. // Retrieve some profile information to personalize our app for the user.
  90. Person currentUser = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
  91. Log.e(TAG, "onConnected : " + currentUser.getDisplayName());
  92. Log.e(TAG, "onConnected : avatar ---&gt; " + currentUser.getImage().getUrl()); // get image
  93. Log.e(TAG, "onConnected : " + currentUser.getCover().getCoverPhoto().getUrl()); // get image
  94. Log.e(TAG, "onConnected : " + currentUser.getUrl());
  95. String email = Plus.AccountApi.getAccountName(mGoogleApiClient); // getemail
  96. Log.e(TAG, "onConnected : " + email);
  97. mShouldResolve = false;
  98.  
  99. // Show the signed-in UI
  100. showSignedInUI();
  101.  
  102. }
  103.  
  104. private void showSignedInUI() {
  105. mSignInButton.setVisibility(View.INVISIBLE);
  106. mSignOutButton.setVisibility(View.VISIBLE);
  107. mRevokeButton.setVisibility(View.VISIBLE);
  108.  
  109. Plus.PeopleApi.loadVisible(mGoogleApiClient, null)
  110. .setResultCallback(this);
  111. }
  112.  
  113. @Override
  114. public void onConnectionSuspended(int i) {
  115.  
  116. }
  117.  
  118. @Override
  119. public void onClick(View v) {
  120. if (v.getId() == R.id.sign_in_button) {
  121. onSignInClicked();
  122. }
  123. else if(v.getId() == R.id.sign_out_button){
  124. onSignOutClicked() ;
  125. }
  126. else {
  127. onRevokeClicked() ;
  128. }
  129.  
  130. }
  131.  
  132. private void onRevokeClicked() {
  133. // After we revoke permissions for the user with a GoogleApiClient
  134. // instance, we must discard it and create a new one.
  135.  
  136. if (mGoogleApiClient.isConnected()) {
  137. Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
  138. Plus.AccountApi.revokeAccessAndDisconnect(mGoogleApiClient);
  139. mGoogleApiClient.disconnect();
  140. }
  141. showSignedOutUI();
  142. }
  143.  
  144.  
  145. private void onSignOutClicked() {
  146. if (mGoogleApiClient.isConnected()) {
  147. Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
  148. mGoogleApiClient.disconnect();
  149. showSignedOutUI();
  150. }
  151. }
  152.  
  153. private void onSignInClicked() {
  154. // User clicked the sign-in button, so begin the sign-in process and automatically
  155. // attempt to resolve any errors that occur.
  156.  
  157. mShouldResolve = true;
  158. mGoogleApiClient.connect();
  159. }
  160.  
  161. @Override
  162. public void onConnectionFailed(ConnectionResult connectionResult) {
  163. // Could not connect to Google Play Services. The user needs to select an account,
  164. // grant permissions or resolve an error in order to sign in. Refer to the javadoc for
  165. // ConnectionResult to see possible error codes.
  166. Log.d(TAG, "onConnectionFailed:" + connectionResult);
  167.  
  168. if (!mIsResolving &amp;&amp; mShouldResolve) {
  169. if (connectionResult.hasResolution()) {
  170. try {
  171. connectionResult.startResolutionForResult(this, RC_SIGN_IN);
  172. mIsResolving = true;
  173. } catch (IntentSender.SendIntentException e) {
  174. Log.e(TAG, "Could not resolve ConnectionResult.", e);
  175. mIsResolving = false;
  176. mGoogleApiClient.connect();
  177. }
  178. } else {
  179. // Could not resolve the connection result, show the user an
  180. // error dialog.
  181. showErrorDialog(connectionResult);
  182. }
  183. } else {
  184. // Show the signed-out UI
  185. showSignedOutUI();
  186. }
  187.  
  188. }
  189.  
  190. private void showErrorDialog(ConnectionResult connectionResult) {
  191.  
  192. }
  193.  
  194. private void showSignedOutUI() {
  195. mSignInButton.setVisibility(View.VISIBLE);
  196. mSignOutButton.setVisibility(View.INVISIBLE);
  197. mRevokeButton.setVisibility(View.INVISIBLE);
  198. }
  199.  
  200. @Override
  201. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  202. super.onActivityResult(requestCode, resultCode, data);
  203. Log.d(TAG, "onActivityResult:" + requestCode + ":" + resultCode + ":" + data);
  204.  
  205. if (requestCode == RC_SIGN_IN) {
  206. // If the error resolution was not successful we should not resolve further.
  207. if (resultCode != RESULT_OK) {
  208. mShouldResolve = false;
  209. }
  210. mIsResolving = false;
  211. mGoogleApiClient.connect();
  212. }
  213. }
  214.  
  215.  
  216. @Override
  217. public void onResult(People.LoadPeopleResult loadPeopleResult) {
  218. if (loadPeopleResult.getStatus().getStatusCode() == CommonStatusCodes.SUCCESS) {
  219.  
  220. PersonBuffer personBuffer = loadPeopleResult.getPersonBuffer();
  221. try {
  222. int count = personBuffer.getCount();
  223. for (int i = 0; i &lt; count; i++) {
  224. Log.e("TAG","onResult : "+personBuffer.get(i).getDisplayName()) ;
  225. }
  226. } finally {
  227. personBuffer.close();
  228. }
  229. } else {
  230. Log.e(TAG, "onResult ---&gt; Error requesting visible circles: " + loadPeopleResult.getStatus());
  231. }
  232. }
  233. }
AndroidManifest
  1. &lt;?xml version="1.0" encoding="utf-8"?&gt;
  2. &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.boxopens.login" &gt;
  4. &lt;uses-permission<strong> android:name="android.permission.INTERNET" /&gt;
  5. </strong> &lt;uses-permission<strong> android:name="android.permission.GET_ACCOUNTS" /&gt;
  6. </strong> &lt;uses-permission<strong> android:name="android.permission.USE_CREDENTIALS" /&gt;</strong>
  7. &lt;application
  8. android:allowBackup="true"
  9. android:icon="@mipmap/ic_launcher"
  10. android:label="@string/app_name"&gt;
  11. &lt;activity
  12. android:name=".MainActivity"
  13. android:label="@string/app_name" &gt;
  14. &lt;intent-filter&gt;
  15. &lt;action android:name="android.intent.action.MAIN" /&gt;
  16.  
  17. &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
  18. &lt;/intent-filter&gt;
  19. &lt;/activity&gt;
  20. &lt;meta-data
  21. android:name="com.google.android.gms.version"
  22. android:value="@integer/google_play_services_version" /&gt;
  23. &lt;/application&gt;
  24.  
  25. &lt;/manifest&gt;
UI
  1. &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
  3. android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
  4. android:paddingRight="@dimen/activity_horizontal_margin"
  5. android:paddingTop="@dimen/activity_vertical_margin"
  6. android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"&gt;
  7.  
  8. &lt;com.google.android.gms.common.SignInButton
  9. android:id="@+id/sign_in_button"
  10. android:layout_width="match_parent"
  11. android:foregroundGravity="center"
  12. android:layout_height="wrap_content" /&gt;
  13.  
  14. &lt;LinearLayout
  15. android:layout_width="match_parent"
  16. android:layout_height="match_parent"
  17. android:orientation="vertical"&gt;
  18. &lt;Button
  19. android:layout_width="match_parent"
  20. android:layout_height="wrap_content"
  21. android:text="Sign-out"
  22.  
  23. android:id="@+id/sign_out_button" /&gt;
  24.  
  25. &lt;Button
  26. android:layout_width="match_parent"
  27. android:layout_height="wrap_content"
  28. android:text="Revoke"
  29. android:id="@+id/revoke_button"/&gt;
  30. &lt;/LinearLayout&gt;
  31.  
  32.  
  33. &lt;/RelativeLayout&gt;

Không có nhận xét nào:

Đăng nhận xét