onActivityResult总是返回未授权

时间:2018-02-06 19:31:45

标签: java android google-api google-drive-api

我试图只是签署一个用户到应用程序,以便能够使用谷歌驱动器api,但在我的activityResults我总是得到其他的声明,这意味着用户未经授权或登录失败。知道为什么吗?我遵循了官方文档HERE

这是我的代码

private static final int REQUEST_CODE_SIGN_IN = 0;
    private DriveClient mDriveClient;
    private DriveResourceClient mDriveResourceClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        signIn();
    }



    private void initializeDriveClient(GoogleSignInAccount signInAccount) {
        mDriveClient = Drive.getDriveClient(getApplicationContext(), signInAccount);
        mDriveResourceClient = Drive.getDriveResourceClient(getApplicationContext(), signInAccount);
    }

    /**
     * Starts the sign-in process and initializes the Drive client.
     */
    private void signIn() {
        Set<Scope> requiredScopes = new HashSet<>(2);
        requiredScopes.add(Drive.SCOPE_FILE);
        requiredScopes.add(Drive.SCOPE_APPFOLDER);
        GoogleSignInAccount signInAccount = GoogleSignIn.getLastSignedInAccount(this);
        if (signInAccount != null && signInAccount.getGrantedScopes().containsAll(requiredScopes)) {
            initializeDriveClient(signInAccount);
        } else {
            GoogleSignInOptions signInOptions =
                    new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                            .requestScopes(Drive.SCOPE_FILE)
                            .requestScopes(Drive.SCOPE_APPFOLDER)
                            .build();
            GoogleSignInClient googleSignInClient = GoogleSignIn.getClient(this, signInOptions);
            startActivityForResult(googleSignInClient.getSignInIntent(), REQUEST_CODE_SIGN_IN);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {

            case REQUEST_CODE_SIGN_IN:
                if (resultCode == Activity.RESULT_OK) {
                    // App is authorized, you can go back to sending the API request

                    Log.e("SignIn", "App autorizada");
                } else {
                    // User denied access, show him the account chooser again
                    Log.e("SignIn", "App No Autorizada");
                    finish();
                }
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

2 个答案:

答案 0 :(得分:1)

解决了我自己的问题,将我的google控制台中的SHA1和包名称粘贴到凭据中,似乎我的凭据出错了。

答案 1 :(得分:1)

代码看起来很好。检查您是否在项目中添加了google_services.json文件。还要检查SHA1键是否正确。