gpt4 book ai didi

android - GoogleSignInAccount 返回 null

转载 作者:太空宇宙 更新时间:2023-11-03 13:48:57 24 4
gpt4 key购买 nike

我正在尝试使用最新的 Google API。
我按照 Google 的建议设置了所有依赖项。

编译 'com.google.android.gms:play-services:9.0.0'

并且还创建了一个 google-service.json。
在控制台中启用 Google API...
我认为一切都应该有效。我可以使用 Google 引用和 Intent 登录。
但是得到回调后

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

您应该能够使用requestEmailscope
这是最新 Google API 的问题吗?

最佳答案

尝试使用这个:

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


}

你的主要 Activity :

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

确保您已在 Developers Console 中启用 Google+ 登录并在您的应用程序目录中添加 JSON 文件!

关于android - GoogleSignInAccount 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37506840/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com