Google+集成在安卓M设备上失败



使用以下链接:https://developers.google.com/identity/sign-in/android/start-integrating我已经将谷歌登录集成到我的项目中。代码与下面的链接相同:

public class MainActivity extends AppCompatActivity {
    GoogleApiClient mGoogleApiClient;
    Button btn;
    int RC_SIGN_IN =100;
    String TAG ="Google";
    @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 */, new GoogleApiClient.OnConnectionFailedListener() {
                    @Override
                    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
                    }
                } /* OnConnectionFailedListener */)
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .build();

        SignInButton signInButton = (SignInButton) findViewById(R.id.sign_in_button);
        signInButton.setSize(SignInButton.SIZE_STANDARD);
        signInButton.setScopes(gso.getScopeArray());

        btn =(Button)findViewById(R.id.btnSignout);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                signOut();
            }
        });
        signInButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                signIn();
            }
        });
    }
    private void signIn() {
        Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
        startActivityForResult(signInIntent, RC_SIGN_IN);
    }
    private void signOut() {
        Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
                new ResultCallback<Status>() {
                    @Override
                    public void onResult(Status status) {
                        Toast.makeText(getApplicationContext(),"Signout",Toast.LENGTH_SHORT).show();
                    }
                });
    }
    @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);
           // GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
          /*  GoogleSignInAccount acct = result.getSignInAccount();
            String personName = acct.getDisplayName();
            String personEmail = acct.getEmail();
            String personId = acct.getId();
            Uri personPhoto = acct.getPhotoUrl();*/
        }
    }
    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(getString(R.string.signed_in_fmt, acct.getDisplayName()));
            updateUI(true);*/
            String personName = acct.getDisplayName();
            String personEmail = acct.getEmail();
            Toast.makeText(getApplicationContext(),"Success "+ personName,Toast.LENGTH_SHORT).show();
        } else {
            // Signed out, show unauthenticated UI.
          //  updateUI(false);
            Toast.makeText(getApplicationContext(),"Failure "+ result.getStatus(),Toast.LENGTH_SHORT).show();
        }
    }
}

这可以在Kitkat设备上找到,但在android M设备上它显示

状态{statusCode=DEVELOPER_ERROR,resolution=null}

首先,我认为可能是Json文件问题,但如果是,它不应该在任何设备上工作。它在kitkat 4.2.2设备上运行成功,但在android M设备上失败。那么这个怎么了?

在所有答案之后,我做了以下简单的代码:

GoogleSignInOptions gso=新的GoogleSignInOptions.Builder(GoogleSignInOperations.DEFAULT_SIGN_IN(.requestEmail((.build((;

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this, new GoogleApiClient.OnConnectionFailedListener() {
                @Override
                public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
                }
            })
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();
    Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
    startActivityForResult(signInIntent, RC_SIGN_IN);

它对我的电子邮件Id有效,但当我在其他设备和不同的电子邮件上使用相同的代码时,它会显示相同的错误。当我尝试添加server_client id时,它显示status=12501错误。我仍然不知道代码出了什么问题。

随着Android的推出,棉花糖被引入了Android运行时权限,所以如果你的设备的系统版本至少是6.0,你应该为用户提供这个功能

阅读此处:https://developer.android.com/training/permissions/requesting.html

要避免实现此功能,请转到您的app/build.gradle并降级CCD_ 2至22。所以你的build.gradle应该看起来像:

  defaultConfig {
        applicationId "com.example.piotr.netgururecruit"
        minSdkVersion 12
        targetSdkVersion 22 //downgrade this value to 22 or 21 to avoid runtime permissions 
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
Since 6.0 some permissions are considered as "dangerous".

为了保护用户,他们必须在运行时获得授权,这样用户才能知道这是否与他的操作有关。您可以在下面的链接上查看完整的示例:https://www.learn2crack.com/2015/10/android-marshmallow-permissions.html

在menifest.xml 中添加这2个权限

 <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
you have to pass your google_server_client_id 

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(getString(R.string.google_server_client_id))
            .requestServerAuthCodegetString(R.string.google_server_client_id)
            .requestEmail()
            .build();
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
            .enableAutoManage(getActivity(), this)
            .addApi(Plus.API, Plus.PlusOptions.builder().build())
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();

最新更新