Google cloud platform 验证Google云API
我试图使用BigQuery客户端,但收到身份验证错误:Google cloud platform 验证Google云API,google-cloud-platform,google-authentication,Google Cloud Platform,Google Authentication,我试图使用BigQuery客户端,但收到身份验证错误: <groupId>com.google.apis</groupId> <artifactId>google-api-services-bigquery</artifactId> <version>v2-rev397-1.23.0</version> com.google.api 谷歌api服务bigquery v2-re
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-bigquery</artifactId>
<version>v2-rev397-1.23.0</version>
com.google.api
谷歌api服务bigquery
v2-rev397-1.23.0
com.google.api.client.googleapis.json.googlejson响应异常:401
未经授权
我在路径中没有服务帐户,因为我是项目的所有者,并且通过
gcloud auth login
登录到我的帐户。当我尝试gcloud auth list
时,我看到我已使用项目所有者授予的凭据登录。为什么会出现此错误?即使您通过gcloud auth login
登录,也不意味着您的应用程序将使用这些凭据。您仍然需要使用OAuth 2.0进行身份验证
如果希望使用用户帐户进行身份验证,可以尝试生成访问令牌并将其传递给类(请记住令牌过期)
要为用户凭据生成访问令牌,请使用gcloud工具:
gcloud auth application-default print-access-token
将生成的令牌传递给GoogleCredential类:
GoogleCredential credential = new GoogleCredential().setAccessToken(accessToken);
我对google api services bigquery api不是很熟悉,但我认为这就是您使用凭证的方式:
Bigquery bigquery = Bigquery.builder(HTTP_TRANSPORT, JSON_FACTORY).setHttpRequestInitializer(credential).build();