Google + 加号在 android 6 版本后无法使用
Google + plus sign in not working after android 6 release
所以,在 android 6 marshmallow 发布之前,google + 登录工作得很好。发布后,我遇到权限、onConnectionFailed 等问题...有人有解决方案吗?我正在从片段中调用 google 登录方法。这是代码:
片段:
@Override
public void onConnected(Bundle bundle) {
Log.d("GoogleApiClient", "onConnected");
mShouldResolve = false;
new RetrieveTokenTask().execute(Plus.AccountApi.getAccountName(mGoogleApiClient));
}
@Override
public void onConnectionSuspended(int i) {
Log.d("GoogleApiClient", "onConnectionSuspended " + String.valueOf(i));
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.d("GoogleApiClient", "onConnectionFailed");
Log.d("GoogleApiCLient", connectionResult.toString());
ViewUtils.hideProgressDialog();
if (!mIsResolving && mShouldResolve) {
if (connectionResult.hasResolution()) {
try {
connectionResult.startResolutionForResult(getActivity(), RC_SIGN_IN);
mIsResolving = true;
} catch (IntentSender.SendIntentException e) {
Log.e("LoginFragment", "Could not resolve ConnectionResult.", e);
mIsResolving = false;
mGoogleApiClient.connect();
}
}
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.i("RequestCode", String.valueOf(requestCode));
Log.i("ResultCode", String.valueOf(resultCode));
try {
Log.i("Intent", data.getAction());
} catch (Exception e) {
}
if (requestCode == RC_SIGN_IN) {
// If the error resolution was not successful we should not resolve further.
ViewUtils.showProgressDialog(getActivity(), getActivity().getResources().getString(R.string.loading_dialog_str));
if (resultCode != getActivity().RESULT_OK) {
mShouldResolve = false;
}
mIsResolving = false;
mGoogleApiClient.connect();
if (mGoogleApiClient.isConnected()) {
new RetrieveTokenTask().execute(Plus.AccountApi.getAccountName(mGoogleApiClient));
}
} else {
//facebook
Log.d("Login", "FaceBook");
mCallbackManagerFacebook.onActivityResult(requestCode, resultCode, data);
}
}
private class RetrieveTokenTask extends AsyncTask<String, Void, String> {
private static final String TAG = "RetrieveAccessToken";
private static final int REQ_SIGN_IN_REQUIRED = 55664;
@Override
protected String doInBackground(String... params) {
String accountName = params[0];
String scopes = "oauth2:profile";
String token = null;
try {
token = GoogleAuthUtil.getToken(getActivity(), accountName, scopes);
} catch (IOException e) {
Log.e(TAG, e.getMessage());
} catch (UserRecoverableAuthException e) {
startActivityForResult(e.getIntent(), REQ_SIGN_IN_REQUIRED);
} catch (GoogleAuthException e) {
Log.e(TAG, e.getMessage());
}
return token;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Log.d("Token", "Token Value: " + s);
if (s == null) {
ViewUtils.hideProgressDialog();
ViewUtils.showToastMessage(getActivity(), getActivity().getResources().getString(R.string.login_error));
} else {
loginGoogleUser(s);
}
if (mGoogleApiClient.isConnected()) {
Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
mGoogleApiClient.disconnect();
}
}
}
和结果活动:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
LoginFragment loginFragment = (LoginFragment) getSupportFragmentManager().findFragmentByTag("MyProfileFragment");
if (loginFragment != null) {
loginFragment.onActivityResult(requestCode, resultCode, data);
}
}
好的,所以,首先,您需要将 google 播放服务的所有依赖项更改为 8.1.0,同时 build.gradle 更改为最新版本,目标 sdk 23,编译版本 23 等。因此,基本上,请检查您的 gradle。之后,您需要将 mGoogleApiClient 更改为:
mGoogleApiClient = new GoogleApiClient.Builder(getActivity()).addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Plus.API)
.addScope(new Scope(Scopes.PROFILE))
.addScope(new Scope(Scopes.EMAIL))
.addScope(new Scope(Scopes.PLUS_LOGIN))
.addScope(new Scope(Scopes.PLUS_ME))
.build();
mGoogleApiClient.connect();
最后,您需要包含 sdk>=23 所需的权限。为此,创建
private static String ACCOUNT_PERMISSIONS[] = new String[]{
Manifest.permission.GET_ACCOUNTS
};
这里有一篇关于如何获取 sdk >= 23 权限的好教程:
https://github.com/googlesamples/android-RuntimePermissions
所以,在 android 6 marshmallow 发布之前,google + 登录工作得很好。发布后,我遇到权限、onConnectionFailed 等问题...有人有解决方案吗?我正在从片段中调用 google 登录方法。这是代码:
片段:
@Override
public void onConnected(Bundle bundle) {
Log.d("GoogleApiClient", "onConnected");
mShouldResolve = false;
new RetrieveTokenTask().execute(Plus.AccountApi.getAccountName(mGoogleApiClient));
}
@Override
public void onConnectionSuspended(int i) {
Log.d("GoogleApiClient", "onConnectionSuspended " + String.valueOf(i));
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.d("GoogleApiClient", "onConnectionFailed");
Log.d("GoogleApiCLient", connectionResult.toString());
ViewUtils.hideProgressDialog();
if (!mIsResolving && mShouldResolve) {
if (connectionResult.hasResolution()) {
try {
connectionResult.startResolutionForResult(getActivity(), RC_SIGN_IN);
mIsResolving = true;
} catch (IntentSender.SendIntentException e) {
Log.e("LoginFragment", "Could not resolve ConnectionResult.", e);
mIsResolving = false;
mGoogleApiClient.connect();
}
}
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.i("RequestCode", String.valueOf(requestCode));
Log.i("ResultCode", String.valueOf(resultCode));
try {
Log.i("Intent", data.getAction());
} catch (Exception e) {
}
if (requestCode == RC_SIGN_IN) {
// If the error resolution was not successful we should not resolve further.
ViewUtils.showProgressDialog(getActivity(), getActivity().getResources().getString(R.string.loading_dialog_str));
if (resultCode != getActivity().RESULT_OK) {
mShouldResolve = false;
}
mIsResolving = false;
mGoogleApiClient.connect();
if (mGoogleApiClient.isConnected()) {
new RetrieveTokenTask().execute(Plus.AccountApi.getAccountName(mGoogleApiClient));
}
} else {
//facebook
Log.d("Login", "FaceBook");
mCallbackManagerFacebook.onActivityResult(requestCode, resultCode, data);
}
}
private class RetrieveTokenTask extends AsyncTask<String, Void, String> {
private static final String TAG = "RetrieveAccessToken";
private static final int REQ_SIGN_IN_REQUIRED = 55664;
@Override
protected String doInBackground(String... params) {
String accountName = params[0];
String scopes = "oauth2:profile";
String token = null;
try {
token = GoogleAuthUtil.getToken(getActivity(), accountName, scopes);
} catch (IOException e) {
Log.e(TAG, e.getMessage());
} catch (UserRecoverableAuthException e) {
startActivityForResult(e.getIntent(), REQ_SIGN_IN_REQUIRED);
} catch (GoogleAuthException e) {
Log.e(TAG, e.getMessage());
}
return token;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Log.d("Token", "Token Value: " + s);
if (s == null) {
ViewUtils.hideProgressDialog();
ViewUtils.showToastMessage(getActivity(), getActivity().getResources().getString(R.string.login_error));
} else {
loginGoogleUser(s);
}
if (mGoogleApiClient.isConnected()) {
Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
mGoogleApiClient.disconnect();
}
}
}
和结果活动:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
LoginFragment loginFragment = (LoginFragment) getSupportFragmentManager().findFragmentByTag("MyProfileFragment");
if (loginFragment != null) {
loginFragment.onActivityResult(requestCode, resultCode, data);
}
}
好的,所以,首先,您需要将 google 播放服务的所有依赖项更改为 8.1.0,同时 build.gradle 更改为最新版本,目标 sdk 23,编译版本 23 等。因此,基本上,请检查您的 gradle。之后,您需要将 mGoogleApiClient 更改为:
mGoogleApiClient = new GoogleApiClient.Builder(getActivity()).addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Plus.API)
.addScope(new Scope(Scopes.PROFILE))
.addScope(new Scope(Scopes.EMAIL))
.addScope(new Scope(Scopes.PLUS_LOGIN))
.addScope(new Scope(Scopes.PLUS_ME))
.build();
mGoogleApiClient.connect();
最后,您需要包含 sdk>=23 所需的权限。为此,创建
private static String ACCOUNT_PERMISSIONS[] = new String[]{
Manifest.permission.GET_ACCOUNTS
};
这里有一篇关于如何获取 sdk >= 23 权限的好教程: https://github.com/googlesamples/android-RuntimePermissions