首页 文章

Android M设备上的Google集成失败

提问于
浏览
0

使用以下链接: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 = 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错误 . 我还不知道代码有什么问题 .

3 回答

  • 0

    随着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"
    
  • 0
    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" />
    
  • 0
    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();
    

相关问题