我正在尝试使用最新的Google API 我根据Google的建议设置了所有依赖项。
compile 'com.google.android.gms:play-services:9.0.0'
还创建了一个google-service.json
在控制台中启用Google API ...
我认为一切都应该有效。
我可以使用Google参考和意图登录
但在收到回电后
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if (result.isSuccess()) {
GoogleSignInAccount acct = result.getSignInAccount();
if (listener != null) {
listener.onSuccess(acct);
}
}
GoogleSignInAccount 中的所有属性值均为 null 。
this.mGoogleSignInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
//.requestScopes(new Scope(Scopes.PLUS_LOGIN)) // "https://www.googleapis.com/auth/plus.login"
//.requestScopes(new Scope(Scopes.PLUS_ME)) // "https://www.googleapis.com/auth/plus.me"
//.requestScopes(new Scope(Scopes.EMAIL))
//.requestScopes(new Scope(Scopes.PROFILE))
//.requestEmail()
//.requestProfile()
.build();
this.mGoogleApiClient = new GoogleApiClient.Builder(mActivity)
.enableAutoManage((FragmentActivity) mActivity /* FragmentActivity */, this /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API)
.build();
您应该可以使用 requestEmail 或范围。
这是最新Google API的问题吗?
答案 0 :(得分:3)
尝试使用:
public class GooglePlusLoginUtils implements ConnectionCallbacks, OnConnectionFailedListener,OnClickListener {
private String TAG = "GooglePlusLoginUtils";
/* Request code used to invoke sign in user interactions. */
private static final int RC_SIGN_IN = 0;
private static final int PROFILE_PIC_SIZE = 400;
public static final String NAME = "name";
public static final String EMAIL = "email";
public static final String PHOTO = "photo";
public static final String PROFILE= "profile";
/* Client used to interact with Google APIs. */
private GoogleApiClient mGoogleApiClient;
private boolean mIntentInProgress;
private boolean mSignInClicked;
private ConnectionResult mConnectionResult;
private SignInButton btnSignIn;
private Context ctx;
private GPlusLoginStatus loginstatus;
public interface GPlusLoginStatus{
public void OnSuccessGPlusLogin(Bundle profile);
}
public GooglePlusLoginUtils(Context ctx,int btnRes){
Log.i(TAG, "GooglePlusLoginUtils");
this.ctx= ctx;
this.btnSignIn =(SignInButton) ((Activity)ctx).findViewById(btnRes);
btnSignIn.setOnClickListener(this);
// Initializing google plus api client
mGoogleApiClient = new GoogleApiClient.Builder(ctx)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_LOGIN).build();
}
public void setLoginStatus(GPlusLoginStatus loginStatus){
this.loginstatus = loginStatus;
}
@Override
public void onConnectionFailed(ConnectionResult result) {
Log.i(TAG, "onConnectionFailed");
Log.i(TAG,"Error Code "+ result.getErrorCode());
if (!result.hasResolution()) {
GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), (Activity)ctx,0).show();
return;
}
if (!mIntentInProgress) {
// Store the ConnectionResult for later usage
mConnectionResult = result;
if (mSignInClicked) {
// The user has already clicked 'sign-in' so we attempt to
// resolve all
// errors until the user is signed in, or they cancel.
resolveSignInError();
}
}
}
public void setSignInClicked(boolean value){
mSignInClicked =value;
}
public void setIntentInProgress(boolean value){
mIntentInProgress = value;
}
public void connect(){
mGoogleApiClient.connect();
}
public void reconnect(){
if (!mGoogleApiClient.isConnecting()) {
mGoogleApiClient.connect();
}
}
public void disconnect(){
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
private void signInWithGplus() {
Log.i(TAG, "signInWithGplus");
if (!mGoogleApiClient.isConnecting()) {
mSignInClicked = true;
resolveSignInError();
}
}
private void resolveSignInError() {
Log.i(TAG, "resolveSignInError");
if (mConnectionResult.hasResolution()) {
try {
mIntentInProgress = true;
mConnectionResult.startResolutionForResult((Activity)ctx, RC_SIGN_IN);
} catch (SendIntentException e) {
mIntentInProgress = false;
mGoogleApiClient.connect();
}
}
}
@Override
public void onConnected(Bundle arg0) {
Log.i(TAG, "onConnected");
mSignInClicked = false;
Toast.makeText(ctx, "User is connected!", Toast.LENGTH_LONG).show();
// Get user's information
getProfileInformation();
}
@Override
public void onConnectionSuspended(int arg0) {
Log.i(TAG, "onConnectionSuspended");
mGoogleApiClient.connect();
}
private void getProfileInformation() {
Log.i(TAG, "getProfileInformation");
try {
if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
Person currentPerson = Plus.PeopleApi
.getCurrentPerson(mGoogleApiClient);
String personName = currentPerson.getDisplayName();
String personPhotoUrl = currentPerson.getImage().getUrl();
String personGooglePlusProfile = currentPerson.getUrl();
String email = Plus.AccountApi.getAccountName(mGoogleApiClient);
Log.e(TAG, "Name: " + personName + ", plusProfile: "
+ personGooglePlusProfile + ", email: " + email
+ ", Image: " + personPhotoUrl);
// by default the profile url gives 50x50 px image only
// we can replace the value with whatever dimension we want by
// replacing sz=X
personPhotoUrl = personPhotoUrl.substring(0,
personPhotoUrl.length() - 2)
+ PROFILE_PIC_SIZE;
Bundle profile = new Bundle();
profile.putString(NAME, personName);
profile.putString(EMAIL, email);
profile.putString(PHOTO, personPhotoUrl);
profile.putString(PROFILE, personGooglePlusProfile);
loginstatus.OnSuccessGPlusLogin(profile);
// new LoadProfileImage(imgProfilePic).execute(personPhotoUrl);
} else {
Toast.makeText(ctx,
"Person information is null", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onClick(View v) {
signInWithGplus();
}
public void onActivityResult(int requestCode,int responseCode,Intent intent){
if (requestCode == RC_SIGN_IN) {
if (responseCode != ((Activity)ctx).RESULT_OK) {
setSignInClicked(false);
}
setIntentInProgress(false);
reconnect();
}
}
}
您的主要活动:
public class LoginActivity extends ActionBarActivity implements GooglePlusLoginUtils.GPlusLoginStatus {
private String TAG = "LoginActivity";
private GooglePlusLoginUtils gLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
gLogin = new GooglePlusLoginUtils(this, R.id.activity_login_gplus);
gLogin.setLoginStatus(this);
}
@Override
protected void onStart() {
super.onStart();
gLogin.connect();
}
@Override
protected void onStop() {
super.onStop();
gLogin.disconnect();
}
@Override
protected void onActivityResult(int requestCode, int responseCode,
Intent intent) {
gLogin.onActivityResult(requestCode, responseCode, intent);
}
@Override
public void OnSuccessGPlusLogin(Bundle profile) {
Log.i(TAG,profile.getString(GooglePlusLoginUtils.NAME));
Log.i(TAG,profile.getString(GooglePlusLoginUtils.EMAIL));
Log.i(TAG,profile.getString(GooglePlusLoginUtils.PHOTO));
Log.i(TAG,profile.getString(GooglePlusLoginUtils.PROFILE));
}
}
确保您已在开发者控制台中启用Google+登录并在应用目录中添加JSON文件!
答案 1 :(得分:0)
要为我解决此问题,我必须在firebase项目设置中设置调试和生产用的SHA证书指纹,并在进行更改后将新的google-services.json下载到我的应用中。