Reputation: 189
I am trying to get an access token to use the Cloud SQL Admin API to export a table as CSV to google cloud storage. I keep getting this error: Scopes not configured for service account. Scoped should be specified by calling createScoped or passing scopes to constructor.
everytime I call credentials.refreshAccessToken();
In the code below.
Could someone guide me as to what I'm doing wrong here
Set<String> oAuthScopes = new HashSet<String>();
oAuthScopes.add(SQLAdminScopes.CLOUD_PLATFORM);
oAuthScopes.add(SQLAdminScopes.SQLSERVICE_ADMIN);
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault();
credentials.createScoped(oAuthScopes);
AccessToken access_token = credentials.refreshAccessToken();
try {
httppost.addRequestHeader("Content-Type", "application/json");
httppost.addRequestHeader("Authorization", "Bearer " + access_token.getTokenValue());
httppost.setRequestEntity(
new StringRequestEntity(
"{\"exportContext\":" +
"\"fileType\": \"CSV\"," +
"\"uri\":" + EXPORT_BUCKET +
"\"databases\": [\"" + DATABASE_NAME + "\"]," +
"\"csvExportOptions\": " +
"{" +
"\"selectQuery\":\"" + SQL_QUERY + "\" " +
"} " +
"} " +
"}",
"application/json",
"UTF-8"));
httpclient.executeMethod(httppost);
String response = new String(httppost.getResponseBody());
httppost.releaseConnection();
} catch (UnsupportedEncodingException unsupportedEncodingException) {
unsupportedEncodingException.printStackTrace();
}
Upvotes: 1
Views: 1940
Reputation: 753
This exception seems to come from the fact that the Application Default Credentials authorization strategy for client libraries will kick in once you set 'GOOGLE_APPLICATION_CREDENTIALS'
Also, you can create a scoped credential like this:
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault().createScoped(Arrays.asList("https://www.googleapis.com/auth/cloud-platform"));
Upvotes: 1