Google Drive/OAuth - Can't figure out how to get re-usable GoogleCredentials

前端 未结 3 2280
無奈伤痛
無奈伤痛 2021-02-20 05:51

I\'ve successfully installed and run the Google Drive Quick Start application called DriveCommandLine. I\'ve also adapted it a little to GET file info for one of the files in my

3条回答
  •  囚心锁ツ
    2021-02-20 06:23

    Ok, I've finally solved the second problem above and I'm finally getting a working GoogleCredential object with an access token and a refresh token.

    I kept trying to solve the scopes problem by modifying the list of scopes in MyClass (the one that manages credentials). In the end I needed to adjust the scopes in my modified version of DriveCommandLine (the one that's originally used to get an authorization code). I added 2 scopes from Oauth2Scopes:

    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
        httpTransport, jsonFactory, CLIENT_ID, CLIENT_SECRET,
        Arrays.asList(DriveScopes.DRIVE, Oauth2Scopes.USERINFO_EMAIL, Oauth2Scopes.USERINFO_PROFILE))
        .setAccessType("offline").setApprovalPrompt("force").build();
    

    Adding the scopes for user information allowed me to get the userid later in MyClass. I can now use the userid to store the credentials in a database for re-use (without having to get the user to go to a URL each time). I also set the access type to "offline" as suggested by pinoyyid.

提交回复
热议问题