must I click “Allow Acess” every time I connect? G

2019-03-04 16:27发布

问题:

I wrote calss that saves refresh and access token in my db. I can generate access token by refresh token too. everything works well. I working with web application. now I have another question:

1) For example, I have already saved Refresh token. then if another person comes, I should check if that person have refresh token in my database to generate her access token. but I should have her user ID firstly.

2) to get user ID I need her credentials

For example:

Oauth2.Builder builder = new Oauth2.Builder(new NetHttpTransport(), new JacksonFactory(), credentials);
Oauth2 userInfoService = builder.build();      
Userinfo userInfo= userInfoService.userinfo().get().execute();

3) and to get credentials I need authorizationCode:

Credential credentials = exchangeCode(authorizationCode);

4) and to get authorizationCode User Should Click "Allow Acess" in order to retrieve that code? is it right?

for example:

HttpTransport httpTransport = new NetHttpTransport(); JacksonFactory jsonFactory = new JacksonFactory();

     GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(jsonFactory, GoogleStorage.class.getResourceAsStream(CLIENTSECRETS_LOCATION));

    flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, jsonFactory, clientSecrets, SCOPES).setAccessType("offline").setApprovalPrompt("force").build();

and get URL where autorization code will be retrieved:

flow.newAuthorizationUrl().setRedirectUri(REDIRECT_URI).build();

for instance URL:

https://accounts.google.com/o/oauth2/auth?access_type=offline&approval_prompt=force&client_id=695230079990-bus3d9gp31vvv64qq5n4ma9mk8vtc1ck.apps.googleusercontent.com&redirect_uri=http://localhost/oauth2callback&response_type=code&scope=https://www.googleapis.com/auth/drive.file%20https://www.googleapis.com/auth/userinfo.email%20https://www.googleapis.com/auth/userinfo.profile

must one person click that button everytime she try to connect to Drive API? I see web applications, where one person clicks that button only once. how to get Autorization code without every time clicking "Allow Acess". I need to click that only once.

回答1:

Users do not have to grant access to the app every time they run it. You store their credentials in a database and reuse them when needed.

Check the Java DrEdit guide for a complete sample app showing how to get a complete set of credentials for every request:

https://developers.google.com/drive/examples/java