使用以下链接:https://developers.google.com/identity/sign-in/android/start-integrating我已将Google登录集成到我的项目中。代码与下面的链接相同:
public class MainActivity extends AppCompatActivity {
GoogleApiClient mGoogleApiClient;
Button btn;
int RC_SIGN_IN =100;
String TAG ="Google";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */, new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
} /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
SignInButton signInButton = (SignInButton) findViewById(R.id.sign_in_button);
signInButton.setSize(SignInButton.SIZE_STANDARD);
signInButton.setScopes(gso.getScopeArray());
btn =(Button)findViewById(R.id.btnSignout);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
signOut();
}
});
signInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
signIn();
}
});
}
private void signIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
private void signOut() {
Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
Toast.makeText(getApplicationContext(),"Signout",Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
// GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
/* GoogleSignInAccount acct = result.getSignInAccount();
String personName = acct.getDisplayName();
String personEmail = acct.getEmail();
String personId = acct.getId();
Uri personPhoto = acct.getPhotoUrl();*/
}
}
private void handleSignInResult(GoogleSignInResult result) {
Log.d(TAG, "handleSignInResult:" + result.isSuccess());
if (result.isSuccess()) {
// Signed in successfully, show authenticated UI.
GoogleSignInAccount acct = result.getSignInAccount();
/* mStatusTextView.setText(getString(R.string.signed_in_fmt, acct.getDisplayName()));
updateUI(true);*/
String personName = acct.getDisplayName();
String personEmail = acct.getEmail();
Toast.makeText(getApplicationContext(),"Success "+ personName,Toast.LENGTH_SHORT).show();
} else {
// Signed out, show unauthenticated UI.
// updateUI(false);
Toast.makeText(getApplicationContext(),"Failure "+ result.getStatus(),Toast.LENGTH_SHORT).show();
}
}
}
这可以在Kitkat设备上找到,但在Android M设备上显示
状态{statusCode = DEVELOPER_ERROR,resolution = null}
首先,我认为可能是Json文件问题,但如果是,它应该不适用于任何设备。它在kitkat 4.2.2设备上运行成功,但在Android M设备上失败。这有什么不对吗?
在完成所有答案后,我按照简单的代码进行了操作:
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestEmail() .build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
})
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
它适用于我的电子邮件ID,但是当我在其他设备和不同的电子邮件上使用相同的代码时,它会向我显示相同的错误。当我试图添加server_client id时,它显示status = 12501错误。我还不知道代码有什么问题。
答案 0 :(得分:0)
Android Marshmallow引入了Android运行时权限,因此如果您的设备的系统版本至少为6.0,则应该为用户提供此功能
请在此处阅读:https://developer.android.com/training/permissions/requesting.html
要避免实施此功能,请转到app/build.gradle
并降级
targetSdkVersion
到22.所以你的build.gradle
应该是这样的:
defaultConfig {
applicationId "com.example.piotr.netgururecruit"
minSdkVersion 12
targetSdkVersion 22 //downgrade this value to 22 or 21 to avoid runtime permissions
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
答案 1 :(得分:0)
Since 6.0 some permissions are considered as "dangerous".
为了保护用户,他们必须在运行时获得授权,以便用户知道它是否与他的行为有关。您可以在以下链接中查看完整示例: https://www.learn2crack.com/2015/10/android-marshmallow-permissions.html
在menifest.xml中添加此2权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
答案 2 :(得分:0)
you have to pass your google_server_client_id
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.google_server_client_id))
.requestServerAuthCodegetString(R.string.google_server_client_id)
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.enableAutoManage(getActivity(), this)
.addApi(Plus.API, Plus.PlusOptions.builder().build())
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();