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

前端 未结 3 2302
無奈伤痛
無奈伤痛 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:19

    Oath2Scopes is imported as follows:

    import com.google.api.services.oauth2.Oauth2Scopes;
    

    You need to have the jar file 'google-api-services-oauth2-v2-rev15-1.8.0-beta.jar' in your class path to access that package. It can be downloaded here.

    No, I don't know how to get Credentials without having to visit the authorization URL at least once and copy the code. I've modified MyClass to store and retrieve credentials from a database (in my case, it's a simple table that contains userid, accesstoken and refreshtoken). This way I only have to get the authorization code once and once I get the access/refresh tokens, I can reuse them to make a GoogleCredential object. Here's how Imake the GoogleCredential object:

    GoogleCredential credential = new GoogleCredential.Builder().setJsonFactory(jsonFactory)
                .setTransport(httpTransport).setClientSecrets(clientid, clientsecret).build();
        credential.setAccessToken(accessToken);
        credential.setRefreshToken(refreshToken);
    

    Just enter your clientid, clientsecret, accessToken and refreshToken above.

    I don't really have a whole lot of time to separate and tidy up my entire code to post it up here but if you're still having problems, let me know and I'll see what I can do. Although, you are effectively asking a blind man for directions. My understanding of this whole system is very sketchy!

    Cheers, Brian

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-20 06:26

    I feel your pain. I'm two months in and still getting surprised.

    Some of my learnings...

    • When you request user permissions, specify "offline=true". This will ("sometimes" sic) return a refreshtoken, which is as good as a password with restricted permissions. You can store this and reuse it at any time (until the user revokes it) to fetch an access token.

    • My feeling is that the Google SDKs are more of a hinderence than a help. One by one, I've stopped using them and now call the REST API directly.

    • On your last point, you can (just) use the Google clientlogin protocol to access the previous generation of APIs. However this is totally deprecated and will shortly be turned off. OAuth is designed to give fine grained control of authorisation which is intrinsically complex. So although I agree it's complicated, I don't think it's unnecessarily so. We live in a complicated world :-)

    Your and mine experiences show that the development community is still in need of a consolidated document and recipes to get this stuff into our rear-view mirrors so we can focus on the task at hand.

    0 讨论(0)
提交回复
热议问题