I am trying to use oauth API to authenticate google service account through Java API. I hope to use it to access Google Bigquery. I get an "invalid grant" returned from my API requests.
Here is the code, which is a copy of the basic authentication example (which was not for Bigquery .. but another Google API):
private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
private static final JsonFactory JSON_FACTORY = new JacksonFactory();
private static Bigquery bigquery;
public ServiceAccountExample() {
try {
try {
GoogleCredential credential = new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
.setServiceAccountScopes(BigqueryScopes.BIGQUERY)
.setServiceAccountPrivateKeyFromP12File(new File("GoogleBigQuery-privatekey.p12"))
.build();
credential.refreshToken();
bigquery = new Bigquery.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
.build();
listDatasets(bigquery, "publicdata");
return;
} catch (IOException e) {
System.err.println(e.getMessage());
}
} catch (Throwable t) {
t.printStackTrace();
}
}
SERVICE_ACCOUNT_EMAIL - form email address: XXXXXXX@developer.gserviceaccount.com
If I delete the credential.refreshToken () line, it will not work in the dataset lists the first time Bigquery is called ... Otherwise, it will not be executed on credential.refreshToken () .. with the same error.
Does BigQuery recognize service account authentication?
, API-. :
- API Big Query.
- " API".
- ( ).
- "".
- .
- ? - ?
..