Sunday, July 21, 2013

Re: Can't authenticate the user to Connect cloud database

I think i need to explain more. All i want to do is doing what sql_command_tool of google : get my access token then use it while connection cloud database. I was examining the examples on internet and there is a part that builds new object depending of what to do. for ex:

  GoogleCredential credential = new  GoogleCredential.Builder()        .setTransport(TRANSPORT)        .setJsonFactory(JSON_FACTORY)        .setServiceAccountId("SOMETHING@developer.gserviceaccount.com")        .setServiceAccountScopes(BigqueryScopes.BIGQUERY)        .setServiceAccountPrivateKeyFromP12File(new File("key.p12"))        .build();        Bigquery bigquery = Bigquery.builder(TRANSPORT, JSON_FACTORY)        .setHttpRequestInitializer(credential)        .build();
  
  i can get my access token from oauth 2.0 after creating credentials but i don't know how to use it while trying to connect cloud sql. Any ideas?
  
  my current code:
  
HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();  		        GoogleCredential credential = new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT)  		            .setJsonFactory(JSON_FACTORY)  		            .setServiceAccountId("xxx@developer.gserviceaccount.com")  		           .setServiceAccountScopes("https://www.googleapis.com/auth/sqlservice.admin","https://www.googleapis.com/auth/cloud-platform")  		            .setServiceAccountPrivateKeyFromP12File(new File("xxx-privatekey.p12"))  		            //.setServiceAccountUser("xxxx.apps.googleusercontent.com")  		            .build();
credential.refreshToken();

Connection cloud = null;
   try {
     DriverManager.registerDriver(new Driver());
     cloud = (Connection) DriverManager.getConnection("jdbc:google:rdbms://instance/dbname","","");
...}


Current error : 


java.sql.SQLException: Unable to load OAuth2 refresh or access token. Please run the google_sql command line tool once to setup cached credentials. See: http://code.google.com/apis/sql/docs/commandline.html
at com.google.cloud.sql.jdbc.internal.Exceptions.newInvalidParameterException(Exceptions.java:131)
at com.google.cloud.sql.jdbc.internal.Util.checkParameter(Util.java:222)
at com.google.cloud.sql.Driver.connect(Driver.java:94)
at com.google.cloud.sql.Driver.connect(Driver.java:31)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at Deneme.main(Deneme.java:84)


On Fri, Jul 19, 2013 at 7:14 PM, Yusuf Subaşı <yusufsubasi88@gmail.com> wrote:
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 a topic in the Google Groups "Google Cloud SQL discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-cloud-sql-discuss/lg8P0pc2Vc0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-cloud-sql-discuss+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--

Yusuf Subaşı
İtü Bilgisayar Mühendisliği
0555 834 81 88

--
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