简体   繁体   English

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

[英]java.lang.IllegalStateException: Must not set scopes in GoogleApiClient.Builder when using Auth.GOOGLE_SIGN_IN_API

I am using drive api with authentication api. 我正在将驱动器api与身份验证api一起使用。 I need to login to google account first then upload files to drive. 我需要先登录到Google帐户,然后将文件上传到驱动器。 Problem is when i use only drive api without authentication is says unable to sign in. I signed in first using 问题是当我仅使用未经身份验证的驱动器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);

it logged in successfully. 它成功登录。 But when i try to use drive api with this it gives error. 但是,当我尝试与此一起使用驱动器api时,会出现错误。 Following is the code 以下是代码

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

Any solution how i can login first then add files to drive? 任何解决方案,我如何可以先登录然后添加文件到驱动器?

Try this code: 试试这个代码:

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

Then set the connection code for authorization to occur: 然后设置用于授权的连接代码:

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

I am following the documentation - Authorizing Android Apps , this documentation also provide give on how to handle ff the user has not previously authorized the application. 我正在关注文档- 授权Android应用程序 ,该文档还提供了如何处理用户先前未授权该应用程序的信息。

Hope this helps. 希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.IllegalStateException:必须连接GoogleApiClient - java.lang.IllegalStateException: GoogleApiClient must be connected Android:Google Play游戏服务连接错误(java.lang.IllegalStateException:必须连接GoogleApiClient。) - Android: Google play games services connection error ( java.lang.IllegalStateException: GoogleApiClient must be connected.) java.lang.IllegalStateException:尚未连接GoogleApiClient - java.lang.IllegalStateException: GoogleApiClient is not connected yet java.lang.IllegalStateException:GoogleApiClient尚未连接 - java.lang.IllegalStateException: GoogleApiClient is not connected yet java.lang.IllegalStateException:使用 WebSecurityConfigurerAdapter 时 - java.lang.IllegalStateException: When using WebSecurityConfigurerAdapter java.lang.IllegalStateException:您必须设置文本或视图 - java.lang.IllegalStateException: You must either set a text or a view 引起:java.lang.IllegalStateException:GoogleApiClient 尚未连接 - Caused by: java.lang.IllegalStateException: GoogleApiClient is not connected yet 致命异常:java.lang.IllegalStateException尚未连接GoogleApiClient - Fatal Exception: java.lang.IllegalStateException GoogleApiClient is not connected yet java.lang.IllegalStateException关于在Android中添加Google Places API - java.lang.IllegalStateException on adding google places API in android 用于FCM的GoogleApiClient.Builder()。addApi() - GoogleApiClient.Builder().addApi() for FCM
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM