首页 文章

Google Analytics 403:禁止使用Java中的p12密钥

提问于
浏览
1

我正在尝试通过API将用户添加到Google Analytics帐户 . 我有以下代码:

private static final String APPLICATION_NAME = "MyNameProject";

private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();

private static final JsonFactory JSON_FACTORY = new JacksonFactory();

private static final String SERVICE_ACCOUNT_EMAIL = "XXXXXXX@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("usertoadd@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();
    }
}

我有以下错误:

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

JSON:{“code”:403,“errors”:[{“domain”:“global”,“message”:“用户对此帐户没有足够的权限 . ”,“reason”:“insufficientPermissions”}] ,“message”:“用户对此帐户没有足够的权限 . ” }

我不知道该怎么办 . 有帮助吗?谢谢!

1 回答

相关问题