Search code examples
javagoogle-cloud-platformgoogle-iam

Change User-Agent for Google IAM in Java


Is it possible to change user-agent information for API calls to Google IAM when we instantiate it? My code is like this:

    public static Iam initIam() throws IOException, GeneralSecurityException {
        InputStream resourceAsStream = GoogleIAM.class.getClassLoader().getResourceAsStream("creds.txt");
        GoogleCredential credential = GoogleCredential.fromStream(resourceAsStream);

        httpTransport = GoogleNetHttpTransport.newTrustedTransport();

        if (credential.createScopedRequired()) {
            List<String> scopes = new ArrayList<>();
            // Enable full Cloud Platform scope.
            scopes.add(IamScopes.CLOUD_PLATFORM);
            credential = credential.createScoped(scopes);
        }

        // Create IAM API object associated with the authenticated transport.
        return new Iam.Builder(httpTransport, JSON_FACTORY, credential).setApplicationName("test").build();
    }

I saw that for Google Storage is possible using HeaderProvider like this:

        Map<String, String> m = ImmutableMap.<String, String>builder().put("user-agent", "my custom useragent").build();
        HeaderProvider headerProvider = FixedHeaderProvider.create(m);

but I couldn't find out how to use HeaderProvider in Google IAM.

Any ideas?


Solution

  • It is actually already done in the code

    return new Iam.Builder(httpTransport, JSON_FACTORY, credential).setApplicationName("test").build();

    with the setApplicationName method wherein the Stackdriver log you can see that the application name, i.e. test, will be there in the user-agent field