我需要通过我的应用程序谷歌,加上登录,我登录了,除了棒棒糖所有版本。什么是谷歌的程序加上通过我的应用程序登录...?请给我回复的是实际的程序...
这里是我的代码,到目前为止我已经做...
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.SyncStateContract;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.auth.api.signin.GoogleSignInResult;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.OptionalPendingResult;
import com.google.android.gms.common.api.ResultCallback;
public class GmailSignin extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener{
TextView signInButton;
private GoogleApiClient mGoogleApiClient;
private static final int RC_SIGN_IN = 9001;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
signInButton = (TextView) findViewById(R.id.sign_in_button);
signInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
signIn();
}
});
}
void signIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
@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);
}
}
private void handleSignInResult(GoogleSignInResult result) {
Log.d("Signin", "handleSignInResult:" + result.isSuccess());
if (result.isSuccess()) {
// Signed in successfully, show authenticated UI.
GoogleSignInAccount acct = result.getSignInAccount();
Conatants.name=acct.getDisplayName();
Conatants.email=acct.getEmail();
Conatants.id=acct.getId();
Intent intent=new Intent(getApplicationContext(),Details.class);
startActivity(intent);
} else {
// Signed out, show unauthenticated UI.
}
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
public void 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("gmailsignup", "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.
opr.setResultCallback(new ResultCallback<GoogleSignInResult>() {
@Override
public void onResult(GoogleSignInResult googleSignInResult) {
handleSignInResult(googleSignInResult);
}
});
}
}
}
我没有登录过与Facebook登录谷歌加,但我敢用它模块,可以帮助你。祝你好运与here
(我试图格式化代码就像一个小时,但没有运气,有人用正确的格式编辑答案请)。
实际的答案:
这个工作对我来说它是目前在生产中,因此经过全面的测试
公共类LoginActivity扩展FragmentActivity实现View.OnClickListener,GoogleApiClient.OnConnectionFailedListener {
private GoogleApiClient mGoogleApiClient;
private static final int RC_SIGN_IN = 9001;
private Button signUpGoogle;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_login);
signUpGoogle = (Button) findViewById(R.id.sign_up_google);
signUpGoogle.setOnClickListener(this);
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
}
@Override
public void onClick(View v) {
signInGoogle();
}
private void signInGoogle() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
@Override
protected 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);
handleSignInResultGoogle(result);
}
}
private void handleSignInResultGoogle(GoogleSignInResult result) {
AlertsAndLogs.log(TAG, "handleSignInResult:" + result.isSuccess());
if (result.isSuccess()) {
GoogleSignInAccount acct = result.getSignInAccount();
if (acct != null) {
//Get details from account: acc and do something when them
}
} else {
AlertsAndLogs.toast("SignUp with Google failed " + result.toString());
}
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
AlertsAndLogs.logError(TAG, "onConnectionFailed:" + connectionResult);
}
}
这是我在我的应用程序中使用与谷歌,加上登录修改后的版本。适用于所有版本的API,我支持(14以上)。
总之这个类将执行以下操作。
GoogleApiClient
时,设置了所需的作用域Activity
。onClickListener
。Activtiy
的标志与结果。onActivityResult
。sign inactivity.Java
public class SignInActivity extends AppCompatActivity implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
private static final int SIGN_IN_REQUEST = 101;
private GoogleApiClient mGoogleApiClient;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_in);
initialize();
}
@Override
public void onConnected(@Nullable Bundle bundle) {
// Maybe only enable the sign in button as soon as the api is connected
}
@Override
public void onConnectionSuspended(int i) {
// Check best practices
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
// Check best practices
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SIGN_IN_REQUEST) {
final AppPreference preference = PreferenceFactory.getPreferenceHelper(this);
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if (result != null && result.isSuccess()) {
// DO SOMETHING ON SUCCESS
} else {
// HANDLE FAILED LOGIN
}
}
}
protected void signInClicked() {
//SHOW A PROGRESS BAR OR SOME INDICATION OF SIGN IN
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, SIGN_IN_REQUEST);
}
private void initialize() {
setupGoogleApiClient();
setupToolbar();
setupSignInButton();
}
private Toolbar setupToolbar() {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setTitle(getString(R.string.sign_in_activity));
}
return toolbar;
}
private void setupSignInButton() {
findViewById(R.id.signInButton).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
signInClicked();
}
});
}
private void setupGoogleApiClient() {
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestProfile()
.requestEmail()
.requestScopes(Drive.SCOPE_FILE, Drive.SCOPE_APPFOLDER)
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.addApi(Plus.API)
.addApi(Drive.API)
.build();
mGoogleApiClient.connect();
}
}
有关此的详细信息看看就在this标志Android
教程。
谷歌加登录是马上就会过时,所以没有人对此提出更多的担心
开始2019年3月7日,与起始范围所有的OAuth令牌请求“加”。 (即“plus.me”,“plus.login”,“plus.profile.emails.read”等)将根据要求失败,并且可以开始早2019年2月15,间歇性地失败。