是否有任何使用应用引擎服务帐户访问 BigQuery 服务的 Java 示例?我不想使用 OAuth 流程。
问问题
554 次
1 回答
2
获取服务帐户凭据的唯一方法:
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson.JacksonFactory;
import java.io.File;
import java.util.Arrays;
credential = new GoogleCredential.Builder().setTransport(TRANSPORT)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(SERVICE_ACCOUNT)
.setServiceAccountScopes(Arrays.asList(new String[] {SCOPE}))
.setServiceAccountPrivateKeyFromP12File(new File(P12_KEY))
.build();
在哪里:
SERVICE_ACCOUNT = "15-uin9i5@developer.gserviceaccount.com"
P12_KEY = "path/to/private_key.p12"
TRANSPORT = new NetHttpTransport();
SCOPE = "https://www.googleapis.com/auth/bigquery"
JSON_FACTORY = new JacksonFactory()
获得凭据后,您可以构建 BigQuery 服务并将其与任何其他身份验证方法一起使用:
import com.google.api.services.bigquery.Bigquery;
bigquery = new Bigquery.Builder(
TRANSPORT, JSON_FACTORY, credential).setApplicationName(
"BQ-ServAcc/so/0.1").setHttpRequestInitializer(credential).build();
例如,要检查作业:
import com.google.api.services.bigquery.model.Job;
while (true) {
Job job = bigquery.jobs().get(projectId, jobId).execute();
System.out.println(job.getStatus().toPrettyString());
if (job.getStatus().getState().equals("DONE")) {
if (job.getStatus().getErrorResult() != null)
System.out.println(job.getStatus().getErrorResult().toPrettyString());
System.out.println(job.getStatistics().toPrettyString());
break;
}
Thread.sleep(1000);
}
于 2013-06-28T01:02:00.423 回答