我是Stack new溢出和android开发的新手。在我的应用程序中,添加google plus和facebook登录集成。我在google plus登录时遇到问题。我正在尝试的是......... GooglePlusSignInHelper.java
public class GooglePlusSignInHelper {
private final static String TAG = "GooglePlusSignInHelper";
public final static int RC_SIGN_IN = 100;
private static GoogleSignInOptions gso;
private static GooglePlusSignInHelper sInstance;
private static String webClientID;
private GoogleApiClient googleApiClient;
private Context context;
private OnGoogleSignInListener loginResultCallback;
private ResultCallback<Status> logoutResultCallback;
/**
* This method should be called before calling any instance.
* This is neccessary to get access token and id of user.
* @param googleClientId
*/
public static void setClientID(String googleClientId) {
webClientID = googleClientId;
}
/**
* Interface to listen the Google login
*/
public interface OnGoogleSignInListener {
void OnGSignSuccess(GoogleSignInAccount googleSignInAccount, @Nullable Person person);
void OnGSignError(GoogleSignInResult errorMessage);
}
public static GooglePlusSignInHelper getInstance() {
if (sInstance == null) {
sInstance = new GooglePlusSignInHelper();
}
return sInstance;
}
private GooglePlusSignInHelper() {
// Configure sign-in to request the user's ID, email address, and basic
// profile. ID and basic profile are included in DEFAULT_SIGN_IN.
gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestScopes(new Scope(Scopes.PLUS_LOGIN))
.requestProfile() //for profile related info
.requestEmail() //for email
.requestIdToken(webClientID) //for accessToken and id
.build();
}
public void initialize(FragmentActivity activity, OnGoogleSignInListener onGoogleSignInListener)
{
loginResultCallback = onGoogleSignInListener;
context = activity;
googleApiClient = new GoogleApiClient.Builder(activity)
.enableAutoManage(activity /* FragmentActivity */, new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.e(TAG, "onConnectionFailed: " + connectionResult);
}
} /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.addApi(Plus.API)
.build();
googleApiClient.registerConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
Log.i(TAG, "onConnected");
}
@Override
public void onConnectionSuspended(int i) {
Log.i(TAG, "onConnectionSuspended");
}
});
googleApiClient.registerConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.i(TAG, "onConnectionFailed");
}
});
}
public boolean isConnected() {
boolean isConnected = googleApiClient.isConnected();
Log.i(TAG, "isConnected()" + isConnected);
return isConnected;
}
public void signIn(Activity activity) {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(googleApiClient);
activity.startActivityForResult(signInIntent, RC_SIGN_IN);
}
public void signOut() {
Auth.GoogleSignInApi.signOut(googleApiClient).setResultCallback(
new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
if (logoutResultCallback != null) {
logoutResultCallback.onResult(status);
}
}
});
}
private void handleSignInResult(GoogleSignInResult result) {
Log.d(TAG, "handleSignInResult:" + result.isSuccess());
if (result.isSuccess()) {
Log.i(TAG, "Signed in");
// Signed in successfully, show authenticated UI.
final GoogleSignInAccount acct = result.getSignInAccount();
//This code is just for getting google plus information like gender, birthday, aboutme etc
final Person[] person = {null};
if(acct!=null) {
Plus.PeopleApi.load(googleApiClient, acct.getId()).setResultCallback(new ResultCallback<People.LoadPeopleResult>() {
@Override
public void onResult(@NonNull People.LoadPeopleResult loadPeopleResult) {
try {
person[0] = loadPeopleResult.getPersonBuffer().get(0);
}
finally {
loadPeopleResult.getPersonBuffer().release();
}
if (loginResultCallback != null) {
loginResultCallback.OnGSignSuccess(acct, person[0]);
}
}
});
}
} else {
Log.i(TAG, "Signed out");
if (loginResultCallback != null) {
loginResultCallback.OnGSignError(result);
}
}
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
}
}
public void setLogoutResultCallback(ResultCallback<Status> callback) {
logoutResultCallback = callback;
}
}
MainActivity.java
findViewById(R.id.google).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
googleApi();
gSignInHelper.signIn(MainActivity.this);
}
});
void googleApi(){
GooglePlusSignInHelper.setClientID(GOOGLE_CLIENT_ID);
gSignInHelper = GooglePlusSignInHelper.getInstance();
gSignInHelper.initialize(this, this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent
data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == GooglePlusSignInHelper.RC_SIGN_IN)
{
gSignInHelper.onActivityResult(requestCode,resultCode,data);
}}
@Override
public void OnGSignSuccess(GoogleSignInAccount acct, @Nullable Person person) {
String name = (acct.getDisplayName()==null)?"":acct.getDisplayName();
String email = acct.getEmail();
Log.i(TAG, "OnGSignSuccess: IdToken "+ acct.getIdToken());
if(person!=null) {
int gender = person.getGender();
String genderType = "OTHERS";
if (gender == 0)
genderType = "MALE";
else if (gender == 1)
genderType = "FEMALE";
Log.i(TAG, "OnGSignSuccess: gender "+ genderType);
}
Uri photoUrl = acct.getPhotoUrl();
String profilePic = "";
if(photoUrl!=null)
profilePic = photoUrl.toString();
String id = acct.getId();
Log.i(TAG, "OnGSignSuccess: "+ name);
Log.i(TAG, "OnGSignSuccess: "+ email);
Log.i(TAG, "OnGSignSuccess: "+ id);
if(companyCheck == null){
Intent in =new Intent(MainActivity.this,CompanyActivity.class);
startActivity(in);
finish();
}else{
Intent in =new Intent(MainActivity.this,SecondActivity.class);
startActivity(in);
finish() }}
@Override
public void OnGSignError(GoogleSignInResult errorMessage) {
Log.i(TAG, "OnGSignSuccess: "+errorMessage.toString() );
}
Toast.makeText(getApplicationContext(), "google Sigin Failed", Toast.LENGTH_SHORT).show();
}
onGSignError我把错误信息放在Log中,我得到了这个
error:com.google.android.gms.auth.api.signin.GoogleSignInResult@b81650
依赖关系()
implementation 'com.google.android.gms:play-services-auth:11.8.0'
implementation 'com.google.android.gms:play-services-plus:11.8.0'
我会在youtube中使用引用它们添加两个依赖项。如果删除其中一个,我在导入类中有编译时错误。
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInResult;
import com.google.android.gms.plus.model.people.Person;
任何人都可以帮我解决这个问题。