java - onActivityResult总是返回未授权

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

我试图让用户登录该应用,以便能够使用 Google Drive API,但在我的 ActivityResults 中,我总是收到 else 语句,这意味着该用户未获得授权或登录失败。为什么会这样?我已关注the official docs .

这是我的代码:

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);
    }

最佳答案

解决了我自己的问题,将 SHA1 和包名称粘贴到我的 Google 控制台中的凭据中,看来我对凭据做了一些错误的操作。

关于java - onActivityResult总是返回未授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48650577/

相关文章:

java - Android中制作进度条

Android bundle = .NET session []?

javascript - Google API 地理编码 OVER_QUERY_LIMIT ,每秒 5 个地址?

java - Google 如何验证 API 调用中的 SHA1 和程序包名称?

android - 通过市场强制更新应用程序,就像用户单击 "Update"

java - 安卓 5.0 IllegalArgumentException : Service Intent must be explicit: on com. google.android.location.internal.GoogleLocationManagerService.START

java - 删除 java 中的 println 调用会导致逻辑错误

java - 将服务设置为从 Android 中的按钮调用

java - 递归打印

java - 如何在Java中获取准确的deltaTime