Reputation: 984
So here is the code that I am using for connecting to Google Drive API and authenticate my users, the problem I am facing is that whenever I call the function to authenticate the user the consent screen is being displayed on the server (where tomcat is installed, not the PC where the user is working on). I know it is a strange issue and I cannot find anything on this in the forums.
private Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
final String CREDENTIALS_FILE_PATH = "/credentials.json";
final String TOKENS_DIRECTORY_PATH = "/tokens";
final List<String> SCOPES = Collections.singletonList(DriveScopes.DRIVE);
InputStream in = this.getClass().getResourceAsStream(CREDENTIALS_FILE_PATH);
if (in == null) {
throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
}
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
.setAccessType("offline")
.build();
LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(9999).build();
return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
}
After I call this method, the application is opening the default browser of the server (Chrome) and showing the consent screen. I don't know what I am doing wrong here. Thanks for helping. Cheers!
Upvotes: 2
Views: 371
Reputation: 116978
GoogleAuthorizationCodeFlow.Builder
is designed to be used by installed applications.
what you should be using is GoogleBrowserClientRequestUrl
public void doGet(HttpServletRequest request, HttpServletResponse response)throws IOException {
String url = new GoogleBrowserClientRequestUrl("812741506391.apps.googleusercontent.com",
"https://oauth2.example.com/oauthcallback", Arrays.asList(
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/userinfo.profile")).setState("/profile").build();
response.sendRedirect(url);
}
Upvotes: 2