Friday, July 19, 2013

Can't authenticate the user to Connect cloud database

Hi all,

I am new to this cloud datastorage thing. I got a java console project that needs to connect cloud database to verify user-password (and to be able to add new records to database when sign up) and i am super confused what to use. To authenticate should i use service account method or installed application one? For installed application method do i need to open a new google account and use it for everyone who uses the program? 

I connected cloud db with cloud.sql.Driver;

But as you all know i needed to verify my user with sql_command_tool . The users of my program shouldn't connect like this. I think to automate so i need to use OAuth2.0. 

But when i try the code below, i get  error: 

java.lang.NullPointerException
at java.util.Collections$UnmodifiableCollection.<init>(Unknown Source)
at java.util.Collections.unmodifiableCollection(Unknown Source)
at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.<init>(GoogleCredential.java:208)
at com.google.api.client.googleapis.auth.oauth2.GoogleCredential$Builder.build(GoogleCredential.java:368)
at Deneme.main(Deneme.java:66)


My Code:  

 try {
     try {
       HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
       GoogleCredential credential = new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT)
           .setJsonFactory(JSON_FACTORY)
           .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
           .setServiceAccountPrivateKeyFromP12File(new File("f013fa043086020121d22df3d38vaga7a159619fc-privatekey.p12"))
          // .setServiceAccountUser("user@example.com")
           .build();
        Connection cloud = null;
   try {
     DriverManager.registerDriver(new Driver());
     cloud = DriverManager.getConnection("jdbc:google:rdbms://instance/db");
} catch (SQLException e) {
       e.printStackTrace();
   } finally {
       if (cloud != null) 
         try {
           cloud.close();
           } catch (SQLException ignore) {
        }
     } 
 }
       
       return;
     } catch (IOException e) {
       System.err.println(e.getMessage());
     }
   } catch (Throwable t) {
     t.printStackTrace();
   }
   System.exit(1);
 }


Any help is appreciated,

Thanks

Yusuf


   -

--
You received this message because you are subscribed to the Google Groups "Google Cloud SQL discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-cloud-sql-discuss+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment