Google Analytics 403: Forbidden with p12 key in Java

504 views
Skip to first unread message

Fernando Guerra

unread,
Apr 8, 2015, 11:33:58 PM4/8/15
to google-analytic...@googlegroups.com
I'm trying to add an user to a Google Analytics account by API. I have the following code:

private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
private static final JsonFactory JSON_FACTORY = new JacksonFactory();
private static final String SERVICE_ACCOUNT_EMAIL = "XXX...@developer.gserviceaccount.com";

private static Credential authorize() throws Exception {
    GoogleCredential credential = new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT)
            .setJsonFactory(JSON_FACTORY)
            .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
            .setServiceAccountScopes(
                    Collections.singleton(AnalyticsScopes.ANALYTICS_MANAGE_USERS))
            .setServiceAccountPrivateKeyFromP12File(new File("/bla/MyKey.p12"))
            .build();
    credential.refreshToken();
    return credential;
}

public static void main(String[] args) {
    try {
        // Authorize this application to access the user's data.
        Credential credential = authorize();

        // Create an authorized Maps Engine client with the credential.
        Analytics analytics = new Analytics.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
                .setApplicationName(APPLICATION_NAME)
                .build();

        UserRef userRef = new UserRef();
        userRef.setEmail("user...@gmail.com");//add mail


        // Construct the permissions object.
        Permissions permissions = new Permissions();
        List local = Arrays.asList("EDIT", "MANAGE_USERS");
        permissions.setLocal(local);

        // Construct the body of the request
        EntityUserLink body = new EntityUserLink();
        body.setPermissions(permissions);
        body.setUserRef(userRef);

        analytics.management().profileUserLinks().insert("66666666", "UA-55555555-1",
                "888888888", body).execute();

    } catch (GoogleJsonResponseException e) {
        System.err.println("There was a service error: " + e.getDetails().getCode() + " : "
                + e.getDetails().getMessage());
        e.printStackTrace();

    } catch (IOException e) {
        System.out.println(e.getMessage());
    } catch (Throwable t) {
        t.printStackTrace();
    }
}


And I'm having the following error:

There was a service error: 403 : User does not have sufficient permissions for this account.
com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden

The JSON: { "code" : 403, "errors" : [ { "domain" : "global", "message" : "User does not have sufficient permissions for this account.", "reason" : "insufficientPermissions" } ], "message" : "User does not have sufficient permissions for this account." }

I dont know what to do. Any help? Thanks!


Matthew Cohoon

unread,
Apr 9, 2015, 11:35:39 AM4/9/15
to google-analytic...@googlegroups.com
You need to first add "XXX...@developer.gserviceaccount.com" to the authorized list of users who can Manage Users. The service account acts like a separate authorized user, and can be added at the Account, Property or View level.

-Matt

--
You received this message because you are subscribed to the Google Groups "Google Analytics Management API" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-analytics-mana...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Fernando Guerra

unread,
Apr 9, 2015, 2:15:21 PM4/9/15
to google-analytic...@googlegroups.com
Thank you!! That work!
To unsubscribe from this group and stop receiving emails from it, send an email to google-analytics-management-api+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages