gpt4 book ai didi

google-bigquery - 使用服务帐户的 Google Bigquery 401 未经授权的错误

转载 作者:行者123 更新时间:2023-12-01 09:34:37 28 4
gpt4 key购买 nike

当我尝试来自 this post 的 Michael 的 bigquery 片段时遇到了“401 Unauthorized”问题在我的电脑上。根据 ryguyrg 的建议,我同步了我的计算机时间(通过单击“立即更新”)并且代码工作。但问题是,一段时间后,也许几分钟后,当我重新运行代码时,它再次失败并出现 401 错误。所以几乎每次我想运行大查询请求时,我都必须手动同步我的计算机时间。

我很确定我的计算机运行良好,并且时间与服务器的差异不应超过几毫秒。那么究竟是什么导致了这个问题呢?我应该尝试在请求之前从代码中同步时间还是有更好的方法?

下面是401错误信息供引用:

Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized
at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:159)
at com.google.api.client.googleapis.json.GoogleJsonResponseException.execute(GoogleJsonResponseException.java:187)
at com.google.api.client.googleapis.services.GoogleClient.executeUnparsed(GoogleClient.java:115)
at com.google.api.client.http.json.JsonHttpRequest.executeUnparsed(JsonHttpRequest.java:112)
at com.google.api.services.bigquery.Bigquery$Jobs$Insert.executeUnparsed(Bigquery.java:1418)
at com.google.api.services.bigquery.Bigquery$Jobs$Insert.execute(Bigquery.java:1442)
at BigQueryJavaServiceAccount.main(BigQueryJavaServiceAccount.java:83)

最佳答案

在这个例子中我遇到了同样的问题。 https://cloud.google.com/bigquery/docs/quickstarts/quickstart-client-libraries

我试图执行以下命令,但发生 401 错误

导出 GOOGLE_APPLICATION_CREDENTIALS=/Users/mattheu/credentialFileName.json

我使用下面解决了凭据问题

String jsonPath = "/Users/mattheu/credentialFileName.json";
GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(jsonPath));
package com;

import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.bigquery.*;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.UUID;

public class App {
public static void main(String[] args) throws InterruptedException, IOException {
String jsonPath = "/Users/mattheu/credentialFileName.json";
GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(jsonPath));

BigQuery bigquery = BigQueryOptions.newBuilder()
.setCredentials(credentials)
.build().getService();

QueryJobConfiguration queryConfig =
QueryJobConfiguration.newBuilder(
"SELECT "
+ "CONCAT('https://stackoverflow.com/questions/', CAST(id as STRING)) as url, "
+ "view_count "
+ "FROM `bigquery-public-data.stackoverflow.posts_questions` "
+ "WHERE tags like '%google-bigquery%' "
+ "ORDER BY favorite_count DESC LIMIT 10")
// Use standard SQL syntax for queries.
// See: https://cloud.google.com/bigquery/sql-reference/
.setUseLegacySql(false)
.build();

// Create a job ID so that we can safely retry.
JobId jobId = JobId.of(UUID.randomUUID().toString());
Job queryJob = bigquery.create(JobInfo.newBuilder(queryConfig).setJobId(jobId).build());

// Wait for the query to complete.
queryJob = queryJob.waitFor();

// Check for errors
if (queryJob == null) {
throw new RuntimeException("Job no longer exists");
} else if (queryJob.getStatus().getError() != null) {
// You can also look at queryJob.getStatus().getExecutionErrors() for all
// errors, not just the latest one.
throw new RuntimeException(queryJob.getStatus().getError().toString());
}

QueryResponse response = bigquery.getQueryResults(jobId);

TableResult result = queryJob.getQueryResults();

// Print all pages of the results.
for (FieldValueList row : result.iterateAll()) {
String url = row.get("url").getStringValue();
long viewCount = row.get("view_count").getLongValue();
System.out.printf("url: %s views: %d%n", url, viewCount);
}
}
}

result:
url: What is Google's Dremel? How is it different from Mapreduce? views: 27736
url: Unable to access BigQuery from local App Engine development server views: 4732
url: How do I use the TABLE_QUERY() function in BigQuery? views: 9674
url: Migrating from non-partitioned to Partitioned tables views: 3549
url: How can I undelete a BigQuery table? views: 3332
url: Google App Engine: Using Big Query on datastore? views: 6928
url: Random Sampling in Google BigQuery views: 11635
url: How to use Bigquery streaming insertall on app engine & python views: 4279
url: Delete duplicate rows from a BigQuery table views: 8552
url: How to improve performance of GeoIP query in BigQuery? views: 3213

关于google-bigquery - 使用服务帐户的 Google Bigquery 401 未经授权的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10560768/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com