java.lang.IllegalStateException:使用Auth.GOOGLE_SIGN_IN_API时,不得在GoogleApiClient.Builder中设置范围

时间:2017-05-19 20:22:26

标签: java android google-play-services drive android-googleapiclient

我正在使用带有身份验证API的驱动器api。我需要先登录谷歌帐户然后上传文件到驱动器。问题是当我只使用驱动器api而没有身份验证时表示无法登录。我先使用

登录
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestEmail()
                .build();
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .build();

        Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
        startActivityForResult(signInIntent, 4);

已成功登录。但是,当我尝试使用驱动器api时,它会出错。以下是代码

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestEmail()
                .build();
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
                .addApi(Drive.API)
                .addScope(Drive.SCOPE_FILE)
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .build();

        Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
        startActivityForResult(signInIntent, 4);

我可以先登录然后再将文件添加到驱动器的任何解决方案吗?

1 个答案:

答案 0 :(得分:1)

试试这段代码:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstance);

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Drive.API)
            .addScope(Drive.SCOPE_FILE)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
}

然后设置授权发生的连接代码:

@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

我正在关注文档 - Authorizing Android Apps,此文档还提供了如何处理用户之前未授权应用程序的文件。

希望这有帮助。