Issue
I'm creating a Java desktop application that communicates with a Cloud SQL MySQL database. Currently, access is granted per ip address, and restricts access to certain computers to the database. I would like to authenticate computer access to the database using Google's OAuth 2.0. How do I set up Google OAuth for my application? I've read the Google docs (OAuth 2.0 and Google Client API), but I still have no clue how to do this.
Any help is much appreciated!
EDIT I have a client, which is the application running on a computer. This Java application has an openConnection method, that requests a mysql connection through the jdbc driver. I think that the openConnection method should look like presented below.
public void openConnection() {
Connection con = //HTTP request to Google App Engine
//In http request: request(accessToken, ipAddress, );
}
The server should have a doGet method, which should look something like the one presented below.
public void doGet(HttpServletRequest req, HttpServletResponse res) {
//Authenticate
boolean passed = //some way to authenticate the client
if (passed) {
//This request would pass, because the app engine has read write access
Connection con = DriverManager.getConnection("jdbc:mysql://"+ipAddress+":"+portNumber+"/"+databaseName,user,pass);
return connection;
}
else{
//Not authenticated
}
}
Does anyone know how to do this?
Solution
- You have to create a project at Google Console.
- You have to create a OAuth Client ID for the created project
- Implement OAuth in your application like described in chapter Command-line authorization code flow on the Wiki Page
- Replace OAuth2ClientCredentials-Constants from the example with your generated client ID and client secret from step 2
A complete example of an java app which implemented OAuth with Google OAuth lib can be found here. Just checkout the source and adapt it to your application and change the Client ID and Client secret.
Answered By - Lorenz Pfisterer
Answer Checked By - Timothy Miller (JavaFixing Admin)