首页 > 解决方案 > 我收到 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 );
}

这里的部分来自:https ://github.com/google/google-api-java-client-samples/blob/master/compute-engine-cmdline-sample/src/main/java/com/google/api /services/samples/computeengine/cmdline/ComputeEngineSample.java

    /**
     * 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

标签: google-cloud-platformgoogle-apigoogle-oauth

解决方案


推荐阅读