google-cloud-platform - 我收到 401 Invalid Token, Invalid Value 但只是有时
问题描述
我有以下代码:
public static void main(String[] args) throws Exception {
String r = null;
try {
httpTransport = GoogleNetHttpTransport.newTrustedTransport();
dataStoreFactory = new FileDataStoreFactory( DATA_STORE_DIR );
Credential credential = authorize();
String accesToken = credential.getAccessToken();
Client client = ClientBuilder.newClient();
WebTarget googleTarget = client.target( "https://compute.googleapis.com/compute/v1/projects/foo-project-name/zones/europe-west6-a/instances" );
// Accesstoken getting from playground would be working!!!
r = googleTarget.request( MediaType.APPLICATION_JSON_TYPE ).header( "Authorization", "Bearer " + accesToken ).get( String.class );
} catch (Exception e) {
e.printStackTrace();
}
System.out.println( r );
}
/**
* Authorizes the installed application to access user's protected data.
*/
private static Credential authorize() throws Exception {
// initialize client secrets object
GoogleClientSecrets clientSecrets;
// load client secrets
clientSecrets = GoogleClientSecrets.load( JSON_FACTORY, new InputStreamReader(
RestForecast.class.getResourceAsStream( "/client_secrets.json" ) ) );
if (clientSecrets.getDetails().getClientId().startsWith( "Enter" )
|| clientSecrets.getDetails().getClientSecret().startsWith( "Enter " )) {
System.out.println( "Enter Client ID and Secret from https://code.google.com/apis/console/ "
+ "into compute-engine-cmdline-sample/src/main/resources/client_secrets.json" );
System.exit( 1 );
}
// set up authorization code flow
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
httpTransport, JSON_FACTORY, clientSecrets, SCOPES ).setDataStoreFactory( dataStoreFactory )
.build();
// authorize
return new AuthorizationCodeInstalledApp( flow, new LocalServerReceiver() ).authorize( "user" );
}
当我使用可以从https://developers.google.com/oauthplayground获得的 AccessToken 时,请求始终有效(至少我没有看到它不起作用)。
当我使用凭据库中的 AccessToken 时,有时它可以工作,有时我会收到以下错误:
javax.ws.rs.NotAuthorizedException: HTTP 401 Unauthorized
at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.handleErrorStatus(ClientInvocation.java:160)
at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.extractResult(ClientInvocation.java:135)
at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.invoke(ClientInvocation.java:425)
at org.jboss.resteasy.client.jaxrs.internal.ClientInvocationBuilder.get(ClientInvocationBuilder.java:165)
at RestForecast.main(RestForecast.java:77)
任何想法?
来自 401 的详细信息是:Invalid Token , Invalid Value
解决方案
推荐阅读
- asp.net-web-api2 - 如何在 Structuremap 中配置 Automapper
- c++ - 如何在c ++中对具有多个条件的结构元素进行排序
- python - 当我尝试导入 jupyter notebook 时安装的包给出“ModuleNotFoundError”(使用 venv)
- javascript - Fabric.js:如何保存没有背景图像的画布图像
- r - 将数据拆分为测试和训练 r studio
- authentication - 什么是无头身份验证?(通用术语)
- javascript - 更新 npm 包后出现“SyntaxError: Unexpected token '/' in...index.ejs while compiling ejs”错误
- java - Java,Android / 数组列表中的存储值,但列表为空
- vagrant - Vagrant ansible:选择 var fron 环境变量
- svg - SVG 过滤灰度到 Alpha