我需要google plus登录我的应用程序,我登录所有版本,除了Lolly pop。谷歌加上我的应用程序登录的程序是什么? 请给我回复实际程序是什么......
这是我到目前为止所做的代码......
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);
}
});
}
}
}
答案 0 :(得分:0)
我没有登录Facebook登录谷歌加,但我已经习惯了它帮助你的模块。祝你好运here
答案 1 :(得分:0)
(我试图将代码格式化为一个小时但没有运气,有人用适当的格式编辑答案)。
实际答案:
这对我有用,目前正在生产中,因此经过彻底测试
public class LoginActivity extends 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);
}
}
答案 2 :(得分:0)
这是我在我的应用中使用Google plus登录的修改版本。适用于我支持的所有api版本(14及以上)。
简而言之,这个课程做了以下几点。
GoogleApiClient
时设置Activity
所需的范围。onClickListener
。Activtiy
中开始签名并显示结果。onActivityResult
。<强> SignInActivity.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();
}
}
有关详细信息,请查看有关Android
登录的this教程。
答案 3 :(得分:0)
Google Plus登录即将被弃用,因此无需担心
从2019年3月7日开始,所有OAuth令牌请求的范围都以“加号”开头。 (即“ plus.me”,“ plus.login”,“ plus.profile.emails.read”等)会根据请求失败,并且可能最早在2019年2月15日开始间歇性地失败。