2016-08-11 47 views
1

我一直在嘗試將Google登錄活動添加到我的應用中,但出於某種原因,我不僅在安裝之前在手機上收到安全警告,雖然登錄彈出窗口工作,但也不登錄。Google登錄安卓無法正常工作,出現安全警告

Security Warning

現在,這裏是我的代碼(根據「Integrating Google Sign-In into Your Android App」教程後,我與我的谷歌帳戶登錄的所有制造。

public class Multiplayer extends AppCompatActivity implements 
    GoogleApiClient.OnConnectionFailedListener, 
    View.OnClickListener { 

private static final String TAG = "SignInActivity"; 
private static final int RC_SIGN_IN = 9001; 

private GoogleApiClient mGoogleApiClient; 
private TextView mStatusTextView; 
private ProgressDialog mProgressDialog; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.content_multiplayer); 

    // Views 
    mStatusTextView = (TextView) findViewById(R.id.status); 

    // Button listeners 
    findViewById(R.id.sign_in_button).setOnClickListener(this); 
    findViewById(R.id.sign_out_button).setOnClickListener(this); 
    findViewById(R.id.disconnect_button).setOnClickListener(this); 

    // [START configure_signin] 
    // Configure sign-in to request the user's ID, email address, and basic 
    // profile. ID and basic profile are included in DEFAULT_SIGN_IN. 
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) 
      .requestEmail() 
      .build(); 
    // [END configure_signin] 

    // [START build_client] 
    // Build a GoogleApiClient with access to the Google Sign-In API and the 
    // options specified by gso. 
    mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */) 
      .addApi(Auth.GOOGLE_SIGN_IN_API, gso) 
      .build(); 
    // [END build_client] 

    // [START customize_button] 
    // Customize sign-in button. The sign-in button can be displayed in 
    // multiple sizes and color schemes. It can also be contextually 
    // rendered based on the requested scopes. For example. a red button may 
    // be displayed when Google+ scopes are requested, but a white button 
    // may be displayed when only basic profile is requested. Try adding the 
    // Scopes.PLUS_LOGIN scope to the GoogleSignInOptions to see the 
    // difference. 
    SignInButton signInButton = (SignInButton) findViewById(R.id.sign_in_button); 
    signInButton.setSize(SignInButton.SIZE_STANDARD); 
    signInButton.setScopes(gso.getScopeArray()); 
    // [END customize_button] 
} 

@Override 
public void onStart() { 
    super.onStart(); 

    OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient); 
    if (opr.isDone()) { 
     // If the user's cached credentials are valid, the OptionalPendingResult will be "done" 
     // and the GoogleSignInResult will be available instantly. 
     Log.d(TAG, "Got cached sign-in"); 
     GoogleSignInResult result = opr.get(); 
     handleSignInResult(result); 
    } else { 
     // If the user has not previously signed in on this device or the sign-in has expired, 
     // this asynchronous branch will attempt to sign in the user silently. Cross-device 
     // single sign-on will occur in this branch. 
     showProgressDialog(); 
     opr.setResultCallback(new ResultCallback<GoogleSignInResult>() { 
      @Override 
      public void onResult(GoogleSignInResult googleSignInResult) { 
       hideProgressDialog(); 
       handleSignInResult(googleSignInResult); 
      } 
     }); 
    } 
} 

// [START onActivityResult] 
@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); 
    } 
} 
// [END onActivityResult] 

// [START handleSignInResult] 
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(acct.getDisplayName()); 
     updateUI(true); 
    } else { 
     // Signed out, show unauthenticated UI. 
     updateUI(false); 
    } 
} 
// [END handleSignInResult] 

// [START signIn] 
private void signIn() { 
    Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); 
    startActivityForResult(signInIntent, RC_SIGN_IN); 
} 
// [END signIn] 

// [START signOut] 
private void signOut() { 
    Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
      new ResultCallback<Status>() { 
       @Override 
       public void onResult(Status status) { 
        // [START_EXCLUDE] 
        updateUI(false); 
        // [END_EXCLUDE] 
       } 
      }); 
} 
// [END signOut] 

// [START revokeAccess] 
private void revokeAccess() { 
    Auth.GoogleSignInApi.revokeAccess(mGoogleApiClient).setResultCallback(
      new ResultCallback<Status>() { 
       @Override 
       public void onResult(Status status) { 
        // [START_EXCLUDE] 
        updateUI(false); 
        // [END_EXCLUDE] 
       } 
      }); 
} 
// [END revokeAccess] 

@Override 
public void onConnectionFailed(ConnectionResult connectionResult) { 
    // An unresolvable error has occurred and Google APIs (including Sign-In) will not 
    // be available. 
    Log.d(TAG, "onConnectionFailed:" + connectionResult); 
} 

private void showProgressDialog() { 
    if (mProgressDialog == null) { 
     mProgressDialog = new ProgressDialog(this); 
     mProgressDialog.setMessage("LOADING"); 
     mProgressDialog.setIndeterminate(true); 
    } 

    mProgressDialog.show(); 
} 

private void hideProgressDialog() { 
    if (mProgressDialog != null && mProgressDialog.isShowing()) { 
     mProgressDialog.hide(); 
    } 
} 

private void updateUI(boolean signedIn) { 
    if (signedIn) { 
     findViewById(R.id.sign_in_button).setVisibility(View.GONE); 
     // findViewById(R.id.sign_out_and_disconnect).setVisibility(View.VISIBLE); 
     findViewById(R.id.comments).setVisibility(View.VISIBLE); 
    } else { 
     mStatusTextView.setText("SIGNED OUT"); 
     findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE); 
     // findViewById(R.id.sign_out_and_disconnect).setVisibility(View.GONE); 
     findViewById(R.id.comments).setVisibility(View.GONE); 
    } 
} 

@Override 
public void onClick(View v) { 
    switch (v.getId()) { 
     case R.id.sign_in_button: 
      signIn(); 
      break; 
     case R.id.sign_out_button: 
      signOut(); 
      break; 
     case R.id.disconnect_button: 
      revokeAccess(); 
      break; 
    } 
} 
} 

,應用程序就如同我沒有這樣做

有人能告訴我什麼我做錯了

編輯:?

提供的解決方案rakesh kashyap確實解決了安全警告,但我仍然無法登錄。任何人都可以提供幫助嗎?

+0

有你設置你的應用程序ID吧? –

+0

@rakeshkashyap這是什麼意思? – user6181605

回答

0

在開始在自己的應用中集成Google登錄之前,您必須先配置Google API控制檯項目並設置您的Android Studio項目。

請按照此URL中的步驟獲取json文件並將其添加到您的Android Studio/app文件夾中。

https://developers.google.com/identity/sign-in/android/start-integrating

由於您沒有設置您的谷歌控制檯上的應用程序,你所看到的安全警告..

+0

謝謝,我正在設置它,我會盡快更新你。 – user6181605

+0

它確實解決了安全警告問題,但我仍然無法登錄(無錯誤消息)。你能幫忙嗎? – user6181605

+0

對不起本週工作阻止。你是否嘗試調試你的代碼?你能讓我知道它的失敗嗎? –