【问题标题】:Google + Integration fails on Android M deviceAndroid M 设备上的 Google + 集成失败
【发布时间】:2017-01-13 11:32:24
【问题描述】:

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

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 = new GoogleSignInOptions.Builder(GoogleSignInOptions.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 google-plus google-signin


    【解决方案1】:

    Android Marshmallow 引入了 Android 运行时权限,因此如果您的设备的系统版本至少为 6.0,您应该为用户提供此功能

    在这里阅读:https://developer.android.com/training/permissions/requesting.html

    为避免实施此功能,请转至您的 app/build.gradle 并降级 targetSdkVersion 到 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"
    

    【讨论】:

      【解决方案2】:
      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" />
      

      【讨论】:

      • 是的,但是我需要授予哪些权限才能登录?
      • Google + 签名需要 GET_ACCOUNTS 权限。请查看我的更新答案。
      • 如果您关注latest version of the Sign In docs,则不再需要 GET_ACCOUNTS 权限。
      【解决方案3】:
      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();
      

      【讨论】:

      • 我做了上面的代码,现在出现以下错误。
      • Bundle[{googleSignInStatus=Status{statusCode=unknown status code: 12501, resolution=null}}]
      猜你喜欢
      • 2016-01-18
      • 2017-09-08
      • 1970-01-01
      • 2018-07-29
      • 1970-01-01
      • 1970-01-01
      • 2016-08-16
      • 2018-04-30
      • 1970-01-01
      相关资源
      最近更新 更多