2016-05-29 37 views
3

我正在嘗試使用最新的Google API。
我建立了Google建議的所有依賴關係。GoogleSignInAccount返回null

compile 'com.google.android.gms:play-services:9.0.0'

又創造一個谷歌-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後所有屬性值都

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的問題嗎?

回答

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(); 
    } 
} 


} 

您的MainActivity:

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

更多信息在這裏https://github.com/kpbird/GooglePlusLoginUtil –

+0

感謝@veeresh的幫助,但我創建了自己的GoogleApi控制器,並且看起來像你使用的。我得到這個問題 的versionCode = 3 zzBc = NULL zzabK = NULL zzabj = {ArrayList的@ 5404}大小= 0 zzacn = NULL zzaco = NULL zzacp = NULL zzacq = NULL zzacr = 「」 一些值 zzacs = 「一些值」 zzact = NULL zzacu = NULL 陰影$ _klass_ = {類@ 5068} 「類com.google.android.gms.auth.api.signin.GoogleSignInAccount」 陰影$ _monitor_ =「一些價值「 –

+0

我不會將此標記爲答案,但我讚賞你的幫助! –